secure_learning.models.secure_logistic¶
Implementation of Logistic regression model.
Classes¶
Class to store whether exponentations are approximated | |
Solver for logistic regression. Optimizes a model with objective function |
Module Contents¶
- class secure_learning.models.secure_logistic.ExponentiationTypes(*args, **kwds)[source]¶
Class to store whether exponentations are approximated or calculated exactly.
- class secure_learning.models.secure_logistic.Logistic(solver_type: tno.mpc.mpyc.secure_learning.models.secure_model.SolverTypes = SolverTypes.GD, exponentiation: ExponentiationTypes = ExponentiationTypes.EXACT, penalty: tno.mpc.mpyc.secure_learning.models.secure_model.PenaltyTypes = PenaltyTypes.NONE, **penalty_args: float)[source]¶
Solver for logistic regression. Optimizes a model with objective function $$left(frac{1}{2{n}_{textrm{samples}}}right) sum_{i=1}^{{n}_{textrm{samples}}}left(textrm{-}(1+y_i) log(h_w(x_i)) - (1-y_i) log(1-h_w(x_i))right)$$
Here, $$h_w(x) = frac{1}{(1 + e^{-w^T x}}$$
Labels $y_i$ are assumed to have value $-1$ or $1$.
The gradient is given by: $$g(X, y, w) = left(frac{1}{2} times {n}_{textrm{samples}}right) sum_{i=1}^{{n}_textrm{samples}} x_i^T left( (2h_w(x_i) - 1) - y right)$$
See secure_model.py docstrings for more information on solver types and penalties.
- 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 from the given parameters.
- Parameters:
X – Independent variables
y – Dependent variables
weights – Current weights vector
grad_per_sample – Return a list with gradient per sample instead of aggregated (summed) gradient
- Returns:
Gradient of objective function as specified in class docstring, evaluated from the provided parameters
- 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 mean accuracy of the prediction.
- Parameters:
X – Test data.
y – True label for $X$.
weights – Weight vector.
- Returns:
Score 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], prob: float = 0.5, **_kwargs: None) tno.mpc.mpyc.secure_learning.utils.Vector[mpyc.sectypes.SecureFixedPoint][source]¶
Predicts labels for input data to classification model. Label $-1$ is assigned of the predicted probability is less then prob, otherwise label $+1$ is assigned.
- Parameters:
X – Input data with all features
weights – Weight vector of classification model
prob – Threshold for labelling. Defaults to $0.5$.
_kwargs – Not used
- Returns:
Target labels of classification model
- 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]¶