forked from spragunr/deep_q_rl
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathale_agent.py
executable file
·321 lines (248 loc) · 11.1 KB
/
ale_agent.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
"""
The NeuralAgent class wraps a deep Q-network for training and testing
in the Arcade learning environment.
Author: Nathan Sprague
"""
import os
import time
import mxnet as mx
import logging
import numpy as np
import ale_data_set
import sys
sys.setrecursionlimit(10000)
class NeuralAgent(object):
def __init__(self, q_network, epsilon_start, epsilon_min,
epsilon_decay, replay_memory_size, exp_pref,
replay_start_size, update_frequency, rng, double=False):
self.network = q_network
self.epsilon_start = epsilon_start
self.epsilon_min = epsilon_min
self.epsilon_decay = epsilon_decay
self.replay_memory_size = replay_memory_size
self.exp_pref = exp_pref
self.replay_start_size = replay_start_size
self.update_frequency = update_frequency
self.rng = rng
if double:
self.test_epsilon = 0.001
else:
self.test_epsilon = 0.05
self.phi_length = self.network.num_frames
self.image_width = self.network.input_width
self.image_height = self.network.input_height
# CREATE A FOLDER TO HOLD RESULTS
time_str = time.strftime("_%m-%d-%H-%M_", time.gmtime())
self.exp_dir = self.exp_pref + time_str + \
"{}".format(self.network.lr).replace(".", "p") + "_" \
+ "{}".format(self.network.discount).replace(".", "p")
try:
os.stat(self.exp_dir)
except OSError:
os.makedirs(self.exp_dir)
self.num_actions = self.network.num_actions
self.data_set = ale_data_set.DataSet(width=self.image_width,
height=self.image_height,
rng=rng,
max_steps=self.replay_memory_size,
phi_length=self.phi_length,
discount = self.network.discount)
# just needs to be big enough to create phi's
self.test_data_set = ale_data_set.DataSet(width=self.image_width,
height=self.image_height,
rng=rng,
max_steps=self.phi_length * 2,
phi_length=self.phi_length,
discount = self.network.discount)
self.epsilon = self.epsilon_start
if self.epsilon_decay != 0:
self.epsilon_rate = ((self.epsilon_start - self.epsilon_min) /
self.epsilon_decay)
else:
self.epsilon_rate = 0
self.testing = False
self._open_results_file()
self._open_learning_file()
self.episode_counter = 0
self.batch_counter = 0
self.holdout_data = None
# In order to add an element to the data set we need the
# previous state and action and the current reward. These
# will be used to store states and actions.
self.last_img = None
self.last_action = None
# Exponential moving average of runtime performance.
self.steps_sec_ema = 0.
def _open_results_file(self):
logging.info("OPENING " + self.exp_dir + '/results.csv')
self.results_file = open(self.exp_dir + '/results.csv', 'w')
self.results_file.write(\
'epoch,num_episodes,total_reward,reward_per_epoch,mean_q\n')
self.results_file.flush()
def _open_learning_file(self):
self.learning_file = open(self.exp_dir + '/learning.csv', 'w')
self.learning_file.write('mean_loss,epsilon\n')
self.learning_file.flush()
def _update_results_file(self, epoch, num_episodes, holdout_sum):
out = "{},{},{},{},{}\n".format(epoch, num_episodes, self.total_reward,
self.total_reward / float(num_episodes),
holdout_sum)
self.results_file.write(out)
self.results_file.flush()
def _update_learning_file(self):
out = "{},{}\n".format(np.mean(self.loss_averages),
self.epsilon)
self.learning_file.write(out)
self.learning_file.flush()
def start_episode(self, observation):
"""
This method is called once at the beginning of each episode.
No reward is provided, because reward is only available after
an action has been taken.
Arguments:
observation - height x width numpy array
Returns:
An integer action
"""
self.step_counter = 0
self.batch_counter = 0
self.episode_reward = 0
# We report the mean loss for every epoch.
self.loss_averages = []
self.qval_averages = []
self.start_time = time.time()
return_action = self.rng.randint(0, self.num_actions)
self.last_action = return_action
self.last_img = observation
return return_action
def _show_phis(self, phi1, phi2):
import matplotlib.pyplot as plt
for p in range(self.phi_length):
plt.subplot(2, self.phi_length, p+1)
plt.imshow(phi1[p, :, :], interpolation='none', cmap="gray")
plt.grid(color='r', linestyle='-', linewidth=1)
for p in range(self.phi_length):
plt.subplot(2, self.phi_length, p+5)
plt.imshow(phi2[p, :, :], interpolation='none', cmap="gray")
plt.grid(color='r', linestyle='-', linewidth=1)
plt.show()
def step(self, reward, observation):
"""
This method is called each time step.
Arguments:
reward - Real valued reward.
observation - A height x width numpy array
Returns:
An integer action.
"""
self.step_counter += 1
#TESTING---------------------------
if self.testing:
self.episode_reward += reward
action = self._choose_action(self.test_data_set, self.test_epsilon,
observation, np.clip(reward, -1, 1))
#NOT TESTING---------------------------
else:
if len(self.data_set) > self.replay_start_size:
self.epsilon = max(self.epsilon_min,
self.epsilon - self.epsilon_rate)
action = self._choose_action(self.data_set, self.epsilon,
observation,
np.clip(reward, -1, 1))
if self.step_counter % self.update_frequency == 0:
loss = self._do_training()
self.batch_counter += 1
self.loss_averages.append(loss)
else: # Still gathering initial random data...
action = self._choose_action(self.data_set, self.epsilon,
observation,
np.clip(reward, -1, 1))
self.last_action = action
self.last_img = observation
return action
def _choose_action(self, data_set, epsilon, cur_img, reward):
"""
Add the most recent data to the data set and choose
an action based on the current policy.
"""
data_set.add_sample(self.last_img, self.last_action, reward, False)
if self.step_counter >= self.phi_length:
phi = data_set.phi(cur_img)
action, qval = self.network.choose_action(phi, epsilon)
if qval != 0:
self.qval_averages.append(qval)
else:
action = self.rng.randint(0, self.num_actions)
return action
def _do_training(self):
"""
Returns the average loss for the current batch.
May be overridden if a subclass needs to train the network
differently.
"""
imgs, actions, rewards, terminals, R= \
self.data_set.random_batch(
self.network.batch_size)
return self.network.train(imgs, actions, rewards, terminals, R)
def end_episode(self, reward, max_steps, reward_sum, epoch, terminal=True):
"""
This function is called once at the end of an episode.
Arguments:
reward - Real valued reward.
terminal - Whether the episode ended intrinsically
(ie we didn't run out of steps)
Returns:
None
"""
self.episode_reward += reward
self.step_counter += 1
total_time = time.time() - self.start_time
if self.testing:
# If we run out of time, only count the last episode if
# it was the only episode.
if terminal or self.episode_counter == 0:
self.episode_counter += 1
self.total_reward += self.episode_reward
info = "----TESTING----Epoch: %3d | Steps: %6d | Qval: %7.3f | Reward: %4d"\
% (epoch, max_steps, np.mean(self.qval_averages), reward_sum)
else:
# Store the latest sample.
self.data_set.add_sample(self.last_img,
self.last_action,
np.clip(reward, -1, 1),
True)
rho = 0.98
self.steps_sec_ema *= rho
self.steps_sec_ema += (1. - rho) * (self.step_counter/total_time)
info = "Epoch:%3d | Steps:%6d | Speed:%8.2f | Qval:%7.3f | Loss:%8.3f | Reward:%4d" \
% (epoch, max_steps, self.steps_sec_ema, np.mean(self.qval_averages), \
np.mean(self.loss_averages), reward_sum)
#logging.info("steps/second: {:.2f}, avg: {:.2f}".format(
# self.step_counter/total_time, self.steps_sec_ema))
if self.batch_counter > 0:
self._update_learning_file()
# logging.info("average loss: {:.4f}".format(\
# np.mean(self.loss_averages)))
logging.info(info)
def finish_epoch(self, epoch):
net_file = self.exp_dir + '/network_file_' + str(epoch) + '.params'
mx.nd.save(net_file, self.network.policy_exe.arg_dict)
def start_testing(self):
self.testing = True
self.total_reward = 0
self.episode_counter = 0
def finish_testing(self, epoch):
self.testing = False
holdout_size = 3200
if self.holdout_data is None and len(self.data_set) > holdout_size:
imgs, _, _, _, _ = self.data_set.random_batch(holdout_size)
self.holdout_data = imgs[:, :self.phi_length]
holdout_sum = 0
if self.holdout_data is not None:
for i in range(holdout_size):
holdout_sum += np.max(
self.network.q_vals(self.holdout_data[i]))
self._update_results_file(epoch, self.episode_counter,
holdout_sum / holdout_size)
if __name__ == "__main__":
pass