-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdqn.py
349 lines (286 loc) · 13.5 KB
/
dqn.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
# coding:utf-8
import os
import gym
import random
import numpy as np
import tensorflow as tf
from collections import deque
from skimage.color import rgb2gray
from skimage.transform import resize
from keras.models import Sequential
from keras.layers import Conv2D, Flatten, Dense
from keras import backend as K
K.set_image_dim_ordering('th')
import argparse
BATCH_SIZE = 32
TRAIN_INTERVAL = 4 # The agent selects 4 actions between successive updates
LEARNING_RATE = 0.00025 # Learning rate used by RMSProp
MOMENTUM = 0.95 # Momentum used by RMSProp
# Constant added to the squared gradient in the denominator of the RMSProp update
MIN_GRAD = 0.01
class Agent(object):
def __init__(self, num_actions, use_gpu):
self.frame_width = 84 # Resized frame width
self.frame_height = 84
self.state_length = 4
self.use_gpu = use_gpu
self.init_replay_size = 20000
self.target_update_interval = 10000
self.save_interval = 300000
self.num_actions = num_actions
self.epsilon = 1.0
self.epsilon_step = (
1.0 - 0.1) / 1000000
self.t = 0
# Parameters used for summary
self.total_reward = 0
self.total_q_max = 0
self.total_loss = 0
self.duration = 0
self.episode = 0
# Create replay memory
self.replay_memory = deque()
# Create q network
self.s, self.q_values, q_network = self.build_network()
q_network_weights = q_network.trainable_weights
# Create target network
self.st, self.target_q_values, target_network = self.build_network()
target_network_weights = target_network.trainable_weights
# Define target network update operation
self.update_target_network = [target_network_weights[i].assign(
q_network_weights[i]) for i in range(len(target_network_weights))]
# Define loss and gradient update operation
self.a, self.y, self.loss, self.grads_update = self.build_training_op(
q_network_weights)
self.sess = tf.InteractiveSession()
self.saver = tf.train.Saver(q_network_weights)
self.summary_placeholders, self.update_ops, self.summary_op = self.setup_summary()
self.summary_writer = tf.summary.FileWriter(
'summary/Breakout-v0', self.sess.graph)
if not os.path.exists('saved_networks/Breakout-v0'):
os.makedirs('saved_networks/Breakout-v0')
self.sess.run(tf.global_variables_initializer())
# Load network
self.load_training = False
if self.load_training:
self.load_network()
# Initialize target network
self.sess.run(self.update_target_network)
def build_network(self):
with tf.device('/gpu:0' if self.use_gpu else '/cpu:0'):
model = Sequential()
model.add(Conv2D(32, (8, 8), strides=(4, 4), activation='relu',
input_shape=(self.state_length, self.frame_width, self.frame_height)))
model.add(Conv2D(64, (4, 4), strides=(2, 2), activation='relu'))
model.add(Conv2D(64, (3, 3), strides=(1, 1), activation='relu'))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(self.num_actions))
s = tf.placeholder(
tf.float32, [None, self.state_length, self.frame_width, self.frame_height])
q_values = model(s)
return s, q_values, model
def build_training_op(self, q_network_weights):
a = tf.placeholder(tf.int64, [None])
y = tf.placeholder(tf.float32, [None])
# Convert action to one hot vector
a_one_hot = tf.one_hot(a, self.num_actions, 1.0, 0.0)
q_value = tf.reduce_sum(
tf.multiply(self.q_values, a_one_hot), reduction_indices=1)
# Clip the error, the loss is quadratic when the error is in (-1, 1), and linear outside of that region
error = tf.abs(y - q_value)
quadratic_part = tf.clip_by_value(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = tf.reduce_mean(0.5 * tf.square(quadratic_part) + linear_part)
optimizer = tf.train.RMSPropOptimizer(
LEARNING_RATE, momentum=MOMENTUM, epsilon=MIN_GRAD)
grads_update = optimizer.minimize(loss, var_list=q_network_weights)
return a, y, loss, grads_update
def get_initial_state(self, observation, last_observation):
processed_observation = np.maximum(observation, last_observation)
processed_observation = np.uint8(
resize(rgb2gray(processed_observation), (self.frame_width, self.frame_height)) * 255)
state = [processed_observation for _ in range(self.state_length)]
return np.stack(state, axis=0)
def get_action(self, state):
if self.epsilon >= random.random() or self.t < self.init_replay_size:
action = random.randrange(self.num_actions)
else:
action = np.argmax(self.q_values.eval(
feed_dict={self.s: [np.float32(state / 255.0)]}))
# Anneal epsilon linearly over time
if self.epsilon > 0.1 and self.t >= self.init_replay_size:
self.epsilon -= self.epsilon_step
return action
def run(self, state, action, reward, terminal, observation):
next_state = np.append(state[1:, :, :], observation, axis=0)
# Clip all positive rewards at 1 and all negative rewards at -1, leaving 0 rewards unchanged
reward = np.clip(reward, -1, 1)
# Store transition in replay memory
self.replay_memory.append(
(state, action, reward, next_state, terminal))
if len(self.replay_memory) > 400000:
self.replay_memory.popleft()
if self.t >= self.init_replay_size:
# Train network
if self.t % TRAIN_INTERVAL == 0:
self.train_network()
# Update target network
if self.t % self.target_update_interval == 0:
self.sess.run(self.update_target_network)
# Save network
if self.t % self.save_interval == 0:
save_path = self.saver.save(
self.sess, 'saved_networks/Breakout-v0' + '/' + 'Breakout-v0', global_step=self.t)
print('Successfully saved: ' + save_path)
self.total_reward += reward
self.total_q_max += np.max(self.q_values.eval(
feed_dict={self.s: [np.float32(state / 255.0)]}))
self.duration += 1
if terminal:
# Write summary
if self.t >= self.init_replay_size:
stats = [self.total_reward, self.total_q_max / float(self.duration),
self.duration, self.total_loss / (float(self.duration) / float(TRAIN_INTERVAL))]
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, self.episode + 1)
# Debug
if self.t < self.init_replay_size:
mode = 'random'
elif self.init_replay_size <= self.t < self.init_replay_size + 1000000:
mode = 'explore'
else:
mode = 'exploit'
print('EPISODE: {0:6d} / TIMESTEP: {1:8d} / DURATION: {2:5d} / EPSILON: {3:.5f} / TOTAL_REWARD: {4:3.0f} / AVG_MAX_Q: {5:2.4f} / AVG_LOSS: {6:.5f} / MODE: {7}'.format(
self.episode + 1, self.t, self.duration, self.epsilon,
self.total_reward, self.total_q_max / float(self.duration),
self.total_loss / (float(self.duration) / float(TRAIN_INTERVAL)), mode))
self.total_reward = 0
self.total_q_max = 0
self.total_loss = 0
self.duration = 0
self.episode += 1
self.t += 1
return next_state
def train_network(self):
state_batch = []
action_batch = []
reward_batch = []
next_state_batch = []
terminal_batch = []
y_batch = []
# Sample random minibatch of transition from replay memory
minibatch = random.sample(self.replay_memory, BATCH_SIZE)
for data in minibatch:
state_batch.append(data[0])
action_batch.append(data[1])
reward_batch.append(data[2])
next_state_batch.append(data[3])
terminal_batch.append(data[4])
# Convert True to 1, False to 0
terminal_batch = np.array(terminal_batch) + 0
target_q_values_batch = self.target_q_values.eval(
feed_dict={self.st: np.float32(np.array(next_state_batch) / 255.0)})
y_batch = reward_batch + (1 - terminal_batch) * \
0.99 * np.max(target_q_values_batch, axis=1)
loss, _ = self.sess.run([self.loss, self.grads_update], feed_dict={
self.s: np.float32(np.array(state_batch) / 255.0),
self.a: action_batch,
self.y: y_batch
})
self.total_loss += loss
def setup_summary(self):
episode_total_reward = tf.Variable(0.)
tf.summary.scalar('Breakout-v0' + '/Total Reward/Episode',
episode_total_reward)
episode_avg_max_q = tf.Variable(0.)
tf.summary.scalar(
'Breakout-v0' + '/Average Max Q/Episode', episode_avg_max_q)
episode_duration = tf.Variable(0.)
tf.summary.scalar(
'Breakout-v0' + '/Duration/Episode', episode_duration)
episode_avg_loss = tf.Variable(0.)
tf.summary.scalar(
'Breakout-v0' + '/Average Loss/Episode', episode_avg_loss)
summary_vars = [episode_total_reward,
episode_avg_max_q, episode_duration, episode_avg_loss]
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
def load_network(self):
checkpoint = tf.train.get_checkpoint_state(
'saved_networks/Breakout-v0')
if checkpoint and checkpoint.model_checkpoint_path:
self.saver.restore(self.sess, checkpoint.model_checkpoint_path)
print('Successfully loaded: ' + checkpoint.model_checkpoint_path)
else:
print('Training new network...')
def get_action_at_test(self, state):
if random.random() <= 0.05:
action = random.randrange(self.num_actions)
else:
action = np.argmax(self.q_values.eval(
feed_dict={self.s: [np.float32(state / 255.0)]}))
self.t += 1
return action
def preprocess(observation, last_observation):
processed_observation = np.maximum(observation, last_observation)
processed_observation = np.uint8(
resize(rgb2gray(processed_observation), (84, 84)) * 255)
return np.reshape(processed_observation, (1, 84, 84))
def main():
parser = argparse.ArgumentParser(description="Perform DQN ATARI Training.",
fromfile_prefix_chars="@", conflict_handler="resolve")
parser.add_argument("--train", default=False, action='store_true',
help="Perform training; Else testing. [Default: %(default)s]")
parser.add_argument("--gpu", default=False, action='store_true',
help="Perform training with gpu. [Default: %(default)s]")
parser.add_argument("--watch-agent-train", default=False, action='store_true',
help="Watch the agent train; CAUTION: Slows down training significantly. [Default: %(default)s]")
args = parser.parse_args()
env = gym.make('Breakout-v0')
agent = Agent(num_actions=env.action_space.n, use_gpu=args.gpu)
if args.train: # Train mode
for _ in range(1200):
terminal = False
observation = env.reset()
for _ in range(random.randint(1, 30)):
last_observation = observation
observation, _, _, _ = env.step(0) # Do nothing
state = agent.get_initial_state(observation, last_observation)
while not terminal:
last_observation = observation
action = agent.get_action(state)
observation, reward, terminal, _ = env.step(action)
env.render() if args.watch_agent_train else None
processed_observation = preprocess(
observation, last_observation)
state = agent.run(state, action, reward,
terminal, processed_observation)
else: # Testing
for _ in range(20):
terminal = False
observation = env.reset()
for _ in range(random.randint(1, 30)):
last_observation = observation
observation, _, _, _ = env.step(0) # Do nothing
state = agent.get_initial_state(observation, last_observation)
while not terminal:
last_observation = observation
action = agent.get_action_at_test(state)
observation, _, terminal, _ = env.step(action)
env.render()
processed_observation = preprocess(
observation, last_observation)
state = np.append(
state[1:, :, :], processed_observation, axis=0)
# env.monitor.close()
if __name__ == '__main__':
main()