Skip to content

Commit

Permalink
docs: address thomas review
Browse files Browse the repository at this point in the history
  • Loading branch information
robinstraub committed Mar 21, 2023
1 parent 5e729c1 commit 592e90c
Showing 1 changed file with 37 additions and 93 deletions.
130 changes: 37 additions & 93 deletions concrete-ml/svm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"In this tutorial, we will show how to create, train, and evaluate a Support Vector Machine (SVM) model using the Concrete-ML library, an open-source privacy-preserving machine learning framework based on fully homomorphic encryption (FHE).\n",
"\n",
"This tutorial is cut in 2 parts:\n",
"1. a quick setup of a LinearSVC model with Concrete-ML\n",
"2. a more in-depth approach taking a closer look at "
"1. A quick setup of a LinearSVC model with Concrete-ML\n",
"2. A more in-depth approach taking a closer look to the concrete-ml specifics\n"
]
},
{
Expand Down Expand Up @@ -41,15 +41,14 @@
"### Support Vector Machine\n",
"\n",
"SVM is a machine learning algorithm for classification and regression. LinearSVC is an efficient implementation of SVM\n",
"that assumes the data is linearly separable. In this tutorial, we will use the [iris dataset](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html) which is a common example for demonstrating how to work with SVMs.\n",
"that works best when the data is linearly separable. In this tutorial, we will use the [iris dataset](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html) which is a common example for demonstrating how to work with SVMs.\n",
"\n",
"Concrete-ML exposes a LinearSVC class which shares the same interface as\n",
"[scikit-learn LinearSVC](https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html), so you should\n",
"feel right at home.\n",
"Concrete-ML exposes a LinearSVC class which implements the\n",
"[scikit-learn LinearSVC](https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html) interface, so you should feel right at home.\n",
"\n",
"### Setup code\n",
"\n",
"Just as in any machine learning project, let's start by importing some libraries."
"Just as in any machine learning project, let's start by importing some libraries and setting the dataset."
]
},
{
Expand All @@ -74,7 +73,12 @@
"\n",
"# import the concrete-ml LinearSVC implementation\n",
"from concrete.ml.sklearn.svm import LinearSVC as ConcreteLinearSVC\n",
"from concrete.numpy import Configuration"
"\n",
"# Load the iris dataset and select the first 2 features \n",
"iris = datasets.load_iris()\n",
"\n",
"# Split the dataset into a training and a testing set\n",
"X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4, random_state=42)"
]
},
{
Expand All @@ -84,7 +88,7 @@
"source": [
"## Part 1: Train a simple model with Concrete-ML\n",
"\n",
"Let's first start by quickly scaffolding a concrete-ml LinearSVC code, so we can see how easy and familiar it is.\n"
"Let's first start by quickly scaffolding a Concrete-ML LinearSVC code, so we can see how easy and familiar it is.\n"
]
},
{
Expand All @@ -94,20 +98,14 @@
"metadata": {},
"outputs": [],
"source": [
"# Load the iris dataset and select the first 2 features \n",
"iris = datasets.load_iris()\n",
"\n",
"# Split the dataset into a training and a testing set\n",
"X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.4, random_state=42)\n",
"\n",
"# Train a model with scikit-learn LinearSVC, perform prediction and compute the accuracy\n",
"svm_sklearn = SklearnLinearSVC()\n",
"svm_sklearn.fit(X_train, y_train)\n",
"y_pred_sklearn = svm_sklearn.predict(X_test)\n",
"accuracy_sklearn = accuracy_score(y_test, y_pred_sklearn)\n",
"\n",
"# Perform the same steps with the Concrete-ML LinearSVC implementation\n",
"svm_concrete = ConcreteLinearSVC(8)\n",
"svm_concrete = ConcreteLinearSVC()\n",
"svm_concrete.fit(X_train, y_train)\n",
"y_pred_concrete_clear = svm_concrete.predict(X_test)\n",
"accuracy_concrete_clear = accuracy_score(y_test, y_pred_concrete_clear)\n",
Expand All @@ -116,7 +114,7 @@
"circuit = svm_concrete.compile(X_train)\n",
"circuit.client.keygen(force=False)\n",
"# Now that a circuit is compiled, the svm_concrete can predict value with FHE\n",
"y_pred_concrete_fhe = svm_concrete.predict(X_test, )\n",
"y_pred_concrete_fhe = svm_concrete.predict(X_test, execute_in_fhe=True)\n",
"accuracy_concrete_fhe = accuracy_score(y_test, y_pred_concrete_fhe)\n",
"\n",
"print(f\"Scikit-learn Accuracy: {accuracy_sklearn:.4f}\")\n",
Expand Down Expand Up @@ -157,13 +155,13 @@
"\n",
"```python\n",
"# Perform the same steps with the Concrete-ML LinearSVC implementation\n",
"svm_concrete = ConcreteLinearSVC(4)\n",
"svm_concrete = ConcreteLinearSVC()\n",
"svm_concrete.fit(X_train, y_train)\n",
"y_pred_concrete_clear = svm_concrete.predict(X_test)\n",
"accuracy_concrete_clear = accuracy_score(y_test, y_pred_sklearn)\n",
"```\n",
"\n",
"One thing to note here is not only the model is trained using clear data, but the prediction are also performed in a *clear environment*, there is no encryption at this stage.\n",
"One thing to note here is not only the model is trained using clear data, but the prediction are also performed in a *plain environment*, there is no encryption at this stage.\n",
"\n",
"In order to perform prediction in a FHE environment, the model first has to be compiled into a circuit.\n",
"\n",
Expand All @@ -174,11 +172,11 @@
"circuit = svm_concrete.compile(X_train)\n",
"circuit.client.keygen(force=False)\n",
"# Now that a circuit is compiled, the svm_concrete can predict value with FHE\n",
"y_pred_concrete_fhe = svm_concrete.predict(X_test, execute_in_fhe = True)\n",
"y_pred_concrete_fhe = svm_concrete.predict(X_test, execute_in_fhe=True)\n",
"accuracy_concrete_fhe = accuracy_score(y_test, y_pred_sklearn)\n",
"```\n",
"\n",
"Now that the model is compiled, computing predictions in with FHE is just a matter of passing a execution_in_fhe parameter with True. \n",
"Now that the model is compiled, computing predictions in with FHE is just a matter of passing a execution_in_fhe parameter set to True.\n",
"\n",
"\n",
"#### Accuracy\n",
Expand Down Expand Up @@ -238,18 +236,10 @@
},
{
"cell_type": "code",
"execution_count": 15,
"id": "a026cafb",
"execution_count": null,
"id": "80f701f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scikit-learn Accuracy: 0.9833\n"
]
}
],
"outputs": [],
"source": [
"# setup and train a scikit-learn LinearSVC model, just as before\n",
"svm_sklearn = SklearnLinearSVC()\n",
Expand All @@ -263,7 +253,7 @@
},
{
"cell_type": "markdown",
"id": "c7c838b8",
"id": "ce2920d8",
"metadata": {},
"source": [
"Not too shabby.\n",
Expand All @@ -279,34 +269,10 @@
},
{
"cell_type": "code",
"execution_count": 28,
"id": "2ac478a2",
"execution_count": null,
"id": "c7d66066",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 Bits Quantized Accuracy: 0.3833\n",
"1 Bits Quantized Accuracy: 0.3833\n",
"2 Bits Quantized Accuracy: 0.6833\n",
"3 Bits Quantized Accuracy: 0.8167\n",
"4 Bits Quantized Accuracy: 0.9000\n",
"5 Bits Quantized Accuracy: 0.9000\n",
"6 Bits Quantized Accuracy: 0.9833\n",
"7 Bits Quantized Accuracy: 0.9833\n",
"8 Bits Quantized Accuracy: 0.9833\n",
"9 Bits Quantized Accuracy: 0.9833\n",
"10 Bits Quantized Accuracy: 0.9833\n",
"11 Bits Quantized Accuracy: 0.9833\n",
"12 Bits Quantized Accuracy: 0.9833\n",
"13 Bits Quantized Accuracy: 0.9833\n",
"14 Bits Quantized Accuracy: 0.9833\n",
"15 Bits Quantized Accuracy: 0.9833\n",
"16 Bits Quantized Accuracy: 0.9833\n"
]
}
],
"outputs": [],
"source": [
"# compute the accuracy of a n_bit quantized linear ranging from 2 to 8 bits\n",
"for n_bits in range(2, 9):\n",
Expand All @@ -319,7 +285,7 @@
},
{
"cell_type": "markdown",
"id": "6d05af26",
"id": "122bd347",
"metadata": {},
"source": [
"### Step c: simulate the model execution\n",
Expand All @@ -338,18 +304,10 @@
},
{
"cell_type": "code",
"execution_count": 59,
"id": "486c342d",
"execution_count": null,
"id": "74f5bf79",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8 Bits FHE Accuracy: 0.9833\n"
]
}
],
"outputs": [],
"source": [
"# import the configuration from concrete-numpy\n",
"from concrete.numpy import Configuration\n",
Expand All @@ -376,7 +334,7 @@
},
{
"cell_type": "markdown",
"id": "15d47b06",
"id": "e1721c59",
"metadata": {},
"source": [
"*The virtual library enables some unsafe features, so it should understandably not be used for production*\n",
Expand All @@ -390,24 +348,10 @@
},
{
"cell_type": "code",
"execution_count": 60,
"id": "878f6c0b",
"execution_count": null,
"id": "8b4c9814",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 Bits Quantized Accuracy: 0.6833\n",
"3 Bits Quantized Accuracy: 0.8167\n",
"4 Bits Quantized Accuracy: 0.9167\n",
"5 Bits Quantized Accuracy: 0.9000\n",
"6 Bits Quantized Accuracy: 0.9833\n",
"7 Bits Quantized Accuracy: 0.9833\n",
"8 Bits Quantized Accuracy: 0.9833\n"
]
}
],
"outputs": [],
"source": [
"for n_bits in range(2, 9):\n",
" svm_concrete = ConcreteLinearSVC(n_bits)\n",
Expand All @@ -420,7 +364,7 @@
},
{
"cell_type": "markdown",
"id": "d744d7a2",
"id": "1785f094",
"metadata": {},
"source": [
"The model predictions in FHE (with virtual library) are aligned with the predictions of the plain, quantized model.\n",
Expand All @@ -437,7 +381,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4fb12f3c",
"id": "861502da",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -456,7 +400,7 @@
},
{
"cell_type": "markdown",
"id": "496f5318",
"id": "86cc8bee",
"metadata": {},
"source": [
"**[todo]: l'accuracy reste-t-elle la même?**"
Expand Down

0 comments on commit 592e90c

Please sign in to comment.