-
Notifications
You must be signed in to change notification settings - Fork 16
/
DRQN_Fully.py
344 lines (260 loc) · 11.6 KB
/
DRQN_Fully.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
import sys
from typing import Dict, List, Tuple
import gym
import collections
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
# Q_network
class Q_net(nn.Module):
def __init__(self, state_space=None,
action_space=None):
super(Q_net, self).__init__()
# space size check
assert state_space is not None, "None state_space input: state_space should be selected."
assert action_space is not None, "None action_space input: action_space should be selected."
self.hidden_space = 64
self.state_space = state_space
self.action_space = action_space
self.Linear1 = nn.Linear(self.state_space, self.hidden_space)
self.lstm = nn.LSTM(self.hidden_space,self.hidden_space, batch_first=True)
self.Linear2 = nn.Linear(self.hidden_space, self.action_space)
def forward(self, x, h, c):
x = F.relu(self.Linear1(x))
x, (new_h, new_c) = self.lstm(x,(h,c))
x = self.Linear2(x)
return x, new_h, new_c
def sample_action(self, obs, h,c, epsilon):
output = self.forward(obs, h,c)
if random.random() < epsilon:
return random.randint(0,1), output[1], output[2]
else:
return output[0].argmax().item(), output[1] , output[2]
def init_hidden_state(self, batch_size, training=None):
assert training is not None, "training step parameter should be dtermined"
if training is True:
return torch.zeros([1, batch_size, self.hidden_space]), torch.zeros([1, batch_size, self.hidden_space])
else:
return torch.zeros([1, 1, self.hidden_space]), torch.zeros([1, 1, self.hidden_space])
class EpisodeMemory():
"""Episode memory for recurrent agent"""
def __init__(self, random_update=False,
max_epi_num=100, max_epi_len=500,
batch_size=1,
lookup_step=None):
self.random_update = random_update # if False, sequential update
self.max_epi_num = max_epi_num
self.max_epi_len = max_epi_len
self.batch_size = batch_size
self.lookup_step = lookup_step
if (random_update is False) and (self.batch_size > 1):
sys.exit('It is recommend to use 1 batch for sequential update, if you want, erase this code block and modify code')
self.memory = collections.deque(maxlen=self.max_epi_num)
def put(self, episode):
self.memory.append(episode)
def sample(self):
sampled_buffer = []
##################### RANDOM UPDATE ############################
if self.random_update: # Random upodate
sampled_episodes = random.sample(self.memory, self.batch_size)
check_flag = True # check if every sample data to train is larger than batch size
min_step = self.max_epi_len
for episode in sampled_episodes:
min_step = min(min_step, len(episode)) # get minimum step from sampled episodes
for episode in sampled_episodes:
if min_step > self.lookup_step: # sample buffer with lookup_step size
idx = np.random.randint(0, len(episode)-self.lookup_step+1)
sample = episode.sample(random_update=self.random_update, lookup_step=self.lookup_step, idx=idx)
sampled_buffer.append(sample)
else:
idx = np.random.randint(0, len(episode)-min_step+1) # sample buffer with minstep size
sample = episode.sample(random_update=self.random_update, lookup_step=min_step, idx=idx)
sampled_buffer.append(sample)
##################### SEQUENTIAL UPDATE ############################
else: # Sequential update
idx = np.random.randint(0, len(self.memory))
sampled_buffer.append(self.memory[idx].sample(random_update=self.random_update))
return sampled_buffer, len(sampled_buffer[0]['obs']) # buffers, sequence_length
def __len__(self):
return len(self.memory)
class EpisodeBuffer:
"""A simple numpy replay buffer."""
def __init__(self):
self.obs = []
self.action = []
self.reward = []
self.next_obs = []
self.done = []
def put(self, transition):
self.obs.append(transition[0])
self.action.append(transition[1])
self.reward.append(transition[2])
self.next_obs.append(transition[3])
self.done.append(transition[4])
def sample(self, random_update=False, lookup_step=None, idx=None) -> Dict[str, np.ndarray]:
obs = np.array(self.obs)
action = np.array(self.action)
reward = np.array(self.reward)
next_obs = np.array(self.next_obs)
done = np.array(self.done)
if random_update is True:
obs = obs[idx:idx+lookup_step]
action = action[idx:idx+lookup_step]
reward = reward[idx:idx+lookup_step]
next_obs = next_obs[idx:idx+lookup_step]
done = done[idx:idx+lookup_step]
return dict(obs=obs,
acts=action,
rews=reward,
next_obs=next_obs,
done=done)
def __len__(self) -> int:
return len(self.obs)
def train(q_net=None, target_q_net=None, episode_memory=None,
device=None,
optimizer = None,
batch_size=1,
learning_rate=1e-3,
gamma=0.99):
assert device is not None, "None Device input: device should be selected."
# Get batch from replay buffer
samples, seq_len = episode_memory.sample()
observations = []
actions = []
rewards = []
next_observations = []
dones = []
for i in range(batch_size):
observations.append(samples[i]["obs"])
actions.append(samples[i]["acts"])
rewards.append(samples[i]["rews"])
next_observations.append(samples[i]["next_obs"])
dones.append(samples[i]["done"])
observations = np.array(observations)
actions = np.array(actions)
rewards = np.array(rewards)
next_observations = np.array(next_observations)
dones = np.array(dones)
observations = torch.FloatTensor(observations.reshape(batch_size,seq_len,-1)).to(device)
actions = torch.LongTensor(actions.reshape(batch_size,seq_len,-1)).to(device)
rewards = torch.FloatTensor(rewards.reshape(batch_size,seq_len,-1)).to(device)
next_observations = torch.FloatTensor(next_observations.reshape(batch_size,seq_len,-1)).to(device)
dones = torch.FloatTensor(dones.reshape(batch_size,seq_len,-1)).to(device)
h_target, c_target = target_q_net.init_hidden_state(batch_size=batch_size, training=True)
q_target, _, _ = target_q_net(next_observations, h_target.to(device), c_target.to(device))
q_target_max = q_target.max(2)[0].view(batch_size,seq_len,-1).detach()
targets = rewards + gamma*q_target_max*dones
h, c = q_net.init_hidden_state(batch_size=batch_size, training=True)
q_out, _, _ = q_net(observations, h.to(device), c.to(device))
q_a = q_out.gather(2, actions)
# Multiply Importance Sampling weights to loss
loss = F.smooth_l1_loss(q_a, targets)
# Update Network
optimizer.zero_grad()
loss.backward()
optimizer.step()
def seed_torch(seed):
torch.manual_seed(seed)
if torch.backends.cudnn.enabled:
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def save_model(model, path='default.pth'):
torch.save(model.state_dict(), path)
if __name__ == "__main__":
# Env parameters
model_name = "DRQN_POMDP_Random_FOMDP"
env_name = "CartPole-v1"
seed = 1
exp_num = 'SEED'+'_'+str(seed)
# Set gym environment
env = gym.make(env_name)
if torch.cuda.is_available():
device = torch.device("cuda")
# Set the seed
np.random.seed(seed)
random.seed(seed)
seed_torch(seed)
env.seed(seed)
# default `log_dir` is "runs" - we'll be more specific here
writer = SummaryWriter('runs/'+env_name+"_"+model_name+"_"+exp_num)
# Set parameters
batch_size = 8
learning_rate = 1e-3
buffer_len = int(100000)
min_epi_num = 16 # Start moment to train the Q network
episodes = 650
print_per_iter = 20
target_update_period = 4
eps_start = 0.1
eps_end = 0.001
eps_decay = 0.995
tau = 1e-2
max_step = 2000
# DRQN param
random_update = True# If you want to do random update instead of sequential update
lookup_step = 10 # If you want to do random update instead of sequential update
max_epi_len = 128
max_epi_step = max_step
# Create Q functions
Q = Q_net(state_space=env.observation_space.shape[0],
action_space=env.action_space.n).to(device)
Q_target = Q_net(state_space=env.observation_space.shape[0],
action_space=env.action_space.n).to(device)
Q_target.load_state_dict(Q.state_dict())
# Set optimizer
score = 0
score_sum = 0
optimizer = optim.Adam(Q.parameters(), lr=learning_rate)
epsilon = eps_start
episode_memory = EpisodeMemory(random_update=random_update,
max_epi_num=100, max_epi_len=600,
batch_size=batch_size,
lookup_step=lookup_step)
# Train
for i in range(episodes):
s = env.reset()
obs = s # Use only Position of Cart and Pole
done = False
episode_record = EpisodeBuffer()
h, c = Q.init_hidden_state(batch_size=batch_size, training=False)
for t in range(max_step):
# Get action
a, h, c = Q.sample_action(torch.from_numpy(obs).float().to(device).unsqueeze(0).unsqueeze(0),
h.to(device), c.to(device),
epsilon)
# Do action
s_prime, r, done, _ = env.step(a)
obs_prime = s_prime
# make data
done_mask = 0.0 if done else 1.0
episode_record.put([obs, a, r/100.0, obs_prime, done_mask])
obs = obs_prime
score += r
score_sum += r
if len(episode_memory) >= min_epi_num:
train(Q, Q_target, episode_memory, device,
optimizer=optimizer,
batch_size=batch_size,
learning_rate=learning_rate)
if (t+1) % target_update_period == 0:
# Q_target.load_state_dict(Q.state_dict()) <- navie update
for target_param, local_param in zip(Q_target.parameters(), Q.parameters()): # <- soft update
target_param.data.copy_(tau*local_param.data + (1.0 - tau)*target_param.data)
if done:
break
episode_memory.put(episode_record)
epsilon = max(eps_end, epsilon * eps_decay) #Linear annealing
if i % print_per_iter == 0 and i!=0:
print("n_episode :{}, score : {:.1f}, n_buffer : {}, eps : {:.1f}%".format(
i, score_sum/print_per_iter, len(episode_memory), epsilon*100))
score_sum=0.0
save_model(Q, model_name+"_"+exp_num+'.pth')
# Log the reward
writer.add_scalar('Rewards per episodes', score, i)
score = 0
writer.close()
env.close()