-
Notifications
You must be signed in to change notification settings - Fork 0
/
RL.py
182 lines (134 loc) · 5.74 KB
/
RL.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
import tensorflow as tf
import cv2 #read in pixel data
import pong #our class
import numpy as np #math
import random #random
from collections import deque #queue data structure. fast appends. and pops. replay memory
#hyper params
ACTIONS = 3 #up,down, stay
#define our learning rate
GAMMA = 0.99
#for updating our gradient or training over time
INITIAL_EPSILON = 1.0
FINAL_EPSILON = 0.05
#how many frames to anneal epsilon
EXPLORE = 500000
OBSERVE = 50000
#store our experiences, the size of it
REPLAY_MEMORY = 500000
#batch size to train on
BATCH = 100
#create tensorflow graph
def createGraph():
#first convolutional layer. bias vector
#creates an empty tensor with all elements set to zero with a shape
W_conv1 = tf.Variable(tf.zeros([8, 8, 4, 32]))
b_conv1 = tf.Variable(tf.zeros([32]))
W_conv2 = tf.Variable(tf.zeros([4, 4, 32, 64]))
b_conv2 = tf.Variable(tf.zeros([64]))
W_conv3 = tf.Variable(tf.zeros([3, 3, 64, 64]))
b_conv3 = tf.Variable(tf.zeros([64]))
W_fc4 = tf.Variable(tf.zeros([3136, 784]))
b_fc4 = tf.Variable(tf.zeros([784]))
W_fc5 = tf.Variable(tf.zeros([784, ACTIONS]))
b_fc5 = tf.Variable(tf.zeros([ACTIONS]))
#input for pixel data
s = tf.placeholder("float", [None, 84, 84, 4])
#Computes rectified linear unit activation fucntion on a 2-D convolution given 4-D input and filter tensors. and
conv1 = tf.nn.relu(tf.nn.conv2d(s, W_conv1, strides = [1, 4, 4, 1], padding = "VALID") + b_conv1)
conv2 = tf.nn.relu(tf.nn.conv2d(conv1, W_conv2, strides = [1, 2, 2, 1], padding = "VALID") + b_conv2)
conv3 = tf.nn.relu(tf.nn.conv2d(conv2, W_conv3, strides = [1, 1, 1, 1], padding = "VALID") + b_conv3)
conv3_flat = tf.reshape(conv3, [-1, 3136])
fc4 = tf.nn.relu(tf.matmul(conv3_flat, W_fc4) + b_fc4)
fc5 = tf.matmul(fc4, W_fc5) + b_fc5
return s, fc5
#deep q network. feed in pixel data to graph session
def trainGraph(inp, out, sess):
#to calculate the argmax, we multiply the predicted output with a vector with one value 1 and rest as 0
argmax = tf.placeholder("float", [None, ACTIONS])
gt = tf.placeholder("float", [None]) #ground truth
#action
action = tf.reduce_sum(tf.multiply(out, argmax), reduction_indices = 1)
#cost function we will reduce through backpropagation
cost = tf.reduce_mean(tf.square(action - gt))
#optimization fucntion to reduce our minimize our cost function
train_step = tf.train.AdamOptimizer(1e-6).minimize(cost)
#initialize our game
game = pong.PongGame()
#create a queue for experience replay to store policies
D = deque()
#intial frame
frame = game.getPresentFrame()
#convert rgb to gray scale for processing
frame = cv2.cvtColor(cv2.resize(frame, (84, 84)), cv2.COLOR_BGR2GRAY)
#binary colors, black or white
ret, frame = cv2.threshold(frame, 1, 255, cv2.THRESH_BINARY)
#stack frames, that is our input tensor
inp_t = np.stack((frame, frame, frame, frame), axis = 2)
#saver
saver = tf.train.Saver()
sess.run(tf.initialize_all_variables())
t = 0
epsilon = INITIAL_EPSILON
#training time
while(1):
#output tensor
out_t = out.eval(feed_dict = {inp : [inp_t]})[0]
#argmax function
argmax_t = np.zeros([ACTIONS])
#
if(random.random() <= epsilon):
maxIndex = random.randrange(ACTIONS)
else:
maxIndex = np.argmax(out_t)
argmax_t[maxIndex] = 1
if epsilon > FINAL_EPSILON:
epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE
#reward tensor if score is positive
reward_t, frame = game.getNextFrame(argmax_t)
#get frame pixel data
frame = cv2.cvtColor(cv2.resize(frame, (84, 84)), cv2.COLOR_BGR2GRAY)
ret, frame = cv2.threshold(frame, 1, 255, cv2.THRESH_BINARY)
frame = np.reshape(frame, (84, 84, 1))
#new input tensor
inp_t1 = np.append(frame, inp_t[:, :, 0:3], axis = 2)
#add our input tensor, argmax tensor, reward and updated input tensor tos tack of experiences
D.append((inp_t, argmax_t, reward_t, inp_t1))
#if we run out of replay memory, make room
if len(D) > REPLAY_MEMORY:
D.popleft()
#training iteration
if t > OBSERVE:
#get values from our replay memory
minibatch = random.sample(D, BATCH)
inp_batch = [d[0] for d in minibatch]
argmax_batch = [d[1] for d in minibatch]
reward_batch = [d[2] for d in minibatch]
inp_t1_batch = [d[3] for d in minibatch]
gt_batch = []
out_batch = out.eval(feed_dict = {inp : inp_t1_batch})
#add values to our batch
for i in range(0, len(minibatch)):
gt_batch.append(reward_batch[i] + GAMMA * np.max(out_batch[i]))
#train on that
train_step.run(feed_dict = {
gt : gt_batch,
argmax : argmax_batch,
inp : inp_batch
})
#update our input tensor the the next frame
inp_t = inp_t1
t = t+1
#print our where wer are after saving where we are
if t % 10000 == 0:
saver.save(sess, './' + 'pong' + '-dqn', global_step = t)
print("TIMESTEP", t, "/ EPSILON", epsilon, "/ ACTION", maxIndex, "/ REWARD", reward_t, "/ Q_MAX %e" % np.max(out_t))
def main():
#create session
sess = tf.InteractiveSession()
#input layer and output layer by creating graph
inp, out = createGraph()
#train our graph on input and output with session variables
trainGraph(inp, out, sess)
if __name__ == "__main__":
main()