-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_dqn duel.py
418 lines (343 loc) · 15.1 KB
/
agent_dqn duel.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import numpy as np
from collections import deque
import os
import sys
import wandb
wandb.init(
project="usivaraman-train_sample",
config = {
"learning_rate": 1.5e-4,
"batch_size": 32,
"epsidoes" : 100000
}
)
import random
from collections import deque, namedtuple
from itertools import count
from typing import List
import math
import torch
import torch.nn.functional as F
from torch import nn, optim
from torch.utils.tensorboard import SummaryWriter
# import torch.autograd as autograd
from agent import Agent
from dqn_model import DQN
from environment import Environment
"""
you can import any package and define any extra function as you need
"""
torch.manual_seed(595)
np.random.seed(595)
random.seed(595)
# wandb.log({"loss": loss})
# wandb.watch(DQN)
writer = SummaryWriter()
EPISODES = 400000
LEARNING_RATE = 1.5e-4 # alpha
GAMMA = 0.99
BATCH_SIZE = 32
BUFFER_SIZE = 5000
EPSILON = 0.02
EPSILON_END = 0.005
EPS_DECAY = 500
FINAL_EXPL_FRAME = 1000000
TARGET_UPDATE_FREQUENCY = 5000
SAVE_MODEL_AFTER = 5000
DECAY_EPSILON_AFTER = 500
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Transition = namedtuple('Transition', ('state', 'action', 'reward', 'next_state'))
rew_buffer = deque([0.0], maxlen=100)
# USE_CUDA = torch.cuda.is_available()
# Variable = lambda *args, **kwargs: autograd.Variable(*args, **kwargs).cuda() if USE_CUDA else autograd.Variable(*args, **kwargs)
class ReplayBuffer(object):
def __init__(self, capacity: int):
self.buffer = deque([], maxlen=capacity)
def push(self, *args) -> None:
"""Save a transition"""
self.buffer.append(Transition(*args))
def sample(self, batch_size: int) -> List[Transition]:
"""Randomly sample 'batch_size' number of transitions from buffer"""
# samples = random.sample(self.buffer, batch_size)
# for elem in samples:
# self.buffer.remove(elem)
# return samples
samples = []
for _ in range(batch_size):
index = random.randrange(len(self.buffer))
samples.append(self.buffer[index])
del self.buffer[index]
return samples
def __len__(self):
return len(self.buffer)
class Agent_DQN(Agent):
def __init__(self, env, args):
"""
Initialize everything you need here.
For example:
paramters for neural network
initialize Q net and target Q net
parameters for repaly buffer
parameters for q-learning; decaying epsilon-greedy
...
"""
super(Agent_DQN,self).__init__(env)
###########################
# YOUR IMPLEMENTATION HERE #
self.env = env
self.actions = self.env.action_space.n
input_channel = 4
self.Q_net = DQN(input_channel,self.actions).to(device)
self.target_Q_net = DQN(input_channel, self.actions).to(device)
self.target_Q_net.load_state_dict(self.Q_net.state_dict())
self.optimizer = optim.Adam(self.Q_net.parameters(), lr=LEARNING_RATE)
self.Q_net.eval()
self.buffer = ReplayBuffer(BUFFER_SIZE)
self.training_steps = 0
if args.test_dqn:
#you can load your model here
print('loading trained model')
self.Q_net.load_state_dict(torch.load('./vanilla_dueldqn_model.pth.pth'))
###########################
# YOUR IMPLEMENTATION HERE #
def init_game_setting(self):
"""
Testing function will call this function at the begining of new game
Put anything you want to initialize if necessary.
If no parameters need to be initialized, you can leave it as blank.
"""
###########################
# YOUR IMPLEMENTATION HERE #
###########################
pass
def make_action(self, observation: np.ndarray, test: bool =True) -> int:
"""
Return predicted action of your agent
Input:
observation: np.array
stack 4 last preprocessed frames, shape: (84, 84, 4)
Return:
action: int
the predicted action from trained model
"""
###########################
# YOUR IMPLEMENTATION HERE #
# print ("Observation",observation)
# print("Shape :" ,observation.shape)
# state = np.asarray(observation, dtype = np.float32)/255
# # image with HWC-layout (height, width, channels), while Pytorch requires CHW-layout. So we have to do np.transpose(image,(2,0,1)) for HWC->CHW transformation.
# # converting to CHW for pytorch tensor
# state = state.transpose(2,0,1)
# state = torch.from_numpy(state).unsqueeze(0)
# print ("State after processing :",state)
# # Get Q from network/model
# Q_value = self.Q_net.forward(state)
# action = torch.argmax(Q_value,dim=1)[0]
# print("Seleected action :",action)
# action.detach().item()
# print("Selected action after:",action)
if random.random() > EPSILON :
observation = observation/255
observation = observation.transpose(2,0,1)
observation = torch.FloatTensor(np.float32(observation)).unsqueeze(0)
q_value = self.Q_net.forward(observation)
action = q_value.max(1)[1].data[0]
action = int(action.item())
else :
action = random.randrange(4)
# print("Seleected action :",action)
###########################
# return action.detach().item()
return action
def get_eps_greedy_action(self, greedy_action: int, epsilon: float):
"""
Take the deterministic action given by the network and return an epsilon
greedy action.
"""
probability = np.ones(self.actions) * epsilon / self.actions # exploration
probability[greedy_action] += 1 - epsilon # exploitation
return np.random.choice(np.arange(self.actions), p=probability)
# def push(self):
# """ You can add additional arguments as you need.
# Push new data to buffer and remove the old one if the buffer is full.
# Hints:
# -----
# you can consider deque(maxlen = 10000) list
# """
# ###########################
# # YOUR IMPLEMENTATION HERE #
# ###########################
# def replay_buffer(self):
# """ You can add additional arguments as you need.
# Select batch from buffer.
# """
# ###########################
# # YOUR IMPLEMENTATION HERE #
# ###########################
# return
def train(self,no_of_episodes: int = EPISODES):
"""
Implement your training algorithm here
"""
###########################
# YOUR IMPLEMENTATION HERE #
reward_data =[]
epi_data =[]
for epi_num in range(no_of_episodes):
print("Episode ",epi_num)
episode_reward = 0
curr_state = self.env.reset()
for step in count():
# if epi_num == DECAY_EPSILON_AFTER: el
# epsilon = epsilon = (2.5/100)*epsilon
if epi_num > DECAY_EPSILON_AFTER:
# epsilon = np.interp(step, [0, FINAL_EXPL_FRAME], [EPSILON, EPSILON_END])
epsilon = EPSILON_END + (EPSILON - EPSILON_END) * math.exp(-1. * step/EPS_DECAY)
else:
epsilon = EPSILON
action = self.get_eps_greedy_action(self.make_action(curr_state), epsilon)
next_state, reward, done ,trans_prob,info = self.env.step(action)
# print("From Environment")
# print("S,A,R,NS ",curr_state,action,reward,next_state)
# Convert numpy arrays/int to tensors
# curr_state_t = self.format_state(curr_state)
curr_state_t = np.asarray(curr_state, dtype = np.float32)/255
# image with HWC-layout (height, width, channels), while Pytorch requires CHW-layout. So we have to do np.transpose(image,(2,0,1)) for HWC->CHW transformation.
# converting to CHW for pytorch tensor
curr_state_t = curr_state_t.transpose(2,0,1)
curr_state_t = torch.from_numpy(curr_state_t).unsqueeze(0)
next_state_t = np.asarray(next_state, dtype = np.float32)/255
# image with HWC-layout (height, width, channels), while Pytorch requires CHW-layout. So we have to do np.transpose(image,(2,0,1)) for HWC->CHW transformation.
# converting to CHW for pytorch tensor
next_state_t = next_state_t.transpose(2,0,1)
next_state_t = torch.from_numpy(next_state_t).unsqueeze(0)
# next_state_t = self.format_state(next_state)
action_t = torch.tensor([action], device=device)
reward_t = torch.tensor([reward], device=device)
self.buffer.push(curr_state_t, action_t, reward_t, next_state_t)
# print("Current_state,Action,Reward,Next state :")
# print(curr_state_t, action_t, reward_t, next_state_t)
curr_state = next_state
episode_reward += reward
# print("reward :",reward)
# wandb.log({"Episode_number":epi_num, "Reward ":reward})
# Optimize
self.optimize_model()
if done:
rew_buffer.append(episode_reward)
break
print("Mean :",np.mean(rew_buffer))
# wandb.log({"Episode Reward":episode_reward})
# wandb.log({"Episode Reward mean":np.mean(rew_buffer)})
if epi_num % 100 == 0:
# writer.add_scalar("Mean reward(100) vs Episode", np.mean(rew_buffer), epi_num)
reward_data.append(np.mean(rew_buffer))
epi_data.append(epi_num)
data = [[x, y] for (x, y) in zip(epi_data,reward_data)]
table = wandb.Table(data=data, columns = ["epi", "reward"])
wandb.log({"my_custom_plot_id" : wandb.plot.line(table, "epi", "reward",
title="Mean Reward vs Episode Plot")})
# Logging
# writer.add_scalar("Epsilon vs Step", epsilon, step)
# if epi_num % 100 == 0:
# writer.add_scalar("Mean reward(100) vs Episode", np.mean(rew_buffer), epi_num)
# writer.add_scalar(
# "Mean reward(100) vs Training steps",
# np.mean(rew_buffer),
# self.training_steps
# )
if epi_num % TARGET_UPDATE_FREQUENCY == 0:
self.target_Q_net.load_state_dict(self.Q_net.state_dict())
if epi_num % SAVE_MODEL_AFTER == 0:
torch.save(self.Q_net.state_dict(), "vanilla_dueldqn_model_revnov16_3.pth")
torch.save(self.Q_net.state_dict(), "vanilla_dueldqn_model_revnov16_3.pth")
print("Complete")
# writer.flush()
# writer.close()
wandb.finish()
###########################
def optimize_model(self)-> None:
"""
"""
if len(self.buffer) < BUFFER_SIZE:
return
self.training_steps += 1
transitions = self.buffer.sample(BATCH_SIZE)
# Convert batch array of transitions to Transition of batch arrays
batch = Transition(*zip(*transitions))
state_batch = torch.cat(batch.state)#.to(device)
action_batch = torch.cat(batch.action)#.unsqueeze(1)#.to(device)
reward_batch = torch.cat(batch.reward)#.unsqueeze(1)#.to(device)
non_terminal_next_state_batch = torch.cat(
[s for s in batch.next_state if s is not None]
)#.to(device)
sav_t = self.Q_net(state_batch)
state_action_values = sav_t[torch.arange(sav_t.size(0)), action_batch]
# Get state-action values
non_terminal_mask = torch.tensor(
tuple(map(lambda s: s is not None, batch.next_state)),
device=device,
dtype=torch.bool
)
next_state_Q_values = torch.zeros(BATCH_SIZE, device=device)
next_state_Q_values[non_terminal_mask] = self.target_Q_net(
non_terminal_next_state_batch
).max(1)[0].detach()
# Q1 = torch.zeros(BATCH_SIZE, device=device)
# Q1[non_terminal_mask] = self.Q_net(
# non_terminal_next_state_batch
# ).max(1)[0].detach()
# Q1_values = torch.argmax(Q1)
# Q2 = torch.zeros(BATCH_SIZE, device=device)
# Q2[Q1_values] = self.target_Q_net(
# non_terminal_next_state_batch
# ).max(1)[0].detach()
# Q_preds = self.Q_net(state_batch)
# # #get Q values of action taken, shape (N,1)
# Q_vals = Q_preds.gather(1, action_batch)
# # """
# # In double DQN, get Q values for next_state from both target and model
# # """
# # #get Q values from target and model
# with torch.no_grad(): #don't want gradients for model
# model_pred = self.Q_net(non_terminal_next_state_batch)
# target_pred = self.target_Q_net(non_terminal_next_state_batch)
# """
# then get the actions from the model predicted Q values, not the target
# """
# model_actions = model_pred.max(1)[1].unsqueeze(1)
# """
# then use these actions to get Q values from the target network
# """
# target_Q = target_pred.gather(1, model_actions)
# #tensor for placing target values
# target_vals = torch.zeros(BATCH_SIZE, 1).to(device)
# """
# target_vals now filled with Q values from target using model predicted actions
# """
# #fill in target values for non_terminal states
# #the terminal states will stay initialized as zeros
# target_vals[non_terminal_mask] = target_Q
# ground_truth_q_values = reward_batch + (target_vals *GAMMA)
# Compute the ground truth
ground_truth_q_values = reward_batch + GAMMA*next_state_Q_values
# ground_truth_q_values = reward_batch + GAMMA*Q2
ground_truth_q_values = torch.reshape(
ground_truth_q_values.unsqueeze(1),
(1, BATCH_SIZE)
)[0]
# Compute Huber loss
criterion = nn.SmoothL1Loss()
loss = criterion(state_action_values, ground_truth_q_values)
# Optimize the model
self.optimizer.zero_grad(set_to_none=True) # https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#use-parameter-grad-none-instead-of-model-zero-grad-or-optimizer-zero-grad
loss.backward()
# print("Parameters ",self.Q_net.parameters())
for param in self.Q_net.parameters():
# print("Para :",param)
if(param.grad is not None):
param.grad.data.clamp_(-1, 1)
self.optimizer.step()