concrete.ml.sklearn.base.md
Last updated
Last updated
concrete.ml.sklearn.base
Module that contains base classes for our libraries estimators.
OPSET_VERSION_FOR_ONNX_EXPORT
get_sklearn_models
Return the list of available models in Concrete-ML.
Returns: the lists of models in Concrete-ML
get_sklearn_linear_models
Return the list of available linear models in Concrete-ML.
Args:
classifier
(bool): whether you want classifiers or not
regressor
(bool): whether you want regressors or not
str_in_class_name
(str): if not None, only return models with this as a substring in the class name
Returns: the lists of linear models in Concrete-ML
get_sklearn_tree_models
Return the list of available tree models in Concrete-ML.
Args:
classifier
(bool): whether you want classifiers or not
regressor
(bool): whether you want regressors or not
str_in_class_name
(str): if not None, only return models with this as a substring in the class name
Returns: the lists of tree models in Concrete-ML
get_sklearn_neural_net_models
Return the list of available neural net models in Concrete-ML.
Args:
classifier
(bool): whether you want classifiers or not
regressor
(bool): whether you want regressors or not
str_in_class_name
(str): if not None, only return models with this as a substring in the class name
Returns: the lists of neural net models in Concrete-ML
QuantizedTorchEstimatorMixin
Mixin that provides quantization for a torch module and follows the Estimator API.
This class should be mixed in with another that provides the full Estimator API. This class only provides modifiers for .fit() (with quantization) and .predict() (optionally in FHE)
__init__
property base_estimator_type
Get the sklearn estimator that should be trained by the child class.
property base_module_to_compile
Get the Torch module that should be compiled to FHE.
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[Quantizer]
: the input quantizers
property n_bits_quant
Get the number of quantization bits.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model_
(onnx.ModelProto): the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths
p_error
(Optional[float]): probability of error of a single PBS
global_p_error
(Optional[float]): probability of error of the full circuit. Not simulated by the VL, i.e., taken as 0
verbose_compilation
(bool): whether to show compilation information
Returns:
Circuit
: the compiled Circuit.
Raises:
ValueError
: if called before the model is trained
fit
Initialize and fit the module.
If the module was already initialized, by calling fit, the module will be re-initialized (unless warm_start
is True). In addition to the torch training step, this method performs quantization of the trained torch model.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
**fit_params
: additional parameters that can be used during training, these are passed to the torch training interface
Returns:
self
: the trained quantized estimator
fit_benchmark
Fit the quantized estimator as well as its equivalent float estimator.
This function returns both the quantized estimator (itself) as well as its non-quantized (float) equivalent, which are both trained separately. This is useful in order to compare performances between quantized and fp32 versions.
Args:
X
: The training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The labels associated with the training data
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
Returns:
self
: The trained quantized estimator
fp32_model
: The trained float equivalent estimator
get_params_for_benchmark
Get the parameters to instantiate the sklearn estimator trained by the child class.
Returns:
params
(dict): dictionary with parameters that will initialize a new Estimator
post_processing
Post-processing the output.
Args:
y_preds
(numpy.ndarray): the output to post-process
Raises:
ValueError
: if unknown post-processing function
Returns:
numpy.ndarray
: the post-processed output
predict
Predict on user provided data.
Predicts using the quantized clear or FHE classifier
Args:
X
: input data, a numpy array of raw values (non quantized)
execute_in_fhe
: whether to execute the inference in FHE or in the clear
Returns:
y_pred
: numpy ndarray with predictions
predict_proba
Predict on user provided data, returning probabilities.
Predicts using the quantized clear or FHE classifier
Args:
X
: input data, a numpy array of raw values (non quantized)
execute_in_fhe
: whether to execute the inference in FHE or in the clear
Returns:
y_pred
: numpy ndarray with probabilities (if applicable)
Raises:
ValueError
: if the estimator was not yet trained or compiled
BaseTreeEstimatorMixin
Mixin class for tree-based estimators.
A place to share methods that are used on all tree-based estimators.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): number of bits used for quantization
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): set to True to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a single PBS
global_p_error
(Optional[float]): probability of error of the full circuit. Not simulated by the VL, i.e., taken as 0
verbose_compilation
(bool): whether to show compilation information
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Dequantize the integer predictions.
Args:
y_preds
(numpy.ndarray): the predictions
Returns: the dequantized predictions
fit_benchmark
Fit the sklearn tree-based model and the FHE tree-based model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[ConcreteEstimators, SklearnEstimators]: The FHE and sklearn tree-based models.
quantize_input
Quantize the input.
Args:
X
(numpy.ndarray): the input
Returns: the quantized input
BaseTreeRegressorMixin
Mixin class for tree-based regressors.
A place to share methods that are used on all tree-based regressors.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): number of bits used for quantization
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): set to True to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a single PBS
global_p_error
(Optional[float]): probability of error of the full circuit. Not simulated by the VL, i.e., taken as 0
verbose_compilation
(bool): whether to show compilation information
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Dequantize the integer predictions.
Args:
y_preds
(numpy.ndarray): the predictions
Returns: the dequantized predictions
fit
Fit the tree-based estimator.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
**kwargs
: args for super().fit
Returns:
Any
: The fitted model.
fit_benchmark
Fit the sklearn tree-based model and the FHE tree-based model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[ConcreteEstimators, SklearnEstimators]: The FHE and sklearn tree-based models.
post_processing
Apply post-processing to the predictions.
Args:
y_preds
(numpy.ndarray): The predictions.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict the probability.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute in FHE. Defaults to False.
Returns:
numpy.ndarray
: The predicted probabilities.
quantize_input
Quantize the input.
Args:
X
(numpy.ndarray): the input
Returns: the quantized input
BaseTreeClassifierMixin
Mixin class for tree-based classifiers.
A place to share methods that are used on all tree-based classifiers.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): number of bits used for quantization
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): set to True to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a single PBS
global_p_error
(Optional[float]): probability of error of the full circuit. Not simulated by the VL, i.e., taken as 0
verbose_compilation
(bool): whether to show compilation information
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Dequantize the integer predictions.
Args:
y_preds
(numpy.ndarray): the predictions
Returns: the dequantized predictions
fit
Fit the tree-based estimator.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
**kwargs
: args for super().fit
Returns:
Any
: The fitted model.
fit_benchmark
Fit the sklearn tree-based model and the FHE tree-based model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[ConcreteEstimators, SklearnEstimators]: The FHE and sklearn tree-based models.
post_processing
Apply post-processing to the predictions.
Args:
y_preds
(numpy.ndarray): The predictions.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict the class with highest probability.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute in FHE. Defaults to False.
Returns:
numpy.ndarray
: The predicted target values.
predict_proba
Predict the probability.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute in FHE. Defaults to False.
Returns:
numpy.ndarray
: The predicted probabilities.
quantize_input
Quantize the input.
Args:
X
(numpy.ndarray): the input
Returns: the quantized input
SklearnLinearModelMixin
A Mixin class for sklearn linear models with FHE.
__init__
Initialize the FHE linear model.
Args:
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
clean_graph
Clean the graph of the onnx model.
This will remove the Cast node in the model's onnx.graph since they have no use in quantized or FHE models.
compile
Compile the FHE linear model.
Args:
X
(numpy.ndarray): The input data.
configuration
(Optional[Configuration]): Configuration object to use during compilation
compilation_artifacts
(Optional[DebugArtifacts]): Artifacts object to fill during compilation
show_mlir
(bool): If set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo. Defaults to False.
use_virtual_lib
(bool): Whether to compile using the virtual library that allows higher bitwidths with simulated FHE computation. Defaults to False
p_error
(Optional[float]): Probability of error of a single PBS
global_p_error
(Optional[float]): probability of error of the full circuit. Not simulated by the VL, i.e., taken as 0
verbose_compilation
(bool): whether to show compilation information
Returns:
Circuit
: The compiled Circuit.
dequantize_output
Dequantize the output.
Args:
q_y_preds
(numpy.ndarray): The quantized output to dequantize
Returns:
numpy.ndarray
: The dequantized output
fit
Fit the FHE linear model.
Args:
X
: Training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.