-
Notifications
You must be signed in to change notification settings - Fork 8
/
a3c_training_thread.py
339 lines (263 loc) · 10.7 KB
/
a3c_training_thread.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
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import random
import time
import sys
import gym
import os
import scipy.misc
from accum_trainer import AccumTrainer
from game_ac_network import Network
import cv2
LOG_INTERVAL = 100
PERFORMANCE_LOG_INTERVAL = 1000
#USE_ALE = False
FLAGS = tf.app.flags.FLAGS
#pong_actions = [1, 2, 3]
class A3CTrainingThread(object):
def __init__(self,
thread_index,
global_network,
initial_learning_rate,
learning_rate_input,
grad_applier,
max_global_time_step,
device,
sess,
name="agent"):
self.thread_index = thread_index
self.learning_rate_input = learning_rate_input
self.max_global_time_step = max_global_time_step
#if USE_LSTM:
# self.local_network = GameACLSTMNetwork(ACTION_SIZE, thread_index, device)
#else:
self.local_network = Network(name=name)
self.local_network.prepare_loss(FLAGS.entropy_beta)
# TODO: don't need accum trainer anymore with batch
self.trainer = AccumTrainer(device)
self.local_network.vars = self.trainer.prepare_minimize(self.local_network.total_loss,
self.local_network.get_train_vars())
self.accum_gradients = self.trainer.accumulate_gradients()
self.reset_gradients = self.trainer.reset_gradients()
self.apply_gradients = grad_applier.apply_gradients(
global_network.get_train_vars(),
self.trainer.get_accum_grad_list())
self.sync = self.local_network.sync_from(global_network)
#if USE_ALE:
# self.game_state = GameState(113 * thread_index)
#else:
self.game = gym.make('Lis-v2')
self.game.configure(str(5000 + thread_index))
# game initialization
# observation = env.reset()
self.observation, reward, end_episode, _ = self.game.step(1)
#self.observation = self.preprocess([self.observation])
self.history = [self.rgb2gray(self.observation) for _ in range(4)]#FLAGS.history_frames
self.observation = np.dstack(self.history)
self.local_t = 0
self.initial_learning_rate = initial_learning_rate
self.episode_reward = 0
# variable controling log output
self.prev_local_t = 0
def _anneal_learning_rate(self, global_time_step):
learning_rate = self.initial_learning_rate * (
self.max_global_time_step - global_time_step) / self.max_global_time_step
if learning_rate < 0.0:
learning_rate = 0.0
return learning_rate
def choose_action(self, pi_values):
values = []
sum = 0.0
for rate in pi_values:
sum = sum + rate
value = sum
values.append(value)
r = random.random() * sum
for i in range(len(values)):
if values[i] >= r:
return i
# fail safe
return len(values) - 1
def _record_score(self, sess, summary_writer, summary_op, score_input, score, global_t):
summary_str = sess.run(summary_op, feed_dict={
score_input: score
})
summary_writer.add_summary(summary_str, global_t)
def set_start_time(self, start_time):
self.start_time = start_time
def rgb2gray(self, rgb, i=0):
if FLAGS.save_frames:
if self.thread_index == 0 and len(os.listdir(os.path.join(FLAGS.model_dir, "images"))) < 1000:
scipy.misc.imsave("%s/%i.png" % (os.path.join(FLAGS.model_dir, "images"), i), rgb["image"][0])
img = np.asarray(rgb["image"][0])[..., :3]
img = np.dot(img, [0.299, 0.587, 0.114])
img = scipy.misc.imresize(img, (84, 84)) / 255.0
#flip H
#
#img = np.fliplr(img)
return img
#return -np.dot(img, [0.299, 0.587, 0.114]) / 255.0 + 1.0
def preprocess(self, frames, name=0):
if len(frames) == 1:
gray = self.rgb2gray(frames[0])
return np.dstack([gray, gray, gray, gray])
return np.dstack([self.rgb2gray(frame) for frame in frames])
def action2string(self, action):
moveX, moveZ, turn = 0, 0, 0
"""if action == 0:
moveX = -10
elif action == 1:
moveX = 10
elif action == 2:
moveZ = -10
elif action == 3:
moveZ = 10
elif action == 4:
turn = 10
elif action == 5:
turn = -10
elif action == 6:
pass"""
if action == 0:
turn = -10
elif action == 1:
turn = 10
elif action == 2:
moveZ = 10
elif action == 3:
pass
return "%s %s %s" % (moveX, moveZ, turn)
def get_frame(self, index):
if index > len(self.history):
return self.history[-1]
else:
return self.history[-index]
def process(self, sess, global_t, summary_writer, summary_op, score_input):
states = []
actions = []
rewards = []
values = []
terminal_end = False
# reset accumulated gradients
sess.run(self.reset_gradients)
# copy weights from shared to local
sess.run(self.sync)
start_local_t = self.local_t
#if USE_LSTM:
# start_lstm_state = self.local_network.lstm_state_out
# t_max times loop
for i in range(FLAGS.local_t_max):
#if USE_ALE:
# pi_, value_ = self.local_network.run_policy_and_value(sess, self.game_state.s_t)
#else:
pi_, value_ = self.local_network.run_policy_and_value(sess, self.observation)
#if self.thread_index == 0:
#print(pi_)
#cv2.namedWindow("img", cv2.WINDOW_NORMAL)
#cv2.imshow("img", self.observation)
#cv2.waitKey(1)
"""if self.thread_index == 0 and len(os.listdir(os.path.join(FLAGS.model_dir, "images"))) < 1000:
ft = sess.run(self.local_network.col_hiddens[0][0], feed_dict={self.local_network.s: [self.observation]})
print(ft.shape)
scipy.misc.imsave("%s/%i-obs.png" % (os.path.join(FLAGS.model_dir, "images"), global_t + i),
self.observation[:, :, 3])
for m in range(8):
img = ft[0, :, :, m]
img = img - np.amin(img)
img /= np.amax(img)
img *= 255.0
scipy.misc.imsave("%s/%i-feature-%i.png" % (os.path.join(FLAGS.model_dir, "images"), global_t + i, m),
img)
"""
action = self.choose_action(pi_)
states.append(self.observation)
actions.append(action)
values.append(value_)
if (self.thread_index == 0) and (self.local_t % LOG_INTERVAL == 0):
print("pi={}".format(pi_))
print(" V={}".format(value_))
#if USE_ALE:
#self.game_state.process(action)
#reward = self.game_state.reward
#end_episode = self.game_state.terminal
#else:
#for i in range(FLAGS.skip_frames):
new_obs, reward, end_episode, _ = self.game.step(self.action2string(action))
if len(self.history) > 10:
del self.history[0]
self.history.append(self.rgb2gray(new_obs, global_t + self.local_t))#, "%i-a%i" % (global_t, action)
def create_history():
return np.dstack([self.get_frame(1), self.get_frame(2), self.get_frame(3), self.get_frame(4)])
new_observation = create_history()
# process game
#self.game_state.process(action)
# receive game result
#reward = self.game_state.reward
terminal = end_episode#self.game_state.terminal
self.episode_reward += reward
# clip reward
rewards.append(np.clip(reward, -1, 1))
self.local_t += 1
#if USE_ALE:
# s_t1 -> s_t
# self.game_state.update()
#else:
if terminal:
terminal_end = True
print("score={}".format(self.episode_reward))
self._record_score(sess, summary_writer, summary_op, score_input,
self.episode_reward, global_t)
self.episode_reward = 0
#if USE_ALE:
self.game.reset()
#else:
#self.history = [self.rgb2gray(self.game.step(0))]
#self.observation = create_history()
#if USE_LSTM:
# self.local_network.reset_state()
break
else:
self.observation = new_observation
R = 0.0
if not terminal_end:
#if USE_ALE:
# R = self.local_network.run_value(sess, self.game_state.s_t)
#else:
R = self.local_network.run_value(sess, self.observation)
actions.reverse()
states.reverse()
rewards.reverse()
values.reverse()
batch_si = []
batch_a = []
batch_td = []
batch_R = []
# compute and accmulate gradients
for (ai, ri, si, Vi) in zip(actions, rewards, states, values):
R = ri + FLAGS.gamma * R
td = R - Vi
a = np.zeros([FLAGS.action_size])
a[ai] = 1
batch_si.append(si)
batch_a.append(a)
batch_td.append(td)
batch_R.append(R)
sess.run(self.accum_gradients,
feed_dict={
self.local_network.s: batch_si,
self.local_network.a: batch_a,
self.local_network.td: batch_td,
self.local_network.r: batch_R})
cur_learning_rate = self._anneal_learning_rate(global_t)
sess.run(self.apply_gradients,
feed_dict={self.learning_rate_input: cur_learning_rate})
if (self.thread_index == 0) and (self.local_t - self.prev_local_t >= PERFORMANCE_LOG_INTERVAL):
self.prev_local_t += PERFORMANCE_LOG_INTERVAL
elapsed_time = time.time() - self.start_time
steps_per_sec = global_t / elapsed_time
print("### Performance : {} STEPS in {:.0f} sec. {:.0f} STEPS/sec. {:.2f}M STEPS/hour".format(
global_t, elapsed_time, steps_per_sec, steps_per_sec * 3600 / 1000000.))
# return advanced local step size
diff_local_t = self.local_t - start_local_t
return diff_local_t