This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| import tf_encrypted as tfe | |
| # define the parties | |
| tfe.set_config(tfe.RemoteConfig({ | |
| "model-owner": "localhost:2222", | |
| "prediction-client": "1.2.3.4:2222", | |
| "server0": "1.1.1.1:4444", | |
| "server1": "1.1.1.2:4444"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| # generic functions for loading model weights and input data | |
| def provide_weights(): """Load model weights as TensorFlow objects.""" | |
| def provide_input(): """Load input data as TensorFlow objects.""" | |
| # get model weights/input data (both unencrypted) | |
| w0, b0, w1, b1, w2, b2 = provide_weights() | |
| x = provide_input() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| import tf_encrypted as tfe | |
| # generic remote procedure calls | |
| def provide_weights(): """Load model weights.""" | |
| def provide_input(): """Load input data.""" | |
| def receive_output(): """Receive and decrypt output.""" | |
| # get model weights/input data | |
| # as private tensors from each party |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| # generic function stubs | |
| def provide_weights(): """Load model weights.""" | |
| def provide_input(): """Load input data.""" | |
| # get model weights/input data | |
| # (both unencrypted) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def matmul(prot: Pond, | |
| x: PondMaskedTensor, | |
| y: PondMaskedTensor): | |
| a, a0, a1, alpha_on_0, alpha_on_1 = x.unwrapped | |
| b, b0, b1, beta_on_0, beta_on_1 = y.unwrapped | |
| with tf.device(prot.crypto_producer.device_name): | |
| ab = a.matmul(b) | |
| ab0, ab1 = prot._share(ab) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| import syft | |
| hook = syft.TensorFlowHook(tf) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| alice = syft.VirtualWorker(hook, “alice”) | |
| x = tf.constant([1., 2., 3., 4.]) | |
| x_ptr = x.send(alice) | |
| print(x_ptr) | |
| # ==> (Wrapper)>[PointerTensor | me:random_id1 -> alice:random_id2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| y_ptr = x_ptr + x_ptr | |
| y = tf.reshape(y_ptr, shape=[2, 2]) | |
| id = tf.constant([[1., 0.], [0., 1.]]).send(alice) | |
| z = tf.matmul(y, id).get() | |
| print(z) | |
| # ==> tf.Tensor([[2. 4.] | |
| # [6. 8.]], shape=(2, 2), dtype=float32) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x = tf.expand_dims(id[0], 0) | |
| # Initialize the weight | |
| w_init = tf.initializers.glorot_normal() | |
| w = tf.Variable(w_init(shape=(2, 1), dtype=tf.float32)).send(alice) | |
| z = tf.matmul(x, w) | |
| # Manual differentiation & update | |
| dzdx = tf.transpose(x) | |
| w.assign_sub(dzdx) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mnist = tf.keras.datasets.mnist | |
| (x_train, y_train), (x_test, y_test) = mnist.load_data() | |
| x_train, x_test = x_train / 255.0, x_test / 255.0 | |
| # Converting the data from numpy to tf.Tensor in order to have PySyft functionalities. | |
| x_train, y_train = tf.convert_to_tensor(x_train), tf.convert_to_tensor(y_train) | |
| x_test, y_test = tf.convert_to_tensor(x_test), tf.convert_to_tensor(y_test) | |
| # Send data to Alice (for demonstration purposes) | |
| x_train_ptr = x_train.send(alice) |
OlderNewer