forked from miyosuda/async_deep_reinforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accum_trainer_test.py
89 lines (58 loc) · 2.35 KB
/
accum_trainer_test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
import accum_trainer
class AccumTrainerTest(tf.test.TestCase):
def testAccum(self):
with self.test_session():
var0 = tf.Variable([1.0, 2.0])
trainer = accum_trainer.AccumTrainer()
cost = tf.square(var0)
trainer.prepare_minimize(cost, [var0])
accmulate_grad = trainer.accumulate_gradients()
reset = trainer.reset_gradients()
tf.initialize_all_variables().run()
# gradの加算を実行
accmulate_grad.run()
# accmulate_gradしても、var0の中身は変わらない
self.assertAllClose([1.0, 2.0], var0.eval())
accum_grads = trainer._accum_grad_list
accum_grad0 = accum_grads[0]
# gradがaccum_gradへ加算されているのを確認
self.assertAllClose([2.0, 4.0], accum_grad0.eval())
# gradの加算を再度実行
accmulate_grad.run()
# gradがaccum_gradへさらに加算されているのを確認
self.assertAllClose([4.0, 8.0], accum_grad0.eval())
# resetを実行
reset.run()
# accum_gradがゼロになっているのを確認
self.assertAllClose([0.0, 0.0], accum_grad0.eval())
<<<<<<< HEAD
def testBatchAccum(self):
with self.test_session():
x = tf.placeholder("float", shape=(None,1))
c = tf.constant( [1.0] )
var0 = tf.Variable( c )
mul = var0 * x
trainer = accum_trainer.AccumTrainer()
#cost = tf.square(mul)
cost = tf.reduce_sum( tf.square(mul) )
print(cost.get_shape())
trainer.prepare_minimize(cost, [var0])
accmulate_grad = trainer.accumulate_gradients()
reset = trainer.reset_gradients()
tf.initialize_all_variables().run()
si = [ [1.0], [2.0] ]
# gradの加算を実行
accmulate_grad.run( feed_dict = { x: si } )
# accmulate_gradしても、var0の中身は変わらない
self.assertAllClose([1.0], var0.eval())
accum_grads = trainer._accum_grad_list
accum_grad0 = accum_grads[0]
# gradがaccum_gradへbatchで加算されているのを確認
t = 2 * 1*1 * 1 + 2 * 2*2 * 1
self.assertAllClose([t], accum_grad0.eval())
# TODO: gradient clipping test
if __name__ == "__main__":
tf.test.main()