-
Notifications
You must be signed in to change notification settings - Fork 1
/
dqn.py
263 lines (225 loc) · 8.23 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
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
import math
import random
import time
import pdb
import numpy as np
import pandas as pd
import tensorflow as tf
import gym
import argparse
from collections import OrderedDict
##---------- Tensorflow graphs
def main_network(state, action, td_target, alpha=0.1, n_hidden=100):
"""Build TF computation graph"""
weights = {
'hidden': tf.Variable(tf.random_uniform([4, n_hidden], 0, 0.01)),
'output': tf.Variable(tf.random_uniform([n_hidden, 2], 0, 0.01))
}
biases = {
'hidden': tf.Variable(tf.random_uniform([n_hidden], 0, 0.01)),
'output': tf.Variable(tf.random_uniform([2], 0, 0.01))
}
hidden = tf.nn.relu(tf.matmul(state, weights['hidden']) + biases['hidden'])
q = tf.matmul(hidden, weights['output']) + biases['output']
# Calculate loss
qa = tf.reshape(tf.gather_nd(q, action), [-1, 1])
loss = tf.reduce_mean(tf.square(td_target - qa)/2)
opt = tf.train.AdamOptimizer(learning_rate=alpha).minimize(loss)
return opt, q, loss
def target_network(state, reward, is_terminal, n_hidden=100, gamma=0.99):
"""Build network to compute TD target"""
weights_target = {
'hidden': tf.Variable(tf.random_uniform([4, n_hidden], 0, 0.01)),
'output': tf.Variable(tf.random_uniform([n_hidden, 2], 0, 0.01))
}
biases_target = {
'hidden': tf.Variable(tf.random_uniform([n_hidden], 0, 0.01)),
'output': tf.Variable(tf.random_uniform([2], 0, 0.01))
}
hidden = tf.nn.relu(tf.matmul(state, weights_target['hidden']) + biases_target['hidden'])
q = tf.matmul(hidden, weights_target['output']) + biases_target['output']
q_max = tf.reshape(tf.reduce_max(q, reduction_indices=[1]), [-1, 1])
target = reward + gamma * tf.multiply(is_terminal, q_max)
return target
def init_vars():
state = tf.placeholder("float", [None, 4])
action = tf.placeholder(tf.int32, [None, 2])
td_target = tf.placeholder("float", [None, 1])
reward = tf.placeholder("float", [None, 1])
is_terminal = tf.placeholder("float", [None, 1])
return state, action, td_target, reward, is_terminal
def update_target_net(tf_vars, sess):
"""Copy network weights from main net to target net"""
# Build list of copy operations
num_vars = int(len(tf_vars)/2) # 4 in each net (2 weights, 2 biases)
op_list = []
for idx, var in enumerate(tf_vars[0:num_vars]):
op_list.append(tf_vars[idx+num_vars].assign(var.value()))
# Run the TF operations to copy variables
for op in op_list:
sess.run(op)
##-------- Helper functions
def epsilon_greedy(action_value, epsilon, env):
"""Apply epsilon greedy policy to given action value"""
if random.uniform(0, 1) <= epsilon:
return env.action_space.sample() # random action
else:
return np.argmax(action_value) # best action
##------- Buffer
class Buffer(object):
def __init__(self, min_buffer_size, max_buffer_size):
self.min_size = min_buffer_size
self.max_size = max_buffer_size
self.buffer = []
def update(self, s, a, r, s_prime, term):
"""Add new observations to buffer and truncate if too big"""
if len(self.buffer) >= self.max_size:
self.buffer = self.buffer[1:]
self.buffer.append((np.reshape(s, (-1, 4)), a, r, np.reshape(s_prime, (-1, 4)), term))
def check_size(self):
"""Check if buffer is large enough to run a batch"""
if len(self.buffer) > self.min_size:
return True
return False
def random_sample(self, state, action, reward, is_terminal):
"""Random sample from buffer"""
sample = np.reshape(np.array(random.sample(self.buffer, self.min_size)), [self.min_size, 5])
act = sample[:,1].reshape(-1,1)
act = np.append(np.arange(len(act)).reshape(-1, 1), act, axis=1)
sample_dict_opt = {
state: np.stack(sample[:,0], axis=0).reshape(-1,4),
action: act
}
sample_dict_target = {
state: np.stack(sample[:,3], axis=0).reshape(-1,4),
reward: np.stack(sample[:,2], axis=0).reshape(-1, 1),
is_terminal: np.stack(sample[:,4], axis=0).reshape(-1, 1)
}
return sample_dict_opt, sample_dict_target
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--run', default='')
args = parser.parse_args()
if args.run == "train":
print("Retraining model...")
n_episodes = 2000
max_eps_length = 300
reload_model = False
save_data = True
else:
print("Testing model")
n_episodes = 1
max_eps_length = 0
reload_model = True
save_data = False
# Training parameters
print_iters = 200
test_steps = 50
test_freq = 20
avg_steps = 1
episodes_counter = 0
min_buffer_size = 128
max_buffer_size = 2000
gamma = 0.99
alpha = 0.001
epsilon = 0.05
##--------------------- Initialize environment and TF graph
env = gym.make('CartPole-v0')
mean_length_list, mean_return_list, loss_list = [], [], []
state, action, td_target, reward, is_terminal = init_vars()
opt, q, loss = main_network(state, action, td_target, alpha=alpha)
target = target_network(state, reward, is_terminal)
init = tf.global_variables_initializer()
saver = tf.train.Saver(write_version=tf.train.SaverDef.V1)
exp_buffer = Buffer(min_buffer_size, max_buffer_size)
copy_counter = 0
with tf.Session() as sess:
sess.run(init)
for episode_i in range(n_episodes):
t0 = time.time()
end = False
episode_return = 0
copy_counter += 1
s = env.reset() # initial state
return_list, length_list = [], []
l_total = 0
if reload_model:
print("Reloading model...")
folder = '../../models/part1/target_q'
saver = tf.train.import_meta_graph(folder+'/tf_model.meta')
saver.restore(sess, tf.train.latest_checkpoint(folder+'/'))
all_vars = tf.get_collection('vars')
#------------- Perform updates and sample experiences (epsilon-greedy)
for t in range(max_eps_length):
q_now = sess.run(q, feed_dict={state: s.reshape(1, 4)})
a = epsilon_greedy(q_now, epsilon, env)
s_prime, _, done, info = env.step(a)
if done:
r = -1
end = True
term = 0
else:
r = 0
term = 1
# Update buffer and perform mini-batch Q update if enough examples
exp_buffer.update(s, a, r, s_prime, term)
if exp_buffer.check_size():
opt_dict, targ_dict = exp_buffer.random_sample(state, action, reward, is_terminal)
targ = sess.run(target, feed_dict=targ_dict)
opt_dict[td_target] = targ
_, l = sess.run([opt, loss], feed_dict=opt_dict)
l_total += l
# Update target network parameters
if copy_counter == 5:
train_vars = tf.trainable_variables() # 1st 4 are main net, 2nd 4 are target net
update_target_net(train_vars, sess)
copy_counter = 0
# Either end or continue from next state
if end:
if episode_i % test_freq == 0:
loss_list.append(l_total/t)
break
s = s_prime
#----------------------------------------------------------------------------
# --------- Evaluate performance over test_steps episodes (using greedy policy)
if episode_i % test_freq == 0:
for i in range(test_steps):
s = env.reset()
for t in range(300):
q_now = sess.run(q, feed_dict={state: s.reshape(1, 4)})
a = np.argmax(q_now)
s_prime, _, done, info = env.step(a)
if done:
episode_length = t + 1
episode_return = -1 * gamma ** t
length_list.append(episode_length)
return_list.append(episode_return)
break
s = s_prime
mean_length = np.mean(np.array(length_list))
mean_return = np.mean(np.array(return_list))
std_length = np.std(np.array(length_list))
std_return = np.std(np.array(return_list))
mean_length_list.append(mean_length)
mean_return_list.append(mean_return)
# save_path = saver.save(sess, '../../models/part1/target_q/tf_model')
# --------------------------------------------------------------------------
if episode_i % print_iters == 0:
print_tuple = (test_steps, mean_length, mean_return, std_length, std_return)
print("#---After %d test episodes: Mean length = %f, mean return = %f, sd length = %f, sd return = %f" % print_tuple)
episodes_counter += 1
sess.close()
# Save final results to CSV file
if save_data:
output_data = OrderedDict()
output_data['episode'] = range(0, n_episodes, test_freq)
output_data['length'] = mean_length_list
output_data['return'] = mean_return_list
output_data['loss'] = loss_list
df = pd.DataFrame.from_dict(output_data)
df.to_csv('results/target_nn_results.csv', index=False)
if __name__ == "__main__":
main()