-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_file.py
54 lines (40 loc) · 961 Bytes
/
test_file.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
import tensorflow as tf
tf.enable_eager_execution()
w = tf.Variable(2.0)
tape = tf.GradientTape(watch_accessed_variables=False, persistent=True)
def nn(x):
global w
return w * x[0] + w + x[1]
def compute_loss(y):
return y
def add_message(message):
global msgs
msgs = msgs[-1:] + msgs[:-1]
msgs[0] = [message]
msgs = [tf.Variable(tf.zeros((1,))) for _ in range(2)]
loss = 0
with tape:
tape.watch(w)
y = nn(tf.concat(msgs, 0))
loss += compute_loss(y)
n_m = y
print(tape.gradient(loss, w))
add_message(n_m)
with tape:
y = nn(tf.concat(msgs, 0))
loss += compute_loss(y)
n_m = y
print(tape.gradient(loss, w))
add_message(n_m)
with tape:
y = nn(tf.concat(msgs, 0))
loss += compute_loss(y)
n_m = y
print(tape.gradient(loss, w))
add_message(n_m)
with tape:
y = nn(tf.concat(msgs, 0))
loss += compute_loss(y)
n_m = y
print(tape.gradient(loss, w))
add_message(n_m)