mirror of
https://github.com/luxfhe/docs.git
synced 2026-10-30 18:13:39 +00:00
2.8 MiB
2.8 MiB
In [1]:
# Source:
# https://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html
# Code source: Gaël Varoquaux
# Andreas Müller
# Modified for documentation by Jaques Grobler
# Modified to integrate Concrete ML functions by Zama
# License: BSD 3 clause
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)
from functools import partial
import torch
from concrete.ml.sklearn import (
DecisionTreeClassifier,
LinearSVC,
LogisticRegression,
NeuralNetClassifier,
RandomForestClassifier,
XGBClassifier,
)In [2]:
# The simulation mode allows to measure the impact of FHE execution on accuracy
# without paying the cost of FHE computations.
# However, data is not encrypted when using the simulation: the model performs inference
# on clear data.
%run utils/classifier_comparison_utils.pyIn [3]:
params_neural_net = {
"module__n_w_bits": 2,
"module__n_a_bits": 4,
"module__n_accum_bits": 32,
"module__n_hidden_neurons_multiplier": 6,
"module__n_layers": 2, # 1 hidden layer
"module__activation_function": torch.nn.ReLU,
"max_epochs": 400,
"verbose": 0,
"lr": 0.001,
}
neural_network_classifiers = [
(
partial(NeuralNetClassifier, batch_size=32, **params_neural_net),
"Neural Net",
),
]
# pylint: disable-next=undefined-variable
make_classifier_comparison("NN Classifiers", neural_network_classifiers, 0.5, simulate=True) # noqaIn [4]:
linear_classifiers = [
(partial(LinearSVC, C=0.025), "Linear SVC"),
(LogisticRegression, "Logistic Regression"),
]
# pylint: disable-next=undefined-variable
make_classifier_comparison("Linear Classifiers", linear_classifiers, 0, simulate=True, h=1) # noqaIn [5]:
tree_classifiers = [
(partial(DecisionTreeClassifier, max_depth=5), "Decision Tree"),
(partial(RandomForestClassifier, max_depth=4, n_estimators=5), "Random Forest"),
(partial(XGBClassifier, n_jobs=1, max_depth=4, n_estimators=5), "XGB"),
]
# pylint: disable-next=undefined-variable
make_classifier_comparison( # noqa
"Tree-Based Classifiers", tree_classifiers, 0.5, simulate=True, h=0.1
)