forked from tf-encrypted/tf-encrypted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputs.py
62 lines (42 loc) · 1.36 KB
/
inputs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import numpy as np
import tensorflow as tf
import tf_encrypted as tfe
config = tfe.get_config()
if len(sys.argv) > 1:
#
# assume we're running as a server
#
player_name = str(sys.argv[1])
server = config.server(player_name)
server.start()
server.join()
else:
#
# assume we're running as master
#
def provide_weights() -> tf.Tensor:
raw_w = np.array([5, 5, 5, 5]).reshape((2, 2))
w = tf.constant(raw_w)
tf.print(w, [w])
return w
def provide_input() -> tf.Tensor:
x = tf.constant([1, 2, 3, 4], shape=(2, 2), dtype=tf.float32)
tf.print(x, [x])
return x
def receive_output(prediction):
tf.print([], [prediction], summarize=4)
return []
with tfe.protocol.Pond() as prot:
# treat weights as private
w = prot.define_private_input("model-provider", provide_weights)
# load input for prediction
x = prot.define_private_input("input-provider", provide_input)
# compute prediction
y = prot.matmul(x, w)
# send output
prediction_op = prot.define_output("input-provider", y, receive_output)
with tfe.Session() as sess:
sess.run(tf.global_variables_initializer(), tag="init")
for _ in range(5):
sess.run(prediction_op, tag="prediction")