-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain script.py
More file actions
280 lines (223 loc) · 10.1 KB
/
Copy pathMain script.py
File metadata and controls
280 lines (223 loc) · 10.1 KB
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
import numpy as np
import gym
import tensorflow as tf
from gym.wrappers.monitoring.video_recorder import VideoRecorder
from collections import deque, Counter
from tensorflow.contrib.layers import flatten, conv2d, fully_connected
import random
from datetime import datetime
from gym import wrappers
from time import time
CONTRAST = np.array([210,164,74]).mean()
EPSILON = 0.5
MIN_EPSILON = 0.05
MAX_EPSILON = 1
EPS_DELTA_STEPS = 500000
BUF_LENGTH = 20000
NUM_EPISODES = 1200
BATCH_SIZE = 50
LEARNING_RATE = 0.1
DISCOUNT_FACTOR = 0.97
INPUT_SHAPE = (None, 88, 80, 1)
X_SHAPE = (None, 88, 80, 1)
STEPS_TRAIN = 4
START_STEPS = 2000
COPY_STEPS = 100
exp_buffer = deque(maxlen=BUF_LENGTH)
global_step = 0
def preprocess_image(obs):
res = obs[1:176:2, ::2]
res = res.mean(axis=2)
res[res==CONTRAST] = 0
res = (res-128)/128 - 1
return res.reshape(88, 80, 1)
def dqn(x, scope, n_outputs):
initializer = tf.contrib.layers.variance_scaling_initializer()
with tf.variable_scope(scope) as cur_scope:
layer1 = conv2d(x, num_outputs=32, kernel_size=(8,8), stride=4, padding='SAME',weights_initializer=initializer)
tf.summary.histogram('layer1', layer1)
layer2 = conv2d(layer1, num_outputs=64, kernel_size=(4,4), stride=2, padding='SAME', weights_initializer=initializer)
tf.summary.histogram('layer2', layer2)
layer3 = conv2d(layer2, num_outputs=64, kernel_size=(3, 3), stride=1, padding='SAME', weights_initializer=initializer)
tf.summary.histogram('layer3', layer3)
flat = flatten(layer3)
fully_con = fully_connected(flat, num_outputs=128, weights_initializer=initializer)
tf.summary.histogram('fully_con', fully_con)
output = fully_connected(fully_con, num_outputs=n_outputs, activation_fn=None, weights_initializer=initializer)
tf.summary.histogram('output', output)
params = {v.name[len(cur_scope.name): ]: v for v in tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES, scope=cur_scope.name)}
return params, output
def get_sample(batch_size):
perm_batch = np.random.permutation(len(exp_buffer))[:batch_size]
mem = np.array(exp_buffer)[perm_batch]
return mem[:,0], mem[:,1], mem[:,2], mem[:,3], mem[:,4]
def epsilon_greedy(action, step):
p = np.random.random(1).squeeze()
epsilon = max(MIN_EPSILON, MAX_EPSILON - (MAX_EPSILON - MIN_EPSILON) * step/EPS_DELTA_STEPS)
if np.random.rand() < epsilon:
return np.random.randint(n_outputs)
else:
return action
env = gym.make("MsPacman-v0")
n_outputs = env.action_space.n
tf.reset_default_graph()
x = tf.placeholder(tf.float32, shape=X_SHAPE)
y = tf.placeholder(tf.float32, shape=(None,1))
in_training_mode = tf.placeholder(tf.bool)
main_dqn, main_dqn_outputs = dqn(x, 'mainQ', n_outputs)
target_dqn, target_dqn_outputs = dqn(x, 'targetQ', n_outputs)
x_action = tf.placeholder(tf.int32, shape=(None,))
q_action = tf.reduce_sum(target_dqn_outputs * tf.one_hot(x_action, n_outputs), axis=-1, keep_dims=True)
copy_operation = [tf.assign(main_name, target_dqn[var_name]) for var_name, main_name in main_dqn.items()]
copy_target_to_main = tf.group(*copy_operation)
loss = tf.reduce_mean(tf.square(y-q_action))
optimizer = tf.train.AdamOptimizer(LEARNING_RATE)
training_operation = optimizer.minimize(loss)
init = tf.global_variables_initializer()
loss_summary = tf.summary.scalar('LOSS', loss)
merge_summary = tf.summary.merge_all()
with tf.Session() as session:
init.run()
for i in range(NUM_EPISODES):
done = False
obs = env.reset()
env.render()
epoch = 0
episodic_reward = 0
actions_counter = Counter()
episodic_loss = []
while not done:
env.render()
obs = preprocess_image(obs)
actions = main_dqn_outputs.eval(feed_dict={x:[obs], in_training_mode:False})
action = np.argmax(actions, axis=-1)
actions_counter[str(action)] += 1
action = epsilon_greedy(action, global_step)
next_obs, reward, done, _ = env.step(action)
exp_buffer.append([obs, action, preprocess_image(next_obs), reward, done])
if global_step % STEPS_TRAIN == 0 and global_step > START_STEPS:
o_obs, o_act, o_next_obs, o_reward, o_done = get_sample(BATCH_SIZE)
o_obs = [x for x in o_obs]
o_next_obs = [x for x in o_next_obs]
next_act = main_dqn_outputs.eval(feed_dict={x:o_next_obs, in_training_mode:False})
y_batch = o_reward + DISCOUNT_FACTOR * np.max(next_act, axis=-1) * (1-o_done)
train_loss, _ = session.run([loss, training_operation], feed_dict={x:o_obs, y:np.expand_dims(y_batch, axis=-1),
x_action: o_act, in_training_mode:True})
episodic_loss.append(train_loss)
if (global_step+1)%COPY_STEPS == 0 and global_step > START_STEPS:
copy_target_to_main.run()
obs=next_obs
epoch+=1
global_step+=1import numpy as np
import gym
import tensorflow as tf
from gym.wrappers.monitoring.video_recorder import VideoRecorder
from collections import deque, Counter
from tensorflow.contrib.layers import flatten, conv2d, fully_connected
import random
from datetime import datetime
from gym import wrappers
from time import time
CONTRAST = np.array([210,164,74]).mean()
EPSILON = 0.5
MIN_EPSILON = 0.05
MAX_EPSILON = 1
EPS_DELTA_STEPS = 500000
BUF_LENGTH = 20000
NUM_EPISODES = 1200
BATCH_SIZE = 50
LEARNING_RATE = 0.1
DISCOUNT_FACTOR = 0.97
INPUT_SHAPE = (None, 88, 80, 1)
X_SHAPE = (None, 88, 80, 1)
STEPS_TRAIN = 4
START_STEPS = 2000
COPY_STEPS = 100
exp_buffer = deque(maxlen=BUF_LENGTH)
global_step = 0
def preprocess_image(obs):
res = obs[1:176:2, ::2]
res = res.mean(axis=2)
res[res==CONTRAST] = 0
res = (res-128)/128 - 1
return res.reshape(88, 80, 1)
def dqn(x, scope, n_outputs):
initializer = tf.contrib.layers.variance_scaling_initializer()
with tf.variable_scope(scope) as cur_scope:
layer1 = conv2d(x, num_outputs=32, kernel_size=(8,8), stride=4, padding='SAME',weights_initializer=initializer)
tf.summary.histogram('layer1', layer1)
layer2 = conv2d(layer1, num_outputs=64, kernel_size=(4,4), stride=2, padding='SAME', weights_initializer=initializer)
tf.summary.histogram('layer2', layer2)
layer3 = conv2d(layer2, num_outputs=64, kernel_size=(3, 3), stride=1, padding='SAME', weights_initializer=initializer)
tf.summary.histogram('layer3', layer3)
flat = flatten(layer3)
fully_con = fully_connected(flat, num_outputs=128, weights_initializer=initializer)
tf.summary.histogram('fully_con', fully_con)
output = fully_connected(fully_con, num_outputs=n_outputs, activation_fn=None, weights_initializer=initializer)
tf.summary.histogram('output', output)
params = {v.name[len(cur_scope.name): ]: v for v in tf.get_collection(key=tf.GraphKeys.TRAINABLE_VARIABLES, scope=cur_scope.name)}
return params, output
def get_sample(batch_size):
perm_batch = np.random.permutation(len(exp_buffer))[:batch_size]
mem = np.array(exp_buffer)[perm_batch]
return mem[:,0], mem[:,1], mem[:,2], mem[:,3], mem[:,4]
def epsilon_greedy(action, step):
p = np.random.random(1).squeeze()
epsilon = max(MIN_EPSILON, MAX_EPSILON - (MAX_EPSILON - MIN_EPSILON) * step/EPS_DELTA_STEPS)
if np.random.rand() < epsilon:
return np.random.randint(n_outputs)
else:
return action
env = gym.make("MsPacman-v0")
n_outputs = env.action_space.n
tf.reset_default_graph()
x = tf.placeholder(tf.float32, shape=X_SHAPE)
y = tf.placeholder(tf.float32, shape=(None,1))
in_training_mode = tf.placeholder(tf.bool)
main_dqn, main_dqn_outputs = dqn(x, 'mainQ', n_outputs)
target_dqn, target_dqn_outputs = dqn(x, 'targetQ', n_outputs)
x_action = tf.placeholder(tf.int32, shape=(None,))
q_action = tf.reduce_sum(target_dqn_outputs * tf.one_hot(x_action, n_outputs), axis=-1, keep_dims=True)
copy_operation = [tf.assign(main_name, target_dqn[var_name]) for var_name, main_name in main_dqn.items()]
copy_target_to_main = tf.group(*copy_operation)
loss = tf.reduce_mean(tf.square(y-q_action))
optimizer = tf.train.AdamOptimizer(LEARNING_RATE)
training_operation = optimizer.minimize(loss)
init = tf.global_variables_initializer()
loss_summary = tf.summary.scalar('LOSS', loss)
merge_summary = tf.summary.merge_all()
with tf.Session() as session:
init.run()
for i in range(NUM_EPISODES):
done = False
obs = env.reset()
env.render()
epoch = 0
episodic_reward = 0
actions_counter = Counter()
episodic_loss = []
while not done:
env.render()
obs = preprocess_image(obs)
actions = main_dqn_outputs.eval(feed_dict={x:[obs], in_training_mode:False})
action = np.argmax(actions, axis=-1)
actions_counter[str(action)] += 1
action = epsilon_greedy(action, global_step)
next_obs, reward, done, _ = env.step(action)
exp_buffer.append([obs, action, preprocess_image(next_obs), reward, done])
if global_step % STEPS_TRAIN == 0 and global_step > START_STEPS:
o_obs, o_act, o_next_obs, o_reward, o_done = get_sample(BATCH_SIZE)
o_obs = [x for x in o_obs]
o_next_obs = [x for x in o_next_obs]
next_act = main_dqn_outputs.eval(feed_dict={x:o_next_obs, in_training_mode:False})
y_batch = o_reward + DISCOUNT_FACTOR * np.max(next_act, axis=-1) * (1-o_done)
train_loss, _ = session.run([loss, training_operation], feed_dict={x:o_obs, y:np.expand_dims(y_batch, axis=-1),
x_action: o_act, in_training_mode:True})
episodic_loss.append(train_loss)
if (global_step+1)%COPY_STEPS == 0 and global_step > START_STEPS:
copy_target_to_main.run()
obs=next_obs
epoch+=1
global_step+=1
episodic_reward+=reward
episodic_reward+=reward