-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreinforce_tf.py
More file actions
143 lines (87 loc) · 3.17 KB
/
reinforce_tf.py
File metadata and controls
143 lines (87 loc) · 3.17 KB
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
134
135
136
137
138
139
140
141
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import gym
dense = tf.layers.dense
class Policy:
def __init__(self):
with tf.variable_scope('Perception'):
self.state = tf.placeholder(tf.float32, shape = [None,4], name = 'State')
with tf.variable_scope('Network'):
l1 = dense(self.state, 64, activation = tf.nn.relu, name = 'first_layer')
self.head = dense(l1, 2, activation = tf.nn.softmax, name = 'head')
with tf.variable_scope('Training'):
self.observed_rewards = tf.placeholder(tf.float32, shape = [None, 1], name = 'rewards')
self.selected_actions = tf.placeholder(tf.int32, shape = [None, 1], name = 'action_indices')
size = tf.shape(self.observed_rewards)[0]
indices = tf.reshape(tf.range(size), (-1,1))
indices = tf.concat([indices, self.selected_actions],1 )
variables = tf.trainable_variables()
self.gradient_holders = []
for idx,var in enumerate(variables):
placeholder = tf.placeholder(tf.float32, name=str(idx)+'_holder')
self.gradient_holders.append(placeholder)
self.log_probs = tf.log(tf.gather_nd(self.head, indices))
self.loss = tf.reduce_mean(self.log_probs*self.observed_rewards)
self.gradients = tf.gradients(self.loss,variables)
self.update = tf.train.AdamOptimizer(5e-3).apply_gradients(zip(self.gradient_holders, variables))
def discount(r):
result, current = [],0.
for i in reversed(range(len(r))):
current = current*0.95 + r[i]
result.insert(0,current)
return np.array(result).reshape(-1,1)
import gym
import time
env = gym.make('CartPole-v0')
agent = Policy()
epochs = 700
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
grad_buffer = sess.run(tf.trainable_variables())
for i,g in enumerate(grad_buffer):
grad_buffer[i] *= 0.
mean_r = 0.
for epoch in range(epochs):
s = env.reset()
reward = 0
done = False
states = []
rewards = []
actions = []
while not done:
distrib = sess.run(agent.head, feed_dict = {agent.state:s.reshape(1,-1)}).reshape(-1)
action = np.random.choice(2, p = distrib)
actions.append(action)
states.append(s)
ns, r, done,_ = env.step(action)
rewards.append(r)
reward += r
s = ns
if done:
mean_r += reward
discounted = discount(rewards)
actions = np.array(actions).reshape(-1,1).astype(int)
states = np.stack(states)
grads = sess.run(agent.gradients, feed_dict = {agent.state: states,
agent.observed_rewards: discounted,
agent.selected_actions: actions})
for i,g in enumerate(grads):
grad_buffer[i] += g
if epoch % 5 == 0:
dico = dict(zip(agent.gradient_holders, grad_buffer))
sess.run(agent.update, feed_dict = dico)
for i, g in enumerate(grad_buffer):
grad_buffer[i] = g*0.
if epoch%100 == 0:
print('Epoch: {} - Reward: {}'.format(epoch, mean_r/100.))
mean_r = 0.
for epoch in range(100):
s = env.reset()
done = False
while not done:
distrib = sess.run(agent.head, feed_dict = {agent.state:s.reshape(1,-1)}).reshape(-1)
action = np.random.choice(2, p = distrib)
env.render()
time.sleep(0.02)
s, r, done, _ = env.step(action)