Esempio n. 1
0
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):
Esempio n. 2
0
                                        strides=(2, 2),
                                        padding='VALID',
                                        channels_first=False)

avg_pool1 = tfe.layers.AveragePooling2D(input_shape=[-1, 8, 8, 16],
                                        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_pool1.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')

    for _ in range(5):
        print("Predicting")
Esempio n. 3
0
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_updater(), tag='training')

    for _ in range(5):
        print("Predicting")
Esempio n. 4
0
# 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')

    for _ in range(5):
        print("Predicting")
Esempio n. 5
0
import tensorflow as tf
import tf_encrypted as tfe

def provide_input():
    return tf.ones(shape=(5,10))

w = tfe.define_private_variable(tf.ones(shape=(10,10)))
x = tfe.define_private_input('input-provider', provide_input)

y = tfe.matmul(x, w)

with tfe.Session() as sess:
    sess.run(tfe.global_variables_initializer())
    result = sess.run(y.reveal())
    print(result)
Esempio n. 6
0
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, ModelTrainer.STRIDE, 'VALID')
pool = lambda x: tfe.avgpool2d(x, (2, 2), (2, 2), 'VALID')

# compute prediction
Wconv1, bconv1, Wconv2, bconv2, Wfc1, bfc1, Wfc2, bfc2, Wfc3, bfc3 = 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
layer4 = tfe.matmul(layer3, Wfc2) + bfc2
logits = tfe.matmul(layer4, Wfc3) + bfc3

# 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')

    for _ in range(5):