forked from Chenkehan21/reinforcement-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimator_test.py
133 lines (105 loc) · 3.78 KB
/
estimator_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import unittest
import gym
import sys
import os
import numpy as np
import tensorflow as tf
from inspect import getsourcefile
current_path = os.path.dirname(os.path.abspath(getsourcefile(lambda:0)))
import_path = os.path.abspath(os.path.join(current_path, "../.."))
if import_path not in sys.path:
sys.path.append(import_path)
# from lib import plotting
from lib.atari.state_processor import StateProcessor
from lib.atari import helpers as atari_helpers
from estimators import ValueEstimator, PolicyEstimator
def make_env():
return gym.envs.make("Breakout-v0")
VALID_ACTIONS = [0, 1, 2, 3]
class PolicyEstimatorTest(tf.test.TestCase):
def testPredict(self):
env = make_env()
sp = StateProcessor()
estimator = PolicyEstimator(len(VALID_ACTIONS))
with self.test_session() as sess:
sess.run(tf.initialize_all_variables())
# Generate a state
state = sp.process(env.reset())
processed_state = atari_helpers.atari_make_initial_state(state)
processed_states = np.array([processed_state])
# Run feeds
feed_dict = {
estimator.states: processed_states,
estimator.targets: [1.0],
estimator.actions: [1]
}
loss = sess.run(estimator.loss, feed_dict)
pred = sess.run(estimator.predictions, feed_dict)
# Assertions
self.assertTrue(loss != 0.0)
self.assertEqual(pred["probs"].shape, (1, len(VALID_ACTIONS)))
self.assertEqual(pred["logits"].shape, (1, len(VALID_ACTIONS)))
def testGradient(self):
env = make_env()
sp = StateProcessor()
estimator = PolicyEstimator(len(VALID_ACTIONS))
grads = [g for g, _ in estimator.grads_and_vars]
with self.test_session() as sess:
sess.run(tf.initialize_all_variables())
# Generate a state
state = sp.process(env.reset())
processed_state = atari_helpers.atari_make_initial_state(state)
processed_states = np.array([processed_state])
# Run feeds to get gradients
feed_dict = {
estimator.states: processed_states,
estimator.targets: [1.0],
estimator.actions: [1]
}
grads_ = sess.run(grads, feed_dict)
# Apply calculated gradients
grad_feed_dict = { k: v for k, v in zip(grads, grads_) }
_ = sess.run(estimator.train_op, grad_feed_dict)
class ValueEstimatorTest(tf.test.TestCase):
def testPredict(self):
env = make_env()
sp = StateProcessor()
estimator = ValueEstimator()
with self.test_session() as sess:
sess.run(tf.initialize_all_variables())
# Generate a state
state = sp.process(env.reset())
processed_state = atari_helpers.atari_make_initial_state(state)
processed_states = np.array([processed_state])
# Run feeds
feed_dict = {
estimator.states: processed_states,
estimator.targets: [1.0],
}
loss = sess.run(estimator.loss, feed_dict)
pred = sess.run(estimator.predictions, feed_dict)
# Assertions
self.assertTrue(loss != 0.0)
self.assertEqual(pred["logits"].shape, (1,))
def testGradient(self):
env = make_env()
sp = StateProcessor()
estimator = ValueEstimator()
grads = [g for g, _ in estimator.grads_and_vars]
with self.test_session() as sess:
sess.run(tf.initialize_all_variables())
# Generate a state
state = sp.process(env.reset())
processed_state = atari_helpers.atari_make_initial_state(state)
processed_states = np.array([processed_state])
# Run feeds
feed_dict = {
estimator.states: processed_states,
estimator.targets: [1.0],
}
grads_ = sess.run(grads, feed_dict)
# Apply calculated gradients
grad_feed_dict = { k: v for k, v in zip(grads, grads_) }
_ = sess.run(estimator.train_op, grad_feed_dict)
if __name__ == '__main__':
unittest.main()