-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn_utils.py
316 lines (265 loc) · 10.9 KB
/
dqn_utils.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
# -*- coding: utf-8 -*-
import preprocessor
import policy
from keras.models import load_model
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from io import StringIO, BytesIO
import os
import sys
import copy
import time
NO_OP_STEPS = 30
NO_OF_ITER =100
ITER_EVAL =5e4
MAX_EPISODE_LENGTH = 1e4
NUM_EPISODES =20
ITER_SAVE =5e4
CASE_NORMAL =0
CASE_DOUBLE =1
"""DQN Agent"""
class DQNAgent:
"""
Class to implement DQN
Parameters
------------
q_network :keras.models.Model
The Q-network Model
preprocessor : Preprocessor to use, stacked preprocessor can be used implemente din Baseline
memory : The replay memory
gamma : flaot Discount factor - closer to 1 means not taking into consideration
target_update_freq : float
Frequency to update the target network
num_burn_in : int
Number of samples to fill up the replay memory initially
train_freq :int
How often to update the Q-ntework
batch_size: int
How many samples in each minibatch
"""
def __init__(self,
q_network,
target_q_network,
preprocessor,
memory,
policy,
gamma,
target_update_freq,
num_burn_in,
train_freq,
batch_size,
optimizer,
loss_func,
summary_writer,
checkpoint_dir,
experiment_id,
env_name,
learning_type= CASE_NORMAL):
self.q_network = q_network
self.target_q_network = target_q_network
self.target_q_network.set_weights(self.q_network.get_weights())
self.compile(optimizer, loss_func)
self.preprocessor = preprocessor
self.memory = memory
self.policy = policy
self.gamma = gamma
self.target_update_freq = target_update_freq
self.num_burn_in = num_burn_in
self.train_freq = train_freq
self.batch_size = batch_size
self.summary_writer = summary_writer
self.checkpoint_dir = checkpoint_dir
self.experiment_id = experiment_id
self.env_name = env_name
self.training_reward_seen = 0
self.input_batch = np.zeros([batch_size,] + \
list(q_network.input_shape[-3:]), dtype='float')
self.nextstate_batch = np.zeros([batch_size,] + \
list(q_network.input_shape[-3:]), dtype='float')
self.learning_type = learning_type
def compile(self, optimizer, loss_func):
self.q_network.compile(optimizer,loss_func)
self.target_q_network.compile(optimizer,loss_func)
def q_values(self,state, preproc=None ,network= None):
"""Given a state(or a batch of states)calculate the Q-values. Returns Q-values of the state"""
if preproc is None:
preproc = self.preprocessor
if network is None:
network= self.q_network
return self.q_network.predict(np.expand_dims(preproc.network_process_state(state),0))
def update_policy(self, itr):
"""Update Policy
Sample a minibatch , calculate the target values, update your network, and then update your target values.
"""
samples = self.memory.sample(self.batch_size)
num_samples =len(samples)
assert(num_samples == self.batch_size)
if self.learning_type == CASE_DOUBLE:
if np.random.uniform() < 0.5:
temp = self.q_network
self.q_network = self.target_q_network
self.target_q_network = temp
for i in range(num_samples):
state, _, _, nextstate,_ = samples[i]
self.input_batch[i,...] =state
self.nextstate_batch[i,...] = nextstate
self.input_batch = self.preprocessor.network_process_state(self.input_batch)
self.nextstate_batch = self.preprocessor.network_process_state(self.nextstate_batch)
target_batch = self.q_network.predict(self.input_batch)
nextstate_q_values = self.target_q_network.predict(self.nextstate_batch)
if self.learning_type == CASE_DOUBLE:
nextstate_q_values_live_network = self.q_network.predict(self.nextstate_batch)
for i in range(num_samples):
_,action,reward,_,is_terminal = samples[i]
if is_terminal:
target_batch[i,action] = reward
else:
if self.learning_type == CASE_DOUBLE:
selected_action = np.argmax(nextstate_q_values_live_network[i].flatten())
target_batch[i,action]= reward + self.gamma * nextstate_q_values[i,selected_action]
else:
target_batch[i,action] = reward + self.gamma * np.max(nextstate_q_values[i])
self.training_reward_seen += sum([i[2] for i in samples])
if iter % NO_OF_ITER:
summary_list =[]
for in range(3):
s = BytesIO()
plt.imsave(s, np.mean(self.input_batch[k],axis=-1),format='png')
img_sum = tf.Summary.Image(
encoded_image_string=s.getvalue(),
height=self.input_batch[k].shape[0],
width=self.input_batch[k].shape[1]
)
img.summaries.append(tf.Summary.Value(
tag ='input/{}'.format(k),image = img_sum))
self.summary_writer.add_summary(tf.Summary(value= summary_list,global_step =itr))
#calculate loss
loss = self.q_network.train_on_batch(self.input_batch,target_batch)
return loss
def fit(self,env,num_iterations,max_episode_length=None):
#fill the replaymemory
self.preprocessor.reset()
env_current_state =env.reset()
# env_current_state = self.run_no_op_steps(env)
env_current_state = self.preprocessor.memory_process_state(env_current_state)
env = copy.deepcopy(env)
value_fn = np.random.random((env.action_space.n,))
for _ in range(self.num_burn_in):
env_current_state = self.push_replay_memory(
env_current_state,env,
policy.UniformRandomPolicy(env.action_space.n),
is_training=False, value_fn= value_fn)
start_time =time.time()
for itr in range(num_iterations):
value_fn = self.q_values(env_current_state,network = self.q_network)
env_current_state = self.push_replay_memory(
env_current_state,env,
self.policy,
is_training=True, value_fn= value_fn)
if itr % self.target_update_freq ==0:
self.target_q_network.set_weights(self.q_network.get_weights())
if itr % self.train_freq == 0:
loss = self.update_policy(itr)
if itr % NO_OF_ITER ==0:
print('Iteration {:}: Loss {:.12f} ({:.4f} it/sec)'
'(reward seen : {}'.format(itr,
loss,
NO_OF_ITER * 1.0/(time.time() -start_time),
self.training_reward_seen)
start_time = time.time()
self.summary_writer.add_summary(tf.Summary(value =[
tf.Summary.Value(
tag = 'loss',
simple_value= loss.item())]),
global_step= itr)
if itr % ITER_EVAL ==0:
self.evaluate(env,NUM_EPISODES,itr,max_episode_length = MAX_EPISODE_LENGTH)
if itr % ITER_SAVE ==0:
self.save(itr)
def push_replay_memory(self, env_current_state,env,policy,is_training, value_fn):
dont_reset = False
env_current_lives = -1
try :
env_current_lives = env.env.ale.lives()
except:
pass
action = policy.select_action(q_values= value_fn, is_training= is_training)
nextstate,reward,is_terminal,debug_info = env.step(action)
if 'ale.lives' in debug_info:
if debug_info['ale.lives'] < env_current_lives:
if not is_terminal:
dont_reset = True
is_terminal =True
reward = self.preprocessor.process_reward(reward)
nextstate = self.preprocessor.memory_process_state(nextstate)
self.memory.append(env_current_state,action, reward,nextstate,is_terminal)
if is_terminal and not dont_reset:
self.preprocessor.reset()
nextstate = env.reset()
nextstate = self.preprocessor.memory_process_state(nextstate)
return nextstate
def save(slef,itr):
filename = "%s/%s_run%d_iter%d.h5" % (self.checkpoint_dir,self.env_name.
self.experiment_id
itr)
self.q_network.save(filename)
def load(self,filename):
self.q_network.save(filename)
def evaluate(self,env, num_episodes, itr , max_episode_length=None,
render = False. render_path='', final_eval= False):
print("Running the evaluation")
preproc = preprocessor.SequentialPreprocessor(
[preprocessor.AtariPreprocessor(
self.q_network.input_shape[1],
self.preprocessor.preprocessor[0].input_image),
preprocessor.PreservePreprocessor(self.q_network.input_shape[-1])])
pol = policy.GreedyEpsilonPolicy(0.05)
all_stats =[]
all_rewards = []
for i in range(num_episodes):
print('Running episode {}'.format(i))
if render:
env = gym.wrappers.Monitor(env,render_path,force= True)
nextstate = env.reset()
preproc.reset()
is_terminal = False
stats ={
'total_reward':0,
'episode_length':0,
'max_q_value':0
}
while not is_terminal and stats['episode_length'] < max_episode_length:
nextstate = preproc.memory_process_state(nextstate)
q_values = self.q_values(nextstate, preproc)
action = pol.select_action(q_values=q_values)
nextstate, reward, is_terminal, _ = env.step(action)
stats['total_reward'] += reward
stats['episode_length'] += 1
stats['max_q_value'] += max(q_values)
all_stats.append(stats)
all_rewards.append(stats['total_reward'])
print('Current mean+ std: {} {}'.format(np.mean(all_rewards),np.std(all_rewards)))
final_stats = {}
if render:
return
if final_eval:
print('Mean reward: {}'.format(np.mean(all_rewards)))
print('Std reward: {}'.format(np.std(all_rewards)))
return
for key in all_stats[0]:
final_stats['mean_' + key] = np.mean([i[key] for i in all_stats]).items()
self.summary_writer.add_summary(tf.Summary(value=[
tf.Summary.Value(tag='eval/{}'.format(key),
simple_value= final_stats['mean_' + key])]),
global_step=itr)
print('Evaluation result: {}'.format(final_stats)
def run_no_op_steps(self,env):
for _ in range(NO_OP_STEPS-1):
_, _, is_terminal, _ = env.step(0)
if is_terminal:
env.reset()
nextstate, _, is_terminal, _ = env.step(0)
if is_terminal:
nextstate = env.reset()
return nextstate