diff --git a/tutorials/Tutorial_3_Introduction_to_Access_Control.ipynb b/tutorials/Tutorial_3_Introduction_to_Access_Control.ipynb index 6e594add..5116f09c 100644 --- a/tutorials/Tutorial_3_Introduction_to_Access_Control.ipynb +++ b/tutorials/Tutorial_3_Introduction_to_Access_Control.ipynb @@ -111,7 +111,7 @@ "\n", "We have now generated features and labels for our model to learn. In the scenarios we explore in this tutorial, we would like to ensure that each party only has access to some subset of the data we have generated. To do so, we will use special save / load methods that CrypTen provides to handle loading only to a specified party and synchronizing across processes. \n", "\n", - "We will use `crypten.save()` here to save data from a particular source, then we will load using `crypten.load()` in each example to load on a particular source. The following code will save all data we will use to files, then each example will load its data as necessary.\n", + "We will use `crypten.save_from_party()` (by default, it saves the data to file) here to save data from a particular source, then we will load using `crypten.load_from_party()` (by default, it loads data from a file) in each example to load on a particular source. The following code will save all data we will use to files, then each example will load its data as necessary.\n", "\n", "(Note that because we are operating on a single machine, all processes will have access to all of the files we are using. However, this still will work as expected when operating across machines.)" ] @@ -160,21 +160,21 @@ " features_alice = features[:50]\n", " features_bob = features[50:]\n", " \n", - " crypten.save(features_alice, filenames[\"features_alice\"], src=ALICE)\n", - " crypten.save(features_bob, filenames[\"features_bob\"], src=BOB)\n", + " crypten.save_from_party(features_alice, filenames[\"features_alice\"], src=ALICE)\n", + " crypten.save_from_party(features_bob, filenames[\"features_bob\"], src=BOB)\n", " \n", " # Save split dataset for Dataset Aggregation example\n", " samples_alice = features[:, :500]\n", " samples_bob = features[:, 500:]\n", - " crypten.save(samples_alice, filenames[\"samples_alice\"], src=ALICE)\n", - " crypten.save(samples_bob, filenames[\"samples_bob\"], src=BOB)\n", + " crypten.save_from_party(samples_alice, filenames[\"samples_alice\"], src=ALICE)\n", + " crypten.save_from_party(samples_bob, filenames[\"samples_bob\"], src=BOB)\n", " \n", " # Save true model weights and biases for Model Hiding example\n", - " crypten.save(w_true, filenames[\"w_true\"], src=ALICE)\n", - " crypten.save(b_true, filenames[\"b_true\"], src=ALICE)\n", + " crypten.save_from_party(w_true, filenames[\"w_true\"], src=ALICE)\n", + " crypten.save_from_party(b_true, filenames[\"b_true\"], src=ALICE)\n", " \n", - " crypten.save(test_features, filenames[\"test_features\"], src=BOB)\n", - " crypten.save(test_labels, filenames[\"test_labels\"], src=BOB)\n", + " crypten.save_from_party(test_features, filenames[\"test_features\"], src=BOB)\n", + " crypten.save_from_party(test_labels, filenames[\"test_labels\"], src=BOB)\n", " \n", "save_all_data()" ] @@ -187,67 +187,108 @@ "\n", "Our first example will focus on the Data Labeling scenario. In this example, Alice has access to features, while Bob has access to the labels. We will train our linear svm by encrypting the features from Alice and the labels from Bob, then training our SVM using an aggregation of the encrypted data.\n", "\n", - "In order to indicate the source of a given encrypted tensor, we encrypt our tensor using `crypten.load()` (from a file) or `crypten.cryptensor()` (from a tensor) using a keyword argument `src`. This `src` argument takes the rank of the party we want to encrypt from (recall that ALICE is 0 and BOB is 1). \n", + "In order to indicate the source of a given encrypted tensor, we encrypt our tensor using `crypten.load_from_party()` or `crypten.cryptensor()` (from a tensor) using a keyword argument `src`. This `src` argument takes the rank of the party we want to encrypt from (recall that ALICE is 0 and BOB is 1). \n", "\n", "(If the `src` is not specified, it will default to the rank 0 party. We will use the default when encrypting public values since the source is irrelevant in this case.)" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:root:==================\n", + "INFO:root:DistributedCommunicator with rank 0\n", + "INFO:root:==================\n", + "INFO:root:==================\n", + "INFO:root:DistributedCommunicator with rank 1\n", + "INFO:root:==================\n", + "INFO:root:World size = 2\n", + "INFO:root:World size = 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4040\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:root:Epoch 0 --- Training Accuracy 53.40%\n", + "INFO:root:Epoch 1 --- Training Accuracy 58.70%\n", + "INFO:root:Epoch 2 --- Training Accuracy 63.80%\n", + "INFO:root:Epoch 3 --- Training Accuracy 68.30%\n", + "INFO:root:Epoch 4 --- Training Accuracy 73.60%\n", + "INFO:root:Epoch 5 --- Training Accuracy 78.00%\n", + "INFO:root:Epoch 6 --- Training Accuracy 81.00%\n", + "INFO:root:Epoch 7 --- Training Accuracy 84.60%\n", + "INFO:root:Epoch 8 --- Training Accuracy 87.00%\n", + "INFO:root:Epoch 9 --- Training Accuracy 90.40%\n", + "INFO:root:Epoch 10 --- Training Accuracy 91.50%\n", + "INFO:root:Epoch 11 --- Training Accuracy 92.90%\n", + "INFO:root:Epoch 12 --- Training Accuracy 93.80%\n", + "INFO:root:Epoch 13 --- Training Accuracy 94.30%\n", + "INFO:root:Epoch 14 --- Training Accuracy 95.50%\n", + "INFO:root:Epoch 15 --- Training Accuracy 95.80%\n", + "INFO:root:Epoch 16 --- Training Accuracy 96.30%\n", + "INFO:root:Epoch 17 --- Training Accuracy 96.60%\n", + "INFO:root:Epoch 18 --- Training Accuracy 96.80%\n", + "INFO:root:Epoch 19 --- Training Accuracy 97.60%\n", + "INFO:root:Epoch 20 --- Training Accuracy 97.70%\n", + "INFO:root:Epoch 21 --- Training Accuracy 97.90%\n", + "INFO:root:Epoch 22 --- Training Accuracy 98.20%\n", + "INFO:root:Epoch 23 --- Training Accuracy 98.10%\n", + "INFO:root:Epoch 24 --- Training Accuracy 98.90%\n", + "INFO:root:Epoch 25 --- Training Accuracy 99.20%\n", + "INFO:root:Epoch 26 --- Training Accuracy 99.20%\n", + "INFO:root:Epoch 27 --- Training Accuracy 99.50%\n", + "INFO:root:Epoch 28 --- Training Accuracy 99.80%\n", + "INFO:root:Epoch 29 --- Training Accuracy 99.60%\n", + "INFO:root:Epoch 30 --- Training Accuracy 99.60%\n", + "INFO:root:Epoch 31 --- Training Accuracy 99.50%\n", + "INFO:root:Epoch 32 --- Training Accuracy 99.90%\n", + "INFO:root:Epoch 33 --- Training Accuracy 99.90%\n", + "INFO:root:Epoch 34 --- Training Accuracy 100.00%\n", + "INFO:root:Epoch 35 --- Training Accuracy 100.00%\n", + "INFO:root:Epoch 36 --- Training Accuracy 100.00%\n", + "INFO:root:Epoch 37 --- Training Accuracy 100.00%\n", + "INFO:root:Epoch 38 --- Training Accuracy 100.00%\n", + "INFO:root:Epoch 39 --- Training Accuracy 100.00%\n" + ] + }, { "name": "stdout", "output_type": "stream", "text": [ - "Epoch 0 --- Training Accuracy 53.40%\n", - "Epoch 1 --- Training Accuracy 58.70%\n", - "Epoch 2 --- Training Accuracy 63.80%\n", - "Epoch 3 --- Training Accuracy 68.30%\n", - "Epoch 4 --- Training Accuracy 73.60%\n", - "Epoch 6 --- Training Accuracy 81.00%\n", - "Epoch 7 --- Training Accuracy 84.60%\n", - "Epoch 8 --- Training Accuracy 87.00%\n", - "Epoch 10 --- Training Accuracy 91.50%\n", - "Epoch 11 --- Training Accuracy 92.90%\n", - "Epoch 12 --- Training Accuracy 93.80%\n", - "Epoch 13 --- Training Accuracy 94.30%\n", - "Epoch 14 --- Training Accuracy 95.50%\n", - "Epoch 16 --- Training Accuracy 96.30%\n", - "Epoch 17 --- Training Accuracy 96.60%\n", - "Epoch 18 --- Training Accuracy 96.80%\n", - "Epoch 19 --- Training Accuracy 97.60%\n", - "Epoch 20 --- Training Accuracy 97.70%\n", - "Epoch 21 --- Training Accuracy 97.90%\n", - "Epoch 22 --- Training Accuracy 98.20%\n", - "Epoch 23 --- Training Accuracy 98.10%\n", - "Epoch 24 --- Training Accuracy 98.90%\n", - "Epoch 25 --- Training Accuracy 99.20%\n", - "Epoch 26 --- Training Accuracy 99.20%\n", - "Epoch 27 --- Training Accuracy 99.50%\n", - "Epoch 28 --- Training Accuracy 99.80%\n", - "Epoch 29 --- Training Accuracy 99.60%\n", - "Epoch 30 --- Training Accuracy 99.60%\n", - "Epoch 31 --- Training Accuracy 99.50%\n", - "Epoch 32 --- Training Accuracy 99.90%\n", - "Epoch 33 --- Training Accuracy 99.90%\n", - "Epoch 34 --- Training Accuracy 100.00%\n", - "Epoch 35 --- Training Accuracy 100.00%\n", - "Epoch 36 --- Training Accuracy 100.00%\n", - "Epoch 37 --- Training Accuracy 100.00%\n", - "Epoch 38 --- Training Accuracy 100.00%\n", - "Epoch 39 --- Training Accuracy 100.00%\n", "Test accuracy 92.00%\n" ] }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:root:==================\n", + "INFO:root:DistributedCommunicator with rank 0\n", + "INFO:root:==================\n", + "INFO:root:World size = 1\n" + ] + }, { "data": { "text/plain": [ "[None, None]" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -255,12 +296,16 @@ "source": [ "from crypten import mpc\n", "\n", + "# If you want to check how the training accuracy behaves\n", + "# you can uncomment the next line\n", + "# crypten.debug.configure_logging()\n", + "\n", "@mpc.run_multiprocess(world_size=2)\n", "def data_labeling_example():\n", " \"\"\"Apply data labeling access control model\"\"\"\n", " # Alice loads features, Bob loads labels\n", - " features_enc = crypten.load(filenames[\"features\"], src=ALICE)\n", - " labels_enc = crypten.load(filenames[\"labels\"], src=BOB)\n", + " features_enc = crypten.load_from_party(filenames[\"features\"], src=ALICE)\n", + " labels_enc = crypten.load_from_party(filenames[\"labels\"], src=BOB)\n", " \n", " # Execute training\n", " w, b = train_linear_svm(features_enc, labels_enc, epochs=epochs, lr=lr)\n", @@ -284,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -300,35 +345,38 @@ "Epoch 6 --- Training Accuracy 81.00%\n", "Epoch 7 --- Training Accuracy 84.60%\n", "Epoch 8 --- Training Accuracy 87.00%\n", + "Epoch 9 --- Training Accuracy 90.40%\n", "Epoch 10 --- Training Accuracy 91.50%\n", "Epoch 11 --- Training Accuracy 92.90%\n", "Epoch 12 --- Training Accuracy 93.80%\n", "Epoch 13 --- Training Accuracy 94.30%\n", "Epoch 14 --- Training Accuracy 95.50%\n", + "Epoch 15 --- Training Accuracy 95.80%\n", "Epoch 16 --- Training Accuracy 96.30%\n", "Epoch 17 --- Training Accuracy 96.60%\n", - "Epoch 18 --- Training Accuracy 96.70%\n", - "Epoch 19 --- Training Accuracy 97.40%\n", - "Epoch 20 --- Training Accuracy 98.30%\n", - "Epoch 21 --- Training Accuracy 98.00%\n", - "Epoch 22 --- Training Accuracy 98.10%\n", - "Epoch 23 --- Training Accuracy 98.00%\n", - "Epoch 24 --- Training Accuracy 98.70%\n", - "Epoch 25 --- Training Accuracy 98.70%\n", - "Epoch 26 --- Training Accuracy 99.30%\n", - "Epoch 27 --- Training Accuracy 99.70%\n", - "Epoch 28 --- Training Accuracy 99.60%\n", - "Epoch 29 --- Training Accuracy 99.50%\n", - "Epoch 30 --- Training Accuracy 99.70%\n", + "Epoch 18 --- Training Accuracy 96.80%\n", + "Epoch 19 --- Training Accuracy 97.60%\n", + "Epoch 20 --- Training Accuracy 97.70%\n", + "Epoch 21 --- Training Accuracy 97.90%\n", + "Epoch 22 --- Training Accuracy 98.20%\n", + "Epoch 23 --- Training Accuracy 98.10%\n", + "Epoch 24 --- Training Accuracy 98.90%\n", + "Epoch 25 --- Training Accuracy 99.20%\n", + "Epoch 26 --- Training Accuracy 99.20%\n", + "Epoch 27 --- Training Accuracy 99.50%\n", + "Epoch 28 --- Training Accuracy 99.80%\n", + "Epoch 29 --- Training Accuracy 99.60%\n", + "Epoch 30 --- Training Accuracy 99.60%\n", "Epoch 31 --- Training Accuracy 99.50%\n", - "Epoch 32 --- Training Accuracy 99.60%\n", + "Epoch 32 --- Training Accuracy 99.90%\n", "Epoch 33 --- Training Accuracy 99.90%\n", "Epoch 34 --- Training Accuracy 100.00%\n", "Epoch 35 --- Training Accuracy 100.00%\n", "Epoch 36 --- Training Accuracy 100.00%\n", "Epoch 37 --- Training Accuracy 100.00%\n", "Epoch 38 --- Training Accuracy 100.00%\n", - "Epoch 39 --- Training Accuracy 100.00%\n" + "Epoch 39 --- Training Accuracy 100.00%\n", + "Test accuracy 92.00%\n" ] }, { @@ -337,7 +385,7 @@ "[None, None]" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -347,8 +395,8 @@ "def feature_aggregation_example():\n", " \"\"\"Apply feature aggregation access control model\"\"\"\n", " # Alice loads some features, Bob loads other features\n", - " features_alice_enc = crypten.load(filenames[\"features_alice\"], src=ALICE)\n", - " features_bob_enc = crypten.load(filenames[\"features_bob\"], src=BOB)\n", + " features_alice_enc = crypten.load_from_party(filenames[\"features_alice\"], src=ALICE)\n", + " features_bob_enc = crypten.load_from_party(filenames[\"features_bob\"], src=BOB)\n", " \n", " # Concatenate features\n", " features_enc = crypten.cat([features_alice_enc, features_bob_enc], dim=0)\n", @@ -373,12 +421,12 @@ "\n", "The next example shows how we can use CrypTen in a Data Augmentation scenario. Here Alice and Bob each have 500 samples, and would like to learn a classifier over their combined sample data. This scenario can occur in applications where several parties may each have access to a small amount of sensitive data, where no individual party has enough data to train an accurate model.\n", "\n", - "Like the last scenario, one of our variables is split amongst parties, so we will have to concatenate tensors from encrypted from different parties. The main difference from the last scenario is that we are concatenating over the other dimension (the sample dimension rather than the feature dimension)." + "Like the last scenario, one of our variables is split amongst parties, so we will have to concatenate encrypted tensors from different parties. The main difference from the last scenario is that we are concatenating over the other dimension (the sample dimension rather than the feature dimension)." ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -389,6 +437,7 @@ "Epoch 1 --- Training Accuracy 58.70%\n", "Epoch 2 --- Training Accuracy 63.80%\n", "Epoch 3 --- Training Accuracy 68.30%\n", + "Epoch 4 --- Training Accuracy 73.60%\n", "Epoch 5 --- Training Accuracy 78.00%\n", "Epoch 6 --- Training Accuracy 81.00%\n", "Epoch 7 --- Training Accuracy 84.60%\n", @@ -402,6 +451,7 @@ "Epoch 15 --- Training Accuracy 96.30%\n", "Epoch 16 --- Training Accuracy 96.20%\n", "Epoch 17 --- Training Accuracy 96.80%\n", + "Epoch 18 --- Training Accuracy 96.80%\n", "Epoch 19 --- Training Accuracy 97.20%\n", "Epoch 20 --- Training Accuracy 97.90%\n", "Epoch 21 --- Training Accuracy 97.80%\n", @@ -409,17 +459,18 @@ "Epoch 23 --- Training Accuracy 98.90%\n", "Epoch 24 --- Training Accuracy 99.20%\n", "Epoch 25 --- Training Accuracy 99.40%\n", - "Epoch 26 --- Training Accuracy 99.40%\n", - "Epoch 27 --- Training Accuracy 99.60%\n", - "Epoch 28 --- Training Accuracy 99.00%\n", + "Epoch 26 --- Training Accuracy 99.20%\n", + "Epoch 27 --- Training Accuracy 99.50%\n", + "Epoch 28 --- Training Accuracy 99.30%\n", "Epoch 29 --- Training Accuracy 99.30%\n", - "Epoch 30 --- Training Accuracy 99.30%\n", - "Epoch 31 --- Training Accuracy 99.40%\n", - "Epoch 32 --- Training Accuracy 99.50%\n", - "Epoch 33 --- Training Accuracy 99.90%\n", + "Epoch 30 --- Training Accuracy 99.50%\n", + "Epoch 31 --- Training Accuracy 99.50%\n", + "Epoch 32 --- Training Accuracy 99.60%\n", + "Epoch 33 --- Training Accuracy 99.80%\n", "Epoch 34 --- Training Accuracy 99.70%\n", - "Epoch 35 --- Training Accuracy 99.70%\n", - "Epoch 36 --- Training Accuracy 99.90%\n", + "Epoch 35 --- Training Accuracy 99.80%\n", + "Epoch 36 --- Training Accuracy 100.00%\n", + "Epoch 37 --- Training Accuracy 100.00%\n", "Epoch 38 --- Training Accuracy 100.00%\n", "Epoch 39 --- Training Accuracy 100.00%\n", "Test accuracy 92.00%\n" @@ -431,7 +482,7 @@ "[None, None]" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -441,10 +492,10 @@ "def dataset_augmentation_example():\n", " \"\"\"Apply dataset augmentation access control model\"\"\" \n", " # Alice loads some samples, Bob loads other samples\n", - " samples_alice_enc = crypten.load(filenames[\"samples_alice\"], src=ALICE)\n", - " samples_bob_enc = crypten.load(filenames[\"samples_bob\"], src=BOB)\n", + " samples_alice_enc = crypten.load_from_party(filenames[\"samples_alice\"], src=ALICE)\n", + " samples_bob_enc = crypten.load_from_party(filenames[\"samples_bob\"], src=BOB)\n", " \n", - " # Concatenate features\n", + " # Concatenate samples\n", " samples_enc = crypten.cat([samples_alice_enc, samples_bob_enc], dim=1)\n", " \n", " labels_enc = crypten.cryptensor(labels)\n", @@ -473,37 +524,19 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Test accuracy 100.00%\n" - ] - }, - { - "data": { - "text/plain": [ - "[None, None]" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "@mpc.run_multiprocess(world_size=2)\n", "def model_hiding_example():\n", " \"\"\"Apply model hiding access control model\"\"\"\n", " # Alice loads the model\n", - " w_true_enc = crypten.load(filenames[\"w_true\"], src=ALICE)\n", - " b_true_enc = crypten.load(filenames[\"b_true\"], src=ALICE)\n", + " w_true_enc = crypten.load_from_party(filenames[\"w_true\"], src=ALICE)\n", + " b_true_enc = crypten.load_from_party(filenames[\"b_true\"], src=ALICE)\n", " \n", " # Bob loads the features to be evaluated\n", - " test_features_enc = crypten.load(filenames[\"test_features\"], src=BOB)\n", + " test_features_enc = crypten.load_from_party(filenames[\"test_features\"], src=BOB)\n", " \n", " # Evaluate model\n", " evaluate_linear_svm(test_features_enc, test_labels, w_true_enc, b_true_enc)\n", @@ -524,7 +557,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -551,7 +584,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.8.6" } }, "nbformat": 4,