-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhavakv_CartPole-A3C.py
377 lines (269 loc) · 8.61 KB
/
havakv_CartPole-A3C.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# coding: utf-8
# In[ ]:
# OpenGym CartPole-v0 with A3C on GPU
# -----------------------------------
#
# A3C implementation with GPU optimizer threads.
#
# Made as part of blog series Let's make an A3C, available at
# https://jaromiru.com/2017/02/16/lets-make-an-a3c-theory/
#
# original author: Jaromir Janisch, 2017
import numpy as np
import tensorflow as tf
import gym, time, random, threading
from keras.models import *
from keras.layers import *
from keras import backend as K
# 10, 8, 2
# 3596
# 3679
# 3621
# 3669
# 3652
# 3619
# 3624
# 3667
# 10, 1, 2
# 5671
# 10, 2, 2
# 6397
# 6382
# 10, 4, 2
# 5563
# 5522
# 5562
# 5534
# 10, 1, 2, ntd
# 18174
# 10, 2, 2, ntd
# 13382
# 13268
# 10, 2, 1, ntd
# 11984
# 11796
# 10, 1, 1, ntd
# 19096
#-- constants
ENV = 'CartPole-v0'
RUN_TIME = 10
THREADS = 1
OPTIMIZERS = 1
THREAD_DELAY = 0.001
GAMMA = 0.99
N_STEP_RETURN = 8
GAMMA_N = GAMMA**N_STEP_RETURN
EPS_START = 0.4
EPS_STOP = .15
EPS_STEPS = 75000
MIN_BATCH = 32
LEARNING_RATE = 5e-3
LOSS_V = .5 # v loss coefficient
LOSS_ENTROPY = .01 # entropy coefficient
#---------
class Brain:
train_queue = [[], [], [], [], []] # s, a, r, s', s' terminal mask
lock_queue = threading.Lock()
def __init__(self):
self.session = tf.Session()
K.set_session(self.session)
K.manual_variable_initialization(True)
self.model = self._build_model()
self.graph = self._build_graph(self.model)
self.session.run(tf.global_variables_initializer())
self.default_graph = tf.get_default_graph()
self.default_graph.finalize() # avoid modifications
def _build_model(self):
l_input = Input(batch_shape=(None, NUM_STATE))
l_dense = Dense(16, activation='relu')(l_input)
out_actions = Dense(NUM_ACTIONS, activation='softmax')(l_dense)
out_value = Dense(1, activation='linear')(l_dense)
model = Model(input=[l_input], output=[out_actions, out_value])
model._make_predict_function() # have to initialize before threading
return model
def _build_graph(self, model):
s_t = tf.placeholder(tf.float32, shape=(None, NUM_STATE))
a_t = tf.placeholder(tf.float32, shape=(None, NUM_ACTIONS))
r_t = tf.placeholder(
tf.float32,
shape=(None, 1)) # not immediate, but discounted n step reward
p, v = model(s_t)
log_prob = tf.log(
tf.reduce_sum(p * a_t, axis=1, keep_dims=True) + 1e-10)
advantage = r_t - v
loss_policy = -log_prob * tf.stop_gradient(
advantage) # maximize policy
loss_value = LOSS_V * tf.square(advantage) # minimize value error
entropy = LOSS_ENTROPY * tf.reduce_sum(
p * tf.log(p + 1e-10), axis=1,
keep_dims=True) # maximize entropy (regularization)
loss_total = tf.reduce_mean(loss_policy + loss_value + entropy)
optimizer = tf.train.RMSPropOptimizer(LEARNING_RATE, decay=.99)
minimize = optimizer.minimize(loss_total)
return s_t, a_t, r_t, minimize
def optimize(self):
if len(self.train_queue[0]) < MIN_BATCH:
time.sleep(0) # yield
return
with self.lock_queue:
if len(self.train_queue[0]
) < MIN_BATCH: # more thread could have passed without lock
return # we can't yield inside lock
s, a, r, s_, s_mask = self.train_queue
self.train_queue = [[], [], [], [], []]
s = np.vstack(s)
a = np.vstack(a)
r = np.vstack(r)
s_ = np.vstack(s_)
s_mask = np.vstack(s_mask)
if len(s) > 5 * MIN_BATCH:
print("Optimizer alert! Minimizing batch of %d" % len(s))
v = self.predict_v(s_)
r = r + GAMMA_N * v * s_mask # set v to 0 where s_ is terminal state
s_t, a_t, r_t, minimize = self.graph
self.session.run(minimize, feed_dict={s_t: s, a_t: a, r_t: r})
def train_push(self, s, a, r, s_):
with self.lock_queue:
self.train_queue[0].append(s)
self.train_queue[1].append(a)
self.train_queue[2].append(r)
if s_ is None:
self.train_queue[3].append(NONE_STATE)
self.train_queue[4].append(0.)
else:
self.train_queue[3].append(s_)
self.train_queue[4].append(1.)
def predict(self, s):
with self.default_graph.as_default():
p, v = self.model.predict(s)
return p, v
def predict_p(self, s):
with self.default_graph.as_default():
p, v = self.model.predict(s)
return p
def predict_v(self, s):
with self.default_graph.as_default():
p, v = self.model.predict(s)
return v
#---------
frames = 0
class Agent:
def __init__(self, eps_start, eps_end, eps_steps):
self.eps_start = eps_start
self.eps_end = eps_end
self.eps_steps = eps_steps
self.memory = [] # used for n_step return
self.R = 0.
def getEpsilon(self):
if (frames >= self.eps_steps):
return self.eps_end
else:
return self.eps_start + frames * (
self.eps_end - self.eps_start
) / self.eps_steps # linearly interpolate
def act(self, s):
eps = self.getEpsilon()
global frames
frames = frames + 1
if random.random() < eps:
return random.randint(0, NUM_ACTIONS - 1)
else:
s = np.array([s])
p = brain.predict_p(s)[0]
# a = np.argmax(p)
a = np.random.choice(NUM_ACTIONS, p=p)
return a
def train(self, s, a, r, s_):
def get_sample(memory, n):
s, a, _, _ = memory[0]
_, _, _, s_ = memory[n - 1]
return s, a, self.R, s_
a_cats = np.zeros(
NUM_ACTIONS) # turn action into one-hot representation
a_cats[a] = 1
self.memory.append((s, a_cats, r, s_))
self.R = (self.R + r * GAMMA_N) / GAMMA
if s_ is None:
while len(self.memory) > 0:
n = len(self.memory)
s, a, r, s_ = get_sample(self.memory, n)
brain.train_push(s, a, r, s_)
self.R = (self.R - self.memory[0][2]) / GAMMA
self.memory.pop(0)
self.R = 0
if len(self.memory) >= N_STEP_RETURN:
s, a, r, s_ = get_sample(self.memory, N_STEP_RETURN)
brain.train_push(s, a, r, s_)
self.R = self.R - self.memory[0][2]
self.memory.pop(0)
# possible edge case - if an episode ends in <N steps, the computation is incorrect
#---------
class Environment(threading.Thread):
stop_signal = False
def __init__(self,
render=False,
eps_start=EPS_START,
eps_end=EPS_STOP,
eps_steps=EPS_STEPS):
threading.Thread.__init__(self)
self.render = render
self.env = gym.make(ENV)
self.agent = Agent(eps_start, eps_end, eps_steps)
self.stepCounter = 0
def runEpisode(self):
s = self.env.reset()
R = 0
while True:
time.sleep(THREAD_DELAY) # yield
if self.render: self.env.render()
a = self.agent.act(s)
s_, r, done, info = self.env.step(a)
self.stepCounter += 1
if done: # terminal state
s_ = None
self.agent.train(s, a, r, s_)
s = s_
R += r
if done or self.stop_signal:
break
# print("Total R:", R)
def run(self):
while not self.stop_signal:
self.runEpisode()
def stop(self):
self.stop_signal = True
#---------
class Optimizer(threading.Thread):
stop_signal = False
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while not self.stop_signal:
brain.optimize()
def stop(self):
self.stop_signal = True
#-- main
env_test = Environment(render=True, eps_start=0., eps_end=0.)
NUM_STATE = env_test.env.observation_space.shape[0]
NUM_ACTIONS = env_test.env.action_space.n
NONE_STATE = np.zeros(NUM_STATE)
brain = Brain() # brain is global in A3C
envs = [Environment() for i in range(THREADS)]
opts = [Optimizer() for i in range(OPTIMIZERS)]
for o in opts:
o.start()
for e in envs:
e.start()
time.sleep(RUN_TIME)
for e in envs:
e.stop()
for e in envs:
e.join()
for o in opts:
o.stop()
for o in opts:
o.join()
print("Training finished")
for e in envs:
print(e.stepCounter)
env_test.run()