Skip to content

Latest commit

 

History

History
4705 lines (2790 loc) · 158 KB

concrete.ml.sklearn.base.md

File metadata and controls

4705 lines (2790 loc) · 158 KB

module concrete.ml.sklearn.base

Base classes for all estimators.

Global Variables

  • USE_OLD_VL
  • OPSET_VERSION_FOR_ONNX_EXPORT
  • QNN_AUTO_KWARGS

class BaseEstimator

Base class for all estimators in Concrete ML.

This class does not inherit from sklearn.base.BaseEstimator as it creates some conflicts with skorch in QuantizedTorchEstimatorMixin's subclasses (more specifically, the get_params method is not properly inherited).

Attributes:

  • _is_a_public_cml_model (bool): Private attribute indicating if the class is a public model (as opposed to base or mixin classes).

method __init__

__init__()

Initialize the base class with common attributes used in all estimators.

An underscore "_" is appended to attributes that were created while fitting the model. This is done in order to follow scikit-Learn's standard format. More information available in their documentation: https://scikit-learn.org/stable/developers/develop.html#:~:text=Estimated%20Attributes%C2%B6


property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

De-quantize the output.

This step ensures that the fit method has been called.

Args:

  • q_y_preds (numpy.ndarray): The quantized output values to de-quantize.

Returns:

  • numpy.ndarray: The de-quantized output values.

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

Fit the estimator.

This method trains a scikit-learn estimator, computes its ONNX graph and defines the quantization parameters needed for proper FHE inference.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The fitted estimator.


method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

Apply post-processing to the de-quantized predictions.

This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.

For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params.

Args:

  • y_preds (numpy.ndarray): The de-quantized predictions to post-process.

Returns:

  • numpy.ndarray: The post-processed predictions.

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict values for X, in FHE or in the clear.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • np.ndarray: The predicted values for X.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

Quantize the input.

This step ensures that the fit method has been called.

Args:

  • X (numpy.ndarray): The input values to quantize.

Returns:

  • numpy.ndarray: The quantized input values.

class BaseClassifier

Base class for linear and tree-based classifiers in Concrete ML.

This class inherits from BaseEstimator and modifies some of its methods in order to align them with classifier behaviors. This notably include applying a sigmoid/softmax post-processing to the predicted values as well as handling a mapping of classes in case they are not ordered.

method __init__

__init__()

Initialize the base class with common attributes used in all estimators.

An underscore "_" is appended to attributes that were created while fitting the model. This is done in order to follow scikit-Learn's standard format. More information available in their documentation: https://scikit-learn.org/stable/developers/develop.html#:~:text=Estimated%20Attributes%C2%B6


property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property n_classes_

Get the model's number of classes.

Using this attribute is deprecated.

Returns:

  • int: The model's number of classes.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

property target_classes_

Get the model's classes.

Using this attribute is deprecated.

Returns:

  • Optional[numpy.ndarray]: The model's classes.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

De-quantize the output.

This step ensures that the fit method has been called.

Args:

  • q_y_preds (numpy.ndarray): The quantized output values to de-quantize.

Returns:

  • numpy.ndarray: The de-quantized output values.

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method predict_proba

predict_proba(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict class probabilities.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • numpy.ndarray: The predicted class probabilities.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

Quantize the input.

This step ensures that the fit method has been called.

Args:

  • X (numpy.ndarray): The input values to quantize.

Returns:

  • numpy.ndarray: The quantized input values.

class QuantizedTorchEstimatorMixin

Mixin that provides quantization for a torch module and follows the Estimator API.

method __init__

__init__()

property base_module

Get the Torch module.

Returns:

  • SparseQuantNeuralNetwork: The fitted underlying module.

property fhe_circuit


property input_quantizers

Get the input quantizers.

Returns:

  • List[UniformQuantizer]: The input quantizers.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

property output_quantizers

Get the output quantizers.

Returns:

  • List[UniformQuantizer]: The output quantizers.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

method dequantize_output

dequantize_output(*q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

Fit he estimator.

If the module was already initialized, the module will be re-initialized unless warm_start is set to True. In addition to the torch training step, this method performs quantization of the trained Torch model using Quantization Aware Training (QAT).

Values of dtype float64 are not supported and will be casted to float32.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • **fit_parameters: Keyword arguments to pass to skorch's fit method.

Returns: The fitted estimator.


method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

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 method differs from the BaseEstimator's fit_benchmark method as QNNs use QAT instead of PTQ. Hence, here, the float model is topologically equivalent as we have less control over the influence of QAT over the weights.

Values of dtype float64 are not supported and will be casted to float32.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. However, skorch does not handle such a parameter and setting it will have no effect. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to skorch's fit method.

Returns: The Concrete ML and equivalent skorch fitted estimators.


method get_params

get_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is overloaded in order to make sure that auto-computed parameters are not considered when cloning the model (e.g during a GridSearchCV call).

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

  • params (dict): Parameter names mapped to their values.

method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → Dict

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict values for X, in FHE or in the clear.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • np.ndarray: The predicted values for X.

method prune

prune(X: 'Data', y: 'Target', n_prune_neurons_percentage: 'float', **fit_params)

Prune a copy of this Neural Network model.

This can be used when the number of neurons on the hidden layers is too high. For example, when creating a Neural Network model with n_hidden_neurons_multiplier high (3-4), it can be used to speed up the model inference in FHE. Many times, up to 50% of neurons can be pruned without losing accuracy, when using this function to fine-tune an already trained model with good accuracy. This method should be used once good accuracy is obtained.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame Pandas Series or List.
  • n_prune_neurons_percentage (float): The percentage of neurons to remove. A value of 0 (resp. 1.0) means no (resp. all) neurons will be removed.
  • fit_params: Additional parameters to pass to the underlying nn.Module's forward method.

Returns: A new pruned copy of the Neural Network model.

Raises:

  • ValueError: If the model has not been trained or has already been pruned.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class BaseTreeEstimatorMixin

Mixin class for tree-based estimators.

This class inherits from sklearn.base.BaseEstimator in order to have access to scikit-learn's get_params and set_params methods.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]')

Initialize the TreeBasedEstimatorMixin.

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 leaves. If a dict is passed, then it should contain "op_inputs" and "op_leaves" as keys with corresponding number of quantization bits so that: - op_inputs (mandatory): number of bits to quantize the input values - op_leaves (optional): number of bits to quantize the leaves Default to 6.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(*args, **kwargs) → Circuit

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Optional[ndarray]' = None,
    n_bits: 'int' = 10
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Optional[Data]): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • n_bits (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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class BaseTreeRegressorMixin

Mixin class for tree-based regressors.

This class is used to create a tree-based regressor class that inherits from sklearn.base.RegressorMixin, which essentially gives access to scikit-learn's score method for regressors.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]')

Initialize the TreeBasedEstimatorMixin.

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 leaves. If a dict is passed, then it should contain "op_inputs" and "op_leaves" as keys with corresponding number of quantization bits so that: - op_inputs (mandatory): number of bits to quantize the input values - op_leaves (optional): number of bits to quantize the leaves Default to 6.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(*args, **kwargs) → Circuit

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Optional[ndarray]' = None,
    n_bits: 'int' = 10
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Optional[Data]): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • n_bits (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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class BaseTreeClassifierMixin

Mixin class for tree-based classifiers.

This class is used to create a tree-based classifier class that inherits from sklearn.base.ClassifierMixin, which essentially gives access to scikit-learn's score method for classifiers.

Additionally, this class adjusts some of the tree-based base class's methods in order to make them compliant with classification workflows.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]')

Initialize the TreeBasedEstimatorMixin.

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 leaves. If a dict is passed, then it should contain "op_inputs" and "op_leaves" as keys with corresponding number of quantization bits so that: - op_inputs (mandatory): number of bits to quantize the input values - op_leaves (optional): number of bits to quantize the leaves Default to 6.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property n_classes_

Get the model's number of classes.

Using this attribute is deprecated.

Returns:

  • int: The model's number of classes.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

property target_classes_

Get the model's classes.

Using this attribute is deprecated.

Returns:

  • Optional[numpy.ndarray]: The model's classes.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(*args, **kwargs) → Circuit

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Optional[ndarray]' = None,
    n_bits: 'int' = 10
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Optional[Data]): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • n_bits (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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method predict_proba

predict_proba(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict class probabilities.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • numpy.ndarray: The predicted class probabilities.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnLinearModelMixin

A Mixin class for sklearn linear models with FHE.

This class inherits from sklearn.base.BaseEstimator in order to have access to scikit-learn's get_params and set_params methods.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]' = 8)

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.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Data',
    n_bits: 'Union[int, Dict[str, int]]' = 8
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • 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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

Apply post-processing to the de-quantized predictions.

This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.

For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params.

Args:

  • y_preds (numpy.ndarray): The de-quantized predictions to post-process.

Returns:

  • numpy.ndarray: The post-processed predictions.

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict values for X, in FHE or in the clear.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • np.ndarray: The predicted values for X.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnLinearRegressorMixin

A Mixin class for sklearn linear regressors with FHE.

This class is used to create a linear regressor class that inherits from sklearn.base.RegressorMixin, which essentially gives access to scikit-learn's score method for regressors.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]' = 8)

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.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Data',
    n_bits: 'Union[int, Dict[str, int]]' = 8
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • 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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

Apply post-processing to the de-quantized predictions.

This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.

For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params.

Args:

  • y_preds (numpy.ndarray): The de-quantized predictions to post-process.

Returns:

  • numpy.ndarray: The post-processed predictions.

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict values for X, in FHE or in the clear.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • np.ndarray: The predicted values for X.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnLinearClassifierMixin

A Mixin class for sklearn linear classifiers with FHE.

This class is used to create a linear classifier class that inherits from sklearn.base.ClassifierMixin, which essentially gives access to scikit-learn's score method for classifiers.

Additionally, this class adjusts some of the tree-based base class's methods in order to make them compliant with classification workflows.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]' = 8)

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.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property n_classes_

Get the model's number of classes.

Using this attribute is deprecated.

Returns:

  • int: The model's number of classes.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

property target_classes_

Get the model's classes.

Using this attribute is deprecated.

Returns:

  • Optional[numpy.ndarray]: The model's classes.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method decision_function

decision_function(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict confidence scores.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • numpy.ndarray: The predicted confidence scores.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Data',
    n_bits: 'Union[int, Dict[str, int]]' = 8
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • 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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method predict_proba

predict_proba(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnSGDRegressorMixin

A Mixin class for sklearn SGD regressors with FHE.

This class is used to create a SGD regressor class what can be exported to ONNX using Hummingbird.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]' = 8)

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.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Data',
    n_bits: 'Union[int, Dict[str, int]]' = 8
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • 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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

Apply post-processing to the de-quantized predictions.

This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.

For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params.

Args:

  • y_preds (numpy.ndarray): The de-quantized predictions to post-process.

Returns:

  • numpy.ndarray: The post-processed predictions.

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict values for X, in FHE or in the clear.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • np.ndarray: The predicted values for X.

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnSGDClassifierMixin

A Mixin class for sklearn SGD classifiers with FHE.

This class is used to create a SGD classifier class what can be exported to ONNX using Hummingbird.

method __init__

__init__(n_bits: 'Union[int, Dict[str, int]]' = 8)

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.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property n_classes_

Get the model's number of classes.

Using this attribute is deprecated.

Returns:

  • int: The model's number of classes.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

property target_classes_

Get the model's classes.

Using this attribute is deprecated.

Returns:

  • Optional[numpy.ndarray]: The model's classes.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method decision_function

decision_function(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Predict confidence scores.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • numpy.ndarray: The predicted confidence scores.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


classmethod from_sklearn_model

from_sklearn_model(
    sklearn_model: 'BaseEstimator',
    X: 'Data',
    n_bits: 'Union[int, Dict[str, int]]' = 8
)

Build a FHE-compliant model using a fitted scikit-learn model.

Args:

  • sklearn_model (sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
  • X (Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • 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.

Returns: The FHE-compliant fitted model.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method predict_proba

predict_proba(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnKNeighborsMixin

A Mixin class for sklearn KNeighbors models with FHE.

This class inherits from sklearn.base.BaseEstimator in order to have access to scikit-learn's get_params and set_params methods.

method __init__

__init__(n_bits: 'int' = 3)

Initialize the FHE knn model.

Args:

  • n_bits (int): Number of bits to quantize the model. The value will be used for quantizing inputs and X_fit. Default to 3.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

method get_topk_labels

get_topk_labels(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Return the K-nearest labels of each point.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • numpy.ndarray: The K-Nearest labels for each point.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method majority_vote

majority_vote(nearest_classes: 'ndarray')

Determine the most common class among nearest neighborsfor each query.

Args:

  • nearest_classes (numpy.ndarray): The class labels of the nearest neighbors for a query

Returns:

  • numpy.ndarray: The majority-voted class label for the corresponding query.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

Provide the majority vote among the topk labels of each point.

For KNN, the de-quantization step is not required. Because _inference returns the label of the k-nearest neighbors.

Args:

  • y_preds (numpy.ndarray): The topk nearest labels for each point.

Returns:

  • numpy.ndarray: The majority vote for each point.

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method quantize_input

quantize_input(X: 'ndarray') → ndarray

class SklearnKNeighborsClassifierMixin

A Mixin class for sklearn KNeighbors classifiers with FHE.

This class is used to create a KNeighbors classifier class that inherits from SklearnKNeighborsMixin and sklearn.base.ClassifierMixin. By inheriting from sklearn.base.ClassifierMixin, it allows this class to be recognized as a classifier."

method __init__

__init__(n_bits: 'int' = 3)

Initialize the FHE knn model.

Args:

  • n_bits (int): Number of bits to quantize the model. The value will be used for quantizing inputs and X_fit. Default to 3.

property fhe_circuit

Get the FHE circuit.

The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/get-started/terminology) Is None if the model is not fitted.

Returns:

  • Circuit: The FHE circuit.

property is_compiled

Indicate if the model is compiled.

Returns:

  • bool: If the model is compiled.

property is_fitted

Indicate if the model is fitted.

Returns:

  • bool: If the model is fitted.

property onnx_model

Get the ONNX model.

Is None if the model is not fitted.

Returns:

  • onnx.ModelProto: The ONNX model.

method check_model_is_compiled

check_model_is_compiled() → None

Check if the model is compiled.

Raises:

  • AttributeError: If the model is not compiled.

method check_model_is_fitted

check_model_is_fitted() → None

Check if the model is fitted.

Raises:

  • AttributeError: If the model is not fitted.

method compile

compile(
    X: 'Data',
    configuration: 'Optional[Configuration]' = None,
    artifacts: 'Optional[DebugArtifacts]' = None,
    show_mlir: 'bool' = False,
    p_error: 'Optional[float]' = None,
    global_p_error: 'Optional[float]' = None,
    verbose: 'bool' = False,
    device: 'str' = 'cpu'
) → Circuit

Compile the model.

Args:

  • X (Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
  • configuration (Optional[Configuration]): Options to use for compilation. Default to None.
  • artifacts (Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
  • show_mlir (bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
  • p_error (Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
  • global_p_error (Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
  • verbose (bool): Indicate if compilation information should be printed during compilation. Default to False.
  • device: FHE compilation device, can be either 'cpu' or 'cuda'.

Returns:

  • Circuit: The compiled Circuit.

method dequantize_output

dequantize_output(q_y_preds: 'ndarray') → ndarray

method dump

dump(file: 'TextIO') → None

Dump itself to a file.

Args:

  • file (TextIO): The file to dump the serialized object into.

method dump_dict

dump_dict() → Dict[str, Any]

Dump the object as a dict.

Returns:

  • Dict[str, Any]: Dict of serialized objects.

method dumps

dumps() → str

Dump itself to a string.

Returns:

  • metadata (str): String of the serialized object.

method fit

fit(X: 'Data', y: 'Target', **fit_parameters)

method fit_benchmark

fit_benchmark(
    X: 'Data',
    y: 'Target',
    random_state: 'Optional[int]' = None,
    **fit_parameters
)

Fit both the Concrete ML and its equivalent float estimators.

Args:

  • X (Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • y (Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
  • random_state (Optional[int]): The random state to use when fitting. Defaults to None.
  • **fit_parameters: Keyword arguments to pass to the float estimator's fit method.

Returns: The Concrete ML and float equivalent fitted estimators.


method get_sklearn_params

get_sklearn_params(deep: 'bool' = True) → dict

Get parameters for this estimator.

This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params method in order to not break its implementation of set_params.

Args:

  • deep (bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.

Returns:

  • params (dict): Parameter names mapped to their values.

method get_topk_labels

get_topk_labels(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

Return the K-nearest labels of each point.

Args:

  • X (Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
  • fhe (Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.

Returns:

  • numpy.ndarray: The K-Nearest labels for each point.

classmethod load_dict

load_dict(metadata: 'Dict[str, Any]') → BaseEstimator

Load itself from a dict.

Args:

  • metadata (Dict[str, Any]): Dict of serialized objects.

Returns:

  • BaseEstimator: The loaded object.

method majority_vote

majority_vote(nearest_classes: 'ndarray')

Determine the most common class among nearest neighborsfor each query.

Args:

  • nearest_classes (numpy.ndarray): The class labels of the nearest neighbors for a query

Returns:

  • numpy.ndarray: The majority-voted class label for the corresponding query.

method post_processing

post_processing(y_preds: 'ndarray') → ndarray

Provide the majority vote among the topk labels of each point.

For KNN, the de-quantization step is not required. Because _inference returns the label of the k-nearest neighbors.

Args:

  • y_preds (numpy.ndarray): The topk nearest labels for each point.

Returns:

  • numpy.ndarray: The majority vote for each point.

method predict

predict(
    X: 'Data',
    fhe: 'Union[FheMode, str]' = <FheMode.DISABLE: 'disable'>
) → ndarray

method quantize_input

quantize_input(X: 'ndarray') → ndarray