-
Notifications
You must be signed in to change notification settings - Fork 1
/
q_learning_HW.py
288 lines (189 loc) · 6.97 KB
/
q_learning_HW.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
import tensorflow as tf
import cv2
import pong_game as game
import random
import numpy as np
from collections import deque
# Game name.
GAME = 'Pong'
# Number of valid actions.
ACTIONS = 3
# Decay rate of past observations.
GAMMA = 0.99
# Timesteps to observe before training.
OBSERVE = 5000.
# Frames over which to anneal epsilon.
EXPLORE = 250000.
# Final value of epsilon.
FINAL_EPSILON = 0.05
# Starting value of epsilon.
INITIAL_EPSILON = 1.0
# Number of previous transitions to remember in the replay memory.
REPLAY_MEMORY = 300000
# Size of minibatch.
BATCH = 32
# Only select an action every Kth frame, repeat the same action for other frames.
K = 5
# Learning Rate.
Lr = 1e-6
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.01)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.01, shape = shape)
return tf.Variable(initial)
def conv2d(x, W, stride):
return tf.nn.conv2d(x, W, strides = [1, stride, stride, 1], padding = "SAME")
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")
def createNetwork():
# Initialize the network weights and biases.
W_conv1 = weight_variable([8, 8, 4, 32])
b_conv1 = bias_variable([32])
W_conv2 = weight_variable([4, 4, 32, 64])
b_conv2 = bias_variable([64])
W_conv3 = weight_variable([3, 3, 64, 64])
b_conv3 = bias_variable([64])
W_fc1 = weight_variable([1600, 512])
b_fc1 = bias_variable([512])
W_fc2 = weight_variable([512, ACTIONS])
b_fc2 = bias_variable([ACTIONS])
# Input layer.
s = tf.placeholder("float", [None, 80, 80, 4])
# Hidden layers.
h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2)
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3)
h_conv3_flat = tf.reshape(h_conv3, [-1, 1600])
h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1)
# Output layer
readout = tf.matmul(h_fc1, W_fc2) + b_fc2
return s, readout
def get_action_index(readout_t,epsilon,t):
rand_num = np.random.random()
if OBSERVE >t or epsilon > rand_num:
action_index = random.randint(0,2)
else:
action_index = np.argmax(readout_t)
return action_index
def scale_down_epsilon(epsilon,t):
if epsilon > FINAL_EPSILON :
if t > OBSERVE:
epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE
return epsilon
def run_selected_action(a_t,s_t,game_state):
x,r_t,terminal=game_state.frame_step(a_t)
x_t = cv2.cvtColor(cv2.resize(x, (80, 80)), cv2.COLOR_BGR2GRAY)
ret, x_t = cv2.threshold(x_t,1,255,cv2.THRESH_BINARY)
s_t1=np.stack((s_t[:,:,1], s_t[:,:,2], s_t[:,:,3], x_t), axis = 2)
return s_t1,r_t,terminal
def compute_cost(target_q,a_t,q_value):
target_y = tf.reduce_sum(q_value * a_t, axis=1)
cost = tf.reduce_sum(tf.square(target_y - target_q))
return cost
def compute_target_q(next_state_batch,r_batch,readout_j1_batch,minibatch):
terminal= [d[4] for d in minibatch]
target_q_batch=[]
for i in range(len(readout_j1_batch)):
if(terminal[i]==True):
target_q_batch.append(r_batch[i])
else:
target_q_batch.append(r_batch[i]+GAMMA*max(readout_j1_batch[i]))
return target_q_batch
def trainNetwork(s, readout, sess):
# Placeholder for the action.
a = tf.placeholder("float", [None, ACTIONS])
# Placeholder for the target Q value.
y = tf.placeholder("float", [None])
# Compute the loss.
cost = compute_cost(y,a,readout)
# Training operation.
train_step = tf.train.AdamOptimizer(Lr).minimize(cost)
# Open up a game state to communicate with emulator.
game_state = game.GameState()
# Initialize the replay memory.
D = deque()
# Initialize the action vector.
do_nothing = np.zeros(ACTIONS)
do_nothing[0] = 1
# Initialize the state of the game.
x_t, r_0, terminal = game_state.frame_step(do_nothing)
x_t = cv2.cvtColor(cv2.resize(x_t, (80, 80)), cv2.COLOR_BGR2GRAY)
ret, x_t = cv2.threshold(x_t,1,255,cv2.THRESH_BINARY)
s_t = np.stack((x_t, x_t, x_t, x_t), axis = 2)
# Save and load model checkpoints.
saver = tf.train.Saver(max_to_keep = 1000000)
sess.run(tf.initialize_all_variables())
checkpoint = tf.train.get_checkpoint_state("saved_networks_q_learning")
if checkpoint and checkpoint.model_checkpoint_path:
saver.restore(sess, checkpoint.model_checkpoint_path)
print("Successfully loaded:", checkpoint.model_checkpoint_path)
else:
print("Could not find old network weights")
# Initialize the epsilon value for the exploration phase.
epsilon = INITIAL_EPSILON
# Initialize the iteration counter.
t = 0
while True:
# Choose an action epsilon-greedily.
readout_t = readout.eval(feed_dict = {s : [s_t]})[0]
action_index = get_action_index(readout_t,epsilon,t)
a_t = np.zeros([ACTIONS])
a_t[action_index] = 1
# Scale down epsilon during the exploitation phase.
epsilon = scale_down_epsilon(epsilon,t)
#run the selected action and update the replay memeory
for i in range(0, K):
# Run the selected action and observe next state and reward.
s_t1,r_t,terminal = run_selected_action(a_t,s_t,game_state)
# Store the transition in the replay memory D.
D.append((s_t, a_t, r_t, s_t1, terminal))
if len(D) > REPLAY_MEMORY:
D.popleft()
# Start training once the observation phase is over.
if (t > OBSERVE):
# Sample a minibatch to train on.
minibatch = random.sample(D, BATCH)
# Get the batch variables.
s_j_batch = [d[0] for d in minibatch]
a_batch = [d[1] for d in minibatch]
r_batch = [d[2] for d in minibatch]
s_j1_batch = [d[3] for d in minibatch]
# Compute the target Q-Value
readout_j1_batch = readout.eval(feed_dict = {s : s_j1_batch})
target_q_batch = compute_target_q(s_j1_batch,r_batch,readout_j1_batch,minibatch)
# Perform gradient step.
train_step.run(feed_dict = {
y : target_q_batch,
a : a_batch,
s : s_j_batch})
# Update the state.
s_t = s_t1
# Update the number of iterations.
t += 1
# Save a checkpoint every 10000 iterations.
if t % 10000 == 0:
saver.save(sess, 'saved_networks_q_learning/' + GAME + '-dqn', global_step = t)
# Print info.
state = ""
if t <= OBSERVE:
state = "observe"
elif t > OBSERVE and t <= OBSERVE + EXPLORE:
state = "explore"
else:
state = "train"
if t % 1000 == 0:
print("TIMESTEP", t, "/ STATE", state, "/ EPSILON", epsilon, "/ ACTION", action_index, "/ REWARD", r_t, "/ Q_MAX %e" % np.max(readout_t))
def playGame():
# Start an active session.
sess = tf.InteractiveSession()
# Create the network.
s, readout = createNetwork()
# Choose between Q-Learning and Policy Gradient.
s, readout = trainNetwork(s, readout, sess)
def main():
""" Main function """
playGame()
if __name__ == "__main__":
main()