-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn_cartpole.py
230 lines (186 loc) · 6.5 KB
/
dqn_cartpole.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import sys
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from datetime import datetime
from cartpole import CartPoleBalancing
class HiddenLayer:
def __init__(self, M1, M2, f=tf.nn.tanh, use_bias=True):
self.W = tf.Variable(tf.random_normal(shape=(M1, M2)))
self.params = [self.W]
self.use_bias = use_bias
if use_bias:
self.b = tf.Variable(np.zeros(M2).astype(np.float32))
self.params.append(self.b)
self.f = f
def forward(self, X):
if self.use_bias:
a = tf.matmul(X, self.W) + self.b
else:
a = tf.matmul(X, self.W)
return self.f(a)
class DQN:
def __init__(self, D, K, hidden_layer_sizes, gamma, max_experiences=257, min_experiences=129, batch_sz=32):
self.K = K
# create the graph
self.layers = []
M1 = D
for M2 in hidden_layer_sizes:
layer = HiddenLayer(M1, M2)
self.layers.append(layer)
M1 = M2
# final layer
layer = HiddenLayer(M1, K, lambda x: x)
self.layers.append(layer)
# collect params for copy
self.params = []
for layer in self.layers:
self.params += layer.params
# inputs and targets
self.X = tf.placeholder(tf.float32, shape=(None, D), name='X')
self.G = tf.placeholder(tf.float32, shape=(None,), name='G')
self.actions = tf.placeholder(tf.int32, shape=(None,), name='actions')
# calculate output and cost
Z = self.X
for layer in self.layers:
Z = layer.forward(Z)
Y_hat = Z
self.predict_op = Y_hat
selected_action_values = tf.reduce_sum(
Y_hat * tf.one_hot(self.actions, K),
reduction_indices=[1]
)
cost = tf.reduce_sum(tf.square(self.G - selected_action_values))
self.train_op = tf.train.AdamOptimizer(1e-2).minimize(cost)
# self.train_op = tf.train.AdagradOptimizer(1e-2).minimize(cost)
# self.train_op = tf.train.MomentumOptimizer(1e-3, momentum=0.2).minimize(cost)
# self.train_op = tf.train.GradientDescentOptimizer(1e-4).minimize(cost)
# create replay memory
self.experience = {'s': [], 'a': [], 'r': [], 's2': [], 'done': []}
self.max_experiences = max_experiences
self.min_experiences = min_experiences
self.batch_sz = batch_sz
self.gamma = gamma
def set_session(self, session):
self.session = session
def copy_from(self, other):
# collect all the ops
ops = []
my_params = self.params
other_params = other.params
for p, q in zip(my_params, other_params):
actual = self.session.run(q)
op = p.assign(actual)
ops.append(op)
# now run them all
self.session.run(ops)
def predict(self, X):
X = np.atleast_2d(X)
return self.session.run(self.predict_op, feed_dict={self.X: X})
def train(self, target_network):
# sample a random batch from buffer, do an iteration of GD
if len(self.experience['s']) < self.min_experiences:
# don't do anything if we don't have enough experience
return
# randomly select a batch
idx = np.random.choice(len(self.experience['s']), size=self.batch_sz, replace=False)
# print("idx:", idx)
states = [self.experience['s'][i] for i in idx]
actions = [self.experience['a'][i] for i in idx]
rewards = [self.experience['r'][i] for i in idx]
next_states = [self.experience['s2'][i] for i in idx]
dones = [self.experience['done'][i] for i in idx]
next_Q = np.max(target_network.predict(next_states), axis=1)
targets = [r + self.gamma*next_q if not done else r for r, next_q, done in zip(rewards, next_Q, dones)]
# call optimizer
self.session.run(
self.train_op,
feed_dict={
self.X: states,
self.G: targets,
self.actions: actions
}
)
def add_experience(self, s, a, r, s2, done):
if len(self.experience['s']) >= self.max_experiences:
self.experience['s'].pop(0)
self.experience['a'].pop(0)
self.experience['r'].pop(0)
self.experience['s2'].pop(0)
self.experience['done'].pop(0)
self.experience['s'].append(s)
self.experience['a'].append(a)
self.experience['r'].append(r)
self.experience['s2'].append(s2)
self.experience['done'].append(done)
def sample_action(self, x, eps):
if np.random.random() < eps:
return np.random.choice(self.K)
else:
X = np.atleast_2d(x)
return np.argmax(self.predict(X)[0])
def play_one(env, model, tmodel, eps, gamma, copy_period, serial_num):
observation = env.reset()
done = False
totalreward = 0
iters = 0
sars2 = np.empty(shape=[0, 10])
while not done and iters < 200:
action = model.sample_action(observation, eps)
prev_observation = observation
observation, reward, done, info = env.step(action)
totalreward += reward
if done:
reward = -200
tmp_sars2 = [prev_observation[0], prev_observation[1], prev_observation[2], prev_observation[3], action, reward, observation[0], observation[1], observation[2], observation[3]]
sars2 = np.append(sars2, [tmp_sars2], axis=0)
# update the model
model.add_experience(prev_observation, action, reward, observation, done)
model.train(tmodel)
iters += 1
if iters % copy_period == 0:
tmodel.copy_from(model)
np.savetxt("./tmp_data/s_%d.csv" % serial_num, sars2, fmt="%10.5f", delimiter=",")
return totalreward
def plot_running_avg(totalrewards):
N = len(totalrewards)
running_avg = np.empty(N)
for t in range(N):
running_avg[t] = totalrewards[max(0, t-100):(t+1)].mean()
plt.plot(running_avg)
plt.title("Running Average")
plt.show()
def main():
env = CartPoleBalancing()
gamma = 0.99
copy_period = 50
D = 4
K = 2
sizes = [64,64]
model = DQN(D, K, sizes, gamma)
tmodel = DQN(D, K, sizes, gamma)
init = tf.global_variables_initializer()
session = tf.InteractiveSession()
session.run(init)
model.set_session(session)
tmodel.set_session(session)
N = 500
totalrewards = np.empty(N)
costs = np.empty(N)
for n in range(N):
eps = 1.0/np.sqrt(n+1)
# eps = 0.1
totalreward = play_one(env, model, tmodel, eps, gamma, copy_period, n)
totalrewards[n] = totalreward
if n % 100 == 0:
print("episode:", n, "total reward:", totalreward, "eps:", eps, "avg reward (last 100):", totalrewards[max(0, n-100):(n+1)].mean())
print("avg reward for last 100 episodes:", totalrewards[-100:].mean())
print("total steps:", totalrewards.sum())
# np.savetxt("dataaa.csv", totalrewards, fmt="%d", delimiter=",")
plt.plot(totalrewards)
plt.title("Rewards")
plt.show()
plot_running_avg(totalrewards)
if __name__ == '__main__':
main()