forked from rlcode/reinforcement-learning-kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakout_a3c.py
378 lines (300 loc) · 13.9 KB
/
breakout_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
377
378
from skimage.color import rgb2gray
from skimage.transform import resize
from keras.layers import Dense, Flatten, Input
from keras.layers.convolutional import Conv2D
from keras.optimizers import RMSprop
from keras import backend as K
from keras.models import Model
import tensorflow as tf
import numpy as np
import threading
import random
import time
import gym
# 멀티쓰레딩을 위한 글로벌 변수
global episode
episode = 0
EPISODES = 8000000
# 환경 생성
env_name = "BreakoutDeterministic-v4"
# 브레이크아웃에서의 A3CAgent 클래스(글로벌신경망)
class A3CAgent:
def __init__(self, action_size):
# 상태크기와 행동크기를 갖고옴
self.state_size = (84, 84, 4)
self.action_size = action_size
# A3C 하이퍼파라미터
self.discount_factor = 0.99
self.no_op_steps = 30
self.actor_lr = 2.5e-4
self.critic_lr = 2.5e-4
# 쓰레드의 갯수
self.threads = 8
# 정책신경망과 가치신경망을 생성
self.actor, self.critic = self.build_model()
# 정책신경망과 가치신경망을 업데이트하는 함수 생성
self.optimizer = [self.actor_optimizer(), self.critic_optimizer()]
# 텐서보드 설정
self.sess = tf.InteractiveSession()
K.set_session(self.sess)
self.sess.run(tf.global_variables_initializer())
self.summary_placeholders, self.update_ops, self.summary_op = \
self.setup_summary()
self.summary_writer = \
tf.summary.FileWriter('summary/breakout_a3c', self.sess.graph)
# 쓰레드를 만들어 학습을 하는 함수
def train(self):
# 쓰레드 수만큼 Agent 클래스 생성
agents = [Agent(self.action_size, self.state_size,
[self.actor, self.critic], self.sess,
self.optimizer, self.discount_factor,
[self.summary_op, self.summary_placeholders,
self.update_ops, self.summary_writer])
for _ in range(self.threads)]
# 각 쓰레드 시작
for agent in agents:
time.sleep(1)
agent.start()
# 10분(600초)에 한번씩 모델을 저장
while True:
time.sleep(60 * 10)
self.save_model("./save_model/breakout_a3c")
# 정책신경망과 가치신경망을 생성
def build_model(self):
input = Input(shape=self.state_size)
conv = Conv2D(16, (8, 8), strides=(4, 4), activation='relu')(input)
conv = Conv2D(32, (4, 4), strides=(2, 2), activation='relu')(conv)
conv = Flatten()(conv)
fc = Dense(256, activation='relu')(conv)
policy = Dense(self.action_size, activation='softmax')(fc)
value = Dense(1, activation='linear')(fc)
actor = Model(inputs=input, outputs=policy)
critic = Model(inputs=input, outputs=value)
# 가치와 정책을 예측하는 함수를 만들어냄
actor._make_predict_function()
critic._make_predict_function()
actor.summary()
critic.summary()
return actor, critic
# 정책신경망을 업데이트하는 함수
def actor_optimizer(self):
action = K.placeholder(shape=[None, self.action_size])
advantages = K.placeholder(shape=[None, ])
policy = self.actor.output
# 정책 크로스 엔트로피 오류함수
action_prob = K.sum(action * policy, axis=1)
cross_entropy = K.log(action_prob + 1e-10) * advantages
cross_entropy = -K.sum(cross_entropy)
# 탐색을 지속적으로 하기 위한 엔트로피 오류
entropy = K.sum(policy * K.log(policy + 1e-10), axis=1)
entropy = K.sum(entropy)
# 두 오류함수를 더해 최종 오류함수를 만듬
loss = cross_entropy + 0.01 * entropy
optimizer = RMSprop(lr=self.actor_lr, rho=0.99, epsilon=0.01)
updates = optimizer.get_updates(self.actor.trainable_weights, [],loss)
train = K.function([self.actor.input, action, advantages],
[loss], updates=updates)
return train
# 가치신경망을 업데이트하는 함수
def critic_optimizer(self):
discounted_prediction = K.placeholder(shape=(None,))
value = self.critic.output
# [반환값 - 가치]의 제곱을 오류함수로 함
loss = K.mean(K.square(discounted_prediction - value))
optimizer = RMSprop(lr=self.critic_lr, rho=0.99, epsilon=0.01)
updates = optimizer.get_updates(self.critic.trainable_weights, [],loss)
train = K.function([self.critic.input, discounted_prediction],
[loss], updates=updates)
return train
def load_model(self, name):
self.actor.load_weights(name + "_actor.h5")
self.critic.load_weights(name + "_critic.h5")
def save_model(self, name):
self.actor.save_weights(name + "_actor.h5")
self.critic.save_weights(name + "_critic.h5")
# 각 에피소드 당 학습 정보를 기록
def setup_summary(self):
episode_total_reward = tf.Variable(0.)
episode_avg_max_q = tf.Variable(0.)
episode_duration = tf.Variable(0.)
tf.summary.scalar('Total Reward/Episode', episode_total_reward)
tf.summary.scalar('Average Max Prob/Episode', episode_avg_max_q)
tf.summary.scalar('Duration/Episode', episode_duration)
summary_vars = [episode_total_reward,
episode_avg_max_q,
episode_duration]
summary_placeholders = [tf.placeholder(tf.float32)
for _ in range(len(summary_vars))]
update_ops = [summary_vars[i].assign(summary_placeholders[i])
for i in range(len(summary_vars))]
summary_op = tf.summary.merge_all()
return summary_placeholders, update_ops, summary_op
# 액터러너 클래스(쓰레드)
class Agent(threading.Thread):
def __init__(self, action_size, state_size, model, sess,
optimizer, discount_factor, summary_ops):
threading.Thread.__init__(self)
# A3CAgent 클래스에서 상속
self.action_size = action_size
self.state_size = state_size
self.actor, self.critic = model
self.sess = sess
self.optimizer = optimizer
self.discount_factor = discount_factor
[self.summary_op, self.summary_placeholders,
self.update_ops, self.summary_writer] = summary_ops
# 지정된 타임스텝동안 샘플을 저장할 리스트
self.states, self.actions, self.rewards = [], [], []
# 로컬 모델 생성
self.local_actor, self.local_critic = self.build_local_model()
self.avg_p_max = 0
self.avg_loss = 0
# 모델 업데이트 주기
self.t_max = 20
self.t = 0
def run(self):
global episode
env = gym.make(env_name)
step = 0
while episode < EPISODES:
done = False
dead = False
score, start_life = 0, 5
observe = env.reset()
next_observe = observe
# 0~30 상태동안 정지
for _ in range(random.randint(1, 30)):
observe = next_observe
next_observe, _, _, _ = env.step(1)
state = pre_processing(next_observe, observe)
history = np.stack((state, state, state, state), axis=2)
history = np.reshape([history], (1, 84, 84, 4))
while not done:
step += 1
self.t += 1
observe = next_observe
action, policy = self.get_action(history)
# 1: 정지, 2: 왼쪽, 3: 오른쪽
if action == 0:
real_action = 1
elif action == 1:
real_action = 2
else:
real_action = 3
# 죽었을 때 시작하기 위해 발사 행동을 함
if dead:
action = 0
real_action = 1
dead = False
# 선택한 행동으로 한 스텝을 실행
next_observe, reward, done, info = env.step(real_action)
# 각 타임스텝마다 상태 전처리
next_state = pre_processing(next_observe, observe)
next_state = np.reshape([next_state], (1, 84, 84, 1))
next_history = np.append(next_state, history[:, :, :, :3],
axis=3)
# 정책의 최대값
self.avg_p_max += np.amax(self.actor.predict(
np.float32(history / 255.)))
if start_life > info['ale.lives']:
dead = True
start_life = info['ale.lives']
score += reward
reward = np.clip(reward, -1., 1.)
# 샘플을 저장
self.append_sample(history, action, reward)
if dead:
history = np.stack((next_state, next_state,
next_state, next_state), axis=2)
history = np.reshape([history], (1, 84, 84, 4))
else:
history = next_history
# 에피소드가 끝나거나 최대 타임스텝 수에 도달하면 학습을 진행
if self.t >= self.t_max or done:
self.train_model(done)
self.update_local_model()
self.t = 0
if done:
# 각 에피소드 당 학습 정보를 기록
episode += 1
print("episode:", episode, " score:", score, " step:",
step)
stats = [score, self.avg_p_max / float(step),
step]
for i in range(len(stats)):
self.sess.run(self.update_ops[i], feed_dict={
self.summary_placeholders[i]: float(stats[i])
})
summary_str = self.sess.run(self.summary_op)
self.summary_writer.add_summary(summary_str, episode + 1)
self.avg_p_max = 0
self.avg_loss = 0
step = 0
# k-스텝 prediction 계산
def discounted_prediction(self, rewards, done):
discounted_prediction = np.zeros_like(rewards)
running_add = 0
if not done:
running_add = self.local_critic.predict(np.float32(
self.states[-1] / 255.))[0]
for t in reversed(range(0, len(rewards))):
running_add = running_add * self.discount_factor + rewards[t]
discounted_prediction[t] = running_add
return discounted_prediction
# 정책신경망과 가치신경망을 업데이트
def train_model(self, done):
discounted_prediction = self.discounted_prediction(self.rewards, done)
states = np.zeros((len(self.states), 84, 84, 4))
for i in range(len(self.states)):
states[i] = self.states[i]
states = np.float32(states / 255.)
values = self.local_critic.predict(states)
values = np.reshape(values, len(values))
advantages = discounted_prediction - values
self.optimizer[0]([states, self.actions, advantages])
self.optimizer[1]([states, discounted_prediction])
self.states, self.actions, self.rewards = [], [], []
# 로컬신경망을 생성하는 함수
def build_local_model(self):
input = Input(shape=self.state_size)
conv = Conv2D(16, (8, 8), strides=(4, 4), activation='relu')(input)
conv = Conv2D(32, (4, 4), strides=(2, 2), activation='relu')(conv)
conv = Flatten()(conv)
fc = Dense(256, activation='relu')(conv)
policy = Dense(self.action_size, activation='softmax')(fc)
value = Dense(1, activation='linear')(fc)
local_actor = Model(inputs=input, outputs=policy)
local_critic = Model(inputs=input, outputs=value)
local_actor._make_predict_function()
local_critic._make_predict_function()
local_actor.set_weights(self.actor.get_weights())
local_critic.set_weights(self.critic.get_weights())
local_actor.summary()
local_critic.summary()
return local_actor, local_critic
# 로컬신경망을 글로벌신경망으로 업데이트
def update_local_model(self):
self.local_actor.set_weights(self.actor.get_weights())
self.local_critic.set_weights(self.critic.get_weights())
# 정책신경망의 출력을 받아서 확률적으로 행동을 선택
def get_action(self, history):
history = np.float32(history / 255.)
policy = self.local_actor.predict(history)[0]
action_index = np.random.choice(self.action_size, 1, p=policy)[0]
return action_index, policy
# 샘플을 저장
def append_sample(self, history, action, reward):
self.states.append(history)
act = np.zeros(self.action_size)
act[action] = 1
self.actions.append(act)
self.rewards.append(reward)
# 학습속도를 높이기 위해 흑백화면으로 전처리
def pre_processing(next_observe, observe):
processed_observe = np.maximum(next_observe, observe)
processed_observe = np.uint8(
resize(rgb2gray(processed_observe), (84, 84), mode='constant') * 255)
return processed_observe
if __name__ == "__main__":
global_agent = A3CAgent(action_size=3)
global_agent.train()