def forward(self, x): """ :param ~tf_encrypted.protocol.pond.PondTensor x: The input tensor :rtype: ~tf_encrypted.protocol.pond.PondTensor :returns: A pond tensor with the same backing type as the input tensor. """ y = tfe.relu(x) self.layer_output = y return y
params = tfe.define_private_input('model-trainer', model_trainer.provide_input, masked=True) # pylint: disable=E0632 # we'll use the same parameters for each prediction so we cache them to avoid re-training each time params = tfe.cache(params) # get prediction input from client x, y = tfe.define_private_input('prediction-client', prediction_client.provide_input, masked=True) # pylint: disable=E0632 # helpers conv = lambda x, w, s: tfe.conv2d(x, w, s, 'VALID') pool = lambda x: tfe.avgpool2d(x, (2, 2), (2, 2), 'VALID') # compute prediction Wconv1, bconv1, Wfc1, bfc1, Wfc2, bfc2 = params bconv1 = tfe.reshape(bconv1, [-1, 1, 1]) layer1 = pool(tfe.relu(conv(x, Wconv1, ModelTrainer.STRIDE) + bconv1)) layer1 = tfe.reshape(layer1, [-1, ModelTrainer.HIDDEN_FC1]) layer2 = tfe.matmul(layer1, Wfc1) + bfc1 logits = tfe.matmul(layer2, Wfc2) + bfc2 # send prediction output back to client prediction_op = tfe.define_output('prediction-client', [logits, y], prediction_client.receive_output) with tfe.Session() as sess: print("Init") sess.run(tf.global_variables_initializer(), tag='init') print("Training") sess.run(tfe.global_caches_updater(), tag='training')
params = tfe.cache(params) # get prediction input from client x, y = tfe.define_private_input('prediction-client', prediction_client.provide_input, masked=True) # pylint: disable=E0632 # helpers conv = lambda x, w: tfe.conv2d(x, w, 1, 'VALID') pool = lambda x: tfe.avgpool2d(x, (2, 2), (2, 2), 'VALID') # compute prediction Wconv1, bconv1, Wconv2, bconv2, Wfc1, bfc1, Wfc2, bfc2 = params bconv1 = tfe.reshape(bconv1, [-1, 1, 1]) bconv2 = tfe.reshape(bconv2, [-1, 1, 1]) layer1 = pool(tfe.relu(conv(x, Wconv1) + bconv1)) layer2 = pool(tfe.relu(conv(layer1, Wconv2) + bconv2)) layer2 = tfe.reshape(layer2, [-1, ModelTrainer.HIDDEN_FC1]) layer3 = tfe.matmul(layer2, Wfc1) + bfc1 logits = tfe.matmul(layer3, Wfc2) + bfc2 # send prediction output back to client prediction_op = tfe.define_output('prediction-client', [logits, y], prediction_client.receive_output) with tfe.Session() as sess: print("Init") sess.run(tf.global_variables_initializer(), tag='init') print("Training") sess.run(tfe.global_caches_updator(), tag='training')
params = tfe.define_private_input('model-trainer', model_trainer.provide_input, masked=True) # pylint: disable=E0632 # we'll use the same parameters for each prediction so we cache them to avoid re-training each time params = tfe.cache(params) # get prediction input from client x, y = tfe.define_private_input('prediction-client', prediction_client.provide_input, masked=True) # pylint: disable=E0632 # compute prediction w0, b0, w1, b1, w2, b2 = params layer0 = x layer1 = tfe.relu((tfe.matmul(layer0, w0) + b0)) layer2 = tfe.relu((tfe.matmul(layer1, w1) + b1)) logits = tfe.matmul(layer2, w2) + b2 # send prediction output back to client prediction_op = tfe.define_output('prediction-client', [logits, y], prediction_client.receive_output) with tfe.Session() as sess: print("Init") sess.run(tf.global_variables_initializer(), tag='init') print("Training") sess.run(tfe.global_caches_updater(), tag='training') for _ in range(5):
conv2_tfe.initialize(initial_weights=wconv2) avg_pool1 = tfe.layers.AveragePooling2D(input_shape=[-1, 24, 24, 20], pool_size=(2, 2), strides=(2, 2), padding='VALID', channels_first=False) avg_pool2 = tfe.layers.AveragePooling2D(input_shape=[-1, 8, 8, 50], pool_size=(2, 2), strides=(2, 2), padding='VALID', channels_first=False) x = tfe.reshape(x, [-1, 28, 28, 1]) layer1 = avg_pool1.forward(tfe.relu(conv1_tfe.forward(x) + bconv1)) layer2 = avg_pool2.forward(tfe.relu(conv2_tfe.forward(layer1) + bconv2)) layer2 = tfe.reshape(layer2, [-1, ModelTrainer.HIDDEN_FC1]) layer3 = tfe.relu(tfe.matmul(layer2, wfc1) + bfc1) logits = tfe.matmul(layer3, wfc2) + bfc2 # send prediction output back to client prediction_op = tfe.define_output('prediction-client', [logits, y], prediction_client.receive_output) with tfe.Session() as sess: print("Init") sess.run(tf.global_variables_initializer(), tag='init') print("Training") sess.run(cache_updater, tag='training')
def relu(x): """Computes relu of x element-wise""" return tfe.relu(x)