-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_DQN_frame_skip_stack.py
346 lines (262 loc) · 9.61 KB
/
2_DQN_frame_skip_stack.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
# Deep Q-Network Algorithm
# Import modules
import tensorflow as tf
import pygame
import random
import numpy as np
import matplotlib.pyplot as plt
import datetime
import time
import cv2
# Import game
import sys
sys.path.append("DQN_GAMES/")
import pong as game
import dot
import dot_test
import tetris
import wormy
import breakout
class DQN_skipping_stacking:
def __init__(self):
# Game Information
self.algorithm = 'DQN_skipping_stacking'
self.game_name = game.ReturnName()
# Get parameters
self.progress = ''
self.Num_action = game.Return_Num_Action()
# Initial parameters
self.Num_Exploration = 50000
self.Num_Training = 500000
self.Num_Testing = 100000
self.learning_rate = 0.00025
self.gamma = 0.99
self.first_epsilon = 1.0
self.final_epsilon = 0.1
self.epsilon = self.first_epsilon
self.Num_plot_episode = 50
self.step = 1
self.score = 0
self.episode = 1
# Lists for plotting episode - average score
self.plot_x = []
self.plot_y = []
# Save test score
self.test_score = []
# date - hour - minute of training time
self.date_time = str(datetime.date.today()) + '_' + \
str(datetime.datetime.now().hour) + '_' + \
str(datetime.datetime.now().minute)
# parameters for skipping and stacking
self.state_set = []
self.Num_skipping = 4
self.Num_stacking = 4
# Parameters for network
self.img_size = 80
self.Num_colorChannel = 1
self.first_conv = [8,8,self.Num_stacking * self.Num_colorChannel,32]
self.second_conv = [4,4,32,64]
self.third_conv = [3,3,64,64]
self.first_dense = [10*10*64, 512]
self.second_dense = [512, self.Num_action]
self.GPU_fraction = 0.2
# Initialize Network
self.input, self.output = self.network()
self.train_step, self.action_target, self.y_target = self.loss_and_train()
self.sess = self.init_sess()
def main(self):
# Define game state
game_state = game.GameState()
# Initialization
state = self.initialization(game_state)
stacked_state = self.skip_and_stack_frame(state)
while True:
# Get progress:
self.progress = self.get_progress()
# Select action
action = self.select_action(stacked_state)
# Take action and get info. for update
next_state, reward, terminal = game_state.frame_step(action)
next_state = self.reshape_input(next_state)
stacked_next_state = self.skip_and_stack_frame(next_state)
# Training!
if self.progress == 'Training':
# Training
self.train(stacked_state, action, reward, stacked_next_state, terminal)
# Plotting
self.plotting()
# Update former info.
stacked_state = stacked_next_state
self.score += reward
self.step += 1
# If game is over (terminal)
if terminal:
stacked_state = self.if_terminal(game_state)
# Finished!
if self.progress == 'Finished':
print('Finished!')
avg_test_score = str(sum(self.test_score) / len(self.test_score))
print('Average Test score: ' + avg_test_score)
plt.savefig('./Plot/' + self.date_time + '_' +self.algorithm + '_' + self.game_name + avg_test_score + '.png')
break
def init_sess(self):
# Initialize variables
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = self.GPU_fraction
sess = tf.InteractiveSession(config=config)
init = tf.global_variables_initializer()
sess.run(init)
return sess
def initialization(self, game_state):
action = np.zeros([self.Num_action])
state, _, _ = game_state.frame_step(action)
state = self.reshape_input(state)
for i in range(self.Num_skipping * self.Num_stacking):
self.state_set.append(state)
return state
def skip_and_stack_frame(self, state):
self.state_set.append(state)
state_in = np.zeros((self.img_size, self.img_size, self.Num_stacking))
# Stack the frame according to the number of skipping frame
for stack_frame in range(self.Num_stacking):
state_in[:,:,stack_frame] = self.state_set[-1 - (self.Num_skipping * stack_frame)]
del self.state_set[0]
state_in = np.uint8(state_in)
return state_in
def get_progress(self):
progress = ''
if self.step <= self.Num_Exploration:
progress = 'Exploring'
elif self.step <= self.Num_Exploration + self.Num_Training:
progress = 'Training'
elif self.step <= self.Num_Exploration + self.Num_Training + self.Num_Testing:
progress = 'Testing'
else:
progress = 'Finished'
return progress
# Resize and make input as grayscale
def reshape_input(self, state):
state_out = cv2.resize(state, (self.img_size, self.img_size))
if self.Num_colorChannel == 1:
state_out = cv2.cvtColor(state_out, cv2.COLOR_BGR2GRAY)
state_out = np.reshape(state_out, (self.img_size, self.img_size))
return state_out
# Convolution and pooling
def conv2d(self, x, w, stride):
return tf.nn.conv2d(x,w,strides=[1, stride, stride, 1], padding='SAME')
# Get Variables
def conv_weight_variable(self, name, shape):
return tf.get_variable(name, shape = shape, initializer = tf.contrib.layers.xavier_initializer_conv2d())
def weight_variable(self, name, shape):
return tf.get_variable(name, shape = shape, initializer = tf.contrib.layers.xavier_initializer())
def bias_variable(self, name, shape):
return tf.get_variable(name, shape = shape, initializer = tf.contrib.layers.xavier_initializer())
def network(self):
# Input
x_image = tf.placeholder(tf.float32, shape = [None,
self.img_size,
self.img_size,
self.Num_stacking * self.Num_colorChannel])
x_normalize = (x_image - (255.0/2)) / (255.0/2)
with tf.variable_scope('network'):
# Convolution variables
w_conv1 = self.conv_weight_variable('w_conv1', self.first_conv)
b_conv1 = self.bias_variable('b_conv1',[self.first_conv[3]])
w_conv2 = self.conv_weight_variable('w_conv2',self.second_conv)
b_conv2 = self.bias_variable('b_conv2',[self.second_conv[3]])
w_conv3 = self.conv_weight_variable('w_conv3',self.third_conv)
b_conv3 = self.bias_variable('b_conv3',[self.third_conv[3]])
# Densely connect layer variables
w_fc1 = self.weight_variable('w_fc1',self.first_dense)
b_fc1 = self.bias_variable('b_fc1',[self.first_dense[1]])
w_fc2 = self.weight_variable('w_fc2',self.second_dense)
b_fc2 = self.bias_variable('b_fc2',[self.second_dense[1]])
# Network
h_conv1 = tf.nn.relu(self.conv2d(x_normalize, w_conv1, 4) + b_conv1)
h_conv2 = tf.nn.relu(self.conv2d(h_conv1, w_conv2, 2) + b_conv2)
h_conv3 = tf.nn.relu(self.conv2d(h_conv2, w_conv3, 1) + b_conv3)
h_pool3_flat = tf.reshape(h_conv3, [-1, self.first_dense[0]])
h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, w_fc1)+b_fc1)
output = tf.matmul(h_fc1, w_fc2) + b_fc2
return x_image, output
def loss_and_train(self):
# Loss function and Train
action_target = tf.placeholder(tf.float32, shape = [None, self.Num_action])
y_target = tf.placeholder(tf.float32, shape = [None])
y_prediction = tf.reduce_sum(tf.multiply(self.output, action_target), reduction_indices = 1)
Loss = tf.reduce_mean(tf.square(y_prediction - y_target))
train_step = tf.train.AdamOptimizer(learning_rate = self.learning_rate, epsilon = 1e-02).minimize(Loss)
return train_step, action_target, y_target
def select_action(self, stacked_state):
action = np.zeros([self.Num_action])
action_index = 0
# Choose action
if self.progress == 'Exploring':
# Choose random action
action_index = random.randint(0, self.Num_action-1)
action[action_index] = 1
elif self.progress == 'Training':
if random.random() < self.epsilon:
# Choose random action
action_index = random.randint(0, self.Num_action-1)
action[action_index] = 1
else:
# Choose greedy action
Q_value = self.output.eval(feed_dict={self.input: [stacked_state]})
action_index = np.argmax(Q_value)
action[action_index] = 1
# Decrease epsilon while training
if self.epsilon > self.final_epsilon:
self.epsilon -= self.first_epsilon/self.Num_Training
elif self.progress == 'Testing':
# Choose greedy action
Q_value = self.output.eval(feed_dict={self.input: [stacked_state]})
action_index = np.argmax(Q_value)
action[action_index] = 1
self.epsilon = 0
return action
def train(self, stacked_state, action, reward, stacked_next_state, terminal):
# Get y_prediction
y = []
Q = self.output.eval(feed_dict = {self.input: [stacked_next_state]})
# Get target values
if terminal == True:
y.append(reward)
else:
y.append(reward + self.gamma * np.max(Q))
self.train_step.run(feed_dict = {self.action_target: [action],
self.y_target: y,
self.input: [stacked_state]})
def plotting(self):
# Plotting episode - average score
if len(self.plot_x) % self.Num_plot_episode == 0 and len(self.plot_x) != 0 and self.progress != 'Exploring':
plt.xlabel('Episode')
plt.ylabel('Score')
plt.title(self.algorithm)
plt.grid(True)
plt.plot(np.average(self.plot_x), np.average(self.plot_y), hold = True, marker = '*', ms = 5)
plt.draw()
plt.pause(0.000001)
self.plot_x = []
self.plot_y = []
def if_terminal(self, game_state):
# Show Progress
print('Step: ' + str(self.step) + ' / ' +
'Episode: ' + str(self.episode) + ' / ' +
'Progress: ' + self.progress + ' / ' +
'Epsilon: ' + str(self.epsilon) + ' / ' +
'Score: ' + str(self.score))
if self.progress == 'Testing':
self.test_score.append(self.score)
if self.progress != 'Exploring':
self.plot_x.append(self.episode)
self.plot_y.append(self.score)
self.episode += 1
self.score = 0
# If game is finished, initialize the state
state = self.initialization(game_state)
stacked_state = self.skip_and_stack_frame(state)
return stacked_state
if __name__ == '__main__':
agent = DQN_skipping_stacking()
agent.main()