secure_learning.models.secure_model¶
Abstract class for secure-learning models.
Attributes¶
Classes¶
The possible solver types associated to models. | |
The possible penalty types associated to models. | |
Abstract secure-learn model class. |
Module Contents¶
- class secure_learning.models.secure_model.SolverTypes(*args, **kwds)[source]¶
The possible solver types associated to models.
- class secure_learning.models.secure_model.PenaltyTypes(*args, **kwds)[source]¶
The possible penalty types associated to models.
- class secure_learning.models.secure_model.Model(solver_type: SolverTypes = SolverTypes.GD, penalty: PenaltyTypes = PenaltyTypes.NONE, **penalty_args: float)[source]¶
Abstract secure-learn model class.
- property solver: tno.mpc.mpyc.secure_learning.solvers.solver.Solver[source]¶
Return solver used by current model.
- Raises:
SecureLearnUninitializedSolverError – raised when solver is not yet initiated
- Returns:
Solver used by current model.
- initialize_solver(solver_type: SolverTypes, penalty: PenaltyTypes, **penalty_args: float) None[source]¶
Initialize solver.
- Parameters:
solver_type – Type of the requested solver.
penalty – Type of penalties
penalty_args – Coefficient(s) of the given penalty
- gradient_function(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], weights: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], grad_per_sample: False) tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint][source]¶
- gradient_function(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], weights: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], grad_per_sample: True) List[tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint]]
- gradient_function(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], weights: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], grad_per_sample: bool) tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint] | List[tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint]]
- gradient_function(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], weights: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], grad_per_sample: True) List[tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint]]
Evaluate the gradient function.
- Parameters:
X – Independent data.
y – Dependent data.
weights – Weight vector.
grad_per_sample – Return gradient per sample if True, return aggregated gradient of all data if False.
- Returns:
Value(s) of gradient evaluated with the provided parameters.
- abstractmethod score(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], weights: tno.mpc.mpyc.secure_learning.utils.Vector[float] | tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint]) mpyc.sectypes.SecureFixedPoint[source]¶
Compute the model score.
- Parameters:
X – Test data.
y – True value for $X$.
weights – Weight vector.
- Returns:
Score of the model prediction.
- async compute_weights_mpc(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], tolerance: float = 0.01, minibatch_size: int | None = None, weights_init: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint] | None = None, nr_maxiters: int = 100, print_progress: bool = False, secure_permutations: bool = False) tno.mpc.mpyc.secure_learning.utils.Vector[float][source]¶
Train the model, compute and return the model weights.
- Parameters:
X – Training data
y – Target vector
tolerance – Threshold for convergence
minibatch_size – The size of the minibatch
weights_init – Initial weight vector to use
nr_maxiters – Threshold for the number of iterations
print_progress – Set to True to print progress every few iterations
secure_permutations – Set to True to perform matrix permutation securely
- Raises:
SecureLearnTypeError – if the training or target data does not consist of secure numbers
- Returns:
Weight vector
- async cross_validate(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], y: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], tolerance: float = 0.01, minibatch_size: int | None = None, weights_init: tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint] | None = None, nr_maxiters: int = 100, print_progress: bool = False, secure_permutations: bool = False, folds: int | List[Tuple[List[int], List[int]]] = 5) tno.mpc.mpyc.secure_learning.utils.Vector[float][source]¶
Evaluate metrics over the model prediction using CV.
- Parameters:
X – Train data.
y – Target variable for X
tolerance – Threshold for convergence
minibatch_size – The size of the minibatch
weights_init – Initial weight vector to use
nr_maxiters – Threshold for the number of iterations
print_progress – Set to True to print progress
secure_permutations – Set to True to perform matrix permutation securely
folds – Folding sets. If set to $k$ (integer) then a KFold (from sklearn.model_selection) is used. If it is not set then KFold is called with $k=5$. It also possible to pass custom folds as a list of tuples of train and test indexes: e.g. $[([2, 3], [0, 1, 4]), ([0, 1, 3], [2, 4]), ([0, 1, 2], [3, 4])]$ is a 3-fold of an array of five elements $([2, 3] , [0, 1, 4] )$ -> 1st fold, elements with indexes $[2, 3]$ are used in the train set, while elements with indexes [0, 1, 4] are used in the test set $([0, 1, 3] , [2, 4] )$ -> 2nd fold, elements with indexes $[0, 1, 3]$ are used in the train set, while elements with indexes [2, 4] are used in the test set $([0, 1, 2] , [3, 4] )$ -> 3rd fold, elements with indexes $[0, 1, 2]$ are used in the train set, while elements with indexes $[3, 4]$ are used in the test set
- Returns:
List of scores of the model prediction.
- static predict(X: tno.mpc.mpyc.secure_learning.utils.Matrix[mpyc.sectypes.SecureFixedPoint], weights: tno.mpc.mpyc.secure_learning.utils.Vector[float] | tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint], **kwargs: Any) List[mpyc.sectypes.SecureFixedPoint][source]¶
- Abstractmethod:
Predicts target values for input data.
- Parameters:
X – Input data with all features
weights – Weight vector of the model
kwargs – Additional keyword arguments that are needed to predict
- Returns:
Target values