-
Notifications
You must be signed in to change notification settings - Fork 2
/
DQN.py
520 lines (395 loc) · 20.4 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import os
import gc
import torch
import pygame
import numpy as np
import torch.nn as nn
import gymnasium as gym
import torch.optim as optim
from collections import deque
import matplotlib.pyplot as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
gc.collect()
torch.cuda.empty_cache()
os.environ['CUDA_LAUNCH_BLOCKING'] = '1' # Used for debugging; CUDA related errors shown immediately.
# Seed everything for reproducible results
seed = 2024
np.random.seed(seed)
np.random.default_rng(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class ReplayMemory:
def __init__(self, capacity):
"""
Experience Replay Memory defined by deques to store transitions/agent experiences
"""
self.capacity = capacity
self.states = deque(maxlen=capacity)
self.actions = deque(maxlen=capacity)
self.next_states = deque(maxlen=capacity)
self.rewards = deque(maxlen=capacity)
self.dones = deque(maxlen=capacity)
def store(self, state, action, next_state, reward, done):
"""
Append (store) the transitions to their respective deques
"""
self.states.append(state)
self.actions.append(action)
self.next_states.append(next_state)
self.rewards.append(reward)
self.dones.append(done)
def sample(self, batch_size):
"""
Randomly sample transitions from memory, then convert sampled transitions
to tensors and move to device (CPU or GPU).
"""
indices = np.random.choice(len(self), size=batch_size, replace=False)
states = torch.stack([torch.as_tensor(self.states[i], dtype=torch.float32, device=device) for i in indices]).to(device)
actions = torch.as_tensor([self.actions[i] for i in indices], dtype=torch.long, device=device)
next_states = torch.stack([torch.as_tensor(self.next_states[i], dtype=torch.float32, device=device) for i in indices]).to(device)
rewards = torch.as_tensor([self.rewards[i] for i in indices], dtype=torch.float32, device=device)
dones = torch.as_tensor([self.dones[i] for i in indices], dtype=torch.bool, device=device)
return states, actions, next_states, rewards, dones
def __len__(self):
"""
To check how many samples are stored in the memory. self.dones deque
represents the length of the entire memory.
"""
return len(self.dones)
class DQN_Network(nn.Module):
"""
The Deep Q-Network (DQN) model for reinforcement learning.
This network consists of Fully Connected (FC) layers with ReLU activation functions.
"""
def __init__(self, num_actions, input_dim):
"""
Initialize the DQN network.
Parameters:
num_actions (int): The number of possible actions in the environment.
input_dim (int): The dimensionality of the input state space.
"""
super(DQN_Network, self).__init__()
self.FC = nn.Sequential(
nn.Linear(input_dim, 12),
nn.ReLU(inplace=True),
nn.Linear(12, 8),
nn.ReLU(inplace=True),
nn.Linear(8, num_actions)
)
# Initialize FC layer weights using He initialization
for layer in [self.FC]:
for module in layer:
if isinstance(module, nn.Linear):
nn.init.kaiming_uniform_(module.weight, nonlinearity='relu')
def forward(self, x):
"""
Forward pass of the network to find the Q-values of the actions.
Parameters:
x (torch.Tensor): Input tensor representing the state.
Returns:
Q (torch.Tensor): Tensor containing Q-values for each action.
"""
Q = self.FC(x)
return Q
class DQN_Agent:
"""
DQN Agent Class. This class defines some key elements of the DQN algorithm,
such as the learning method, hard update, and action selection based on the
Q-value of actions or the epsilon-greedy policy.
"""
def __init__(self, env, epsilon_max, epsilon_min, epsilon_decay,
clip_grad_norm, learning_rate, discount, memory_capacity):
# To save the history of network loss
self.loss_history = []
self.running_loss = 0
self.learned_counts = 0
# RL hyperparameters
self.epsilon_max = epsilon_max
self.epsilon_min = epsilon_min
self.epsilon_decay = epsilon_decay
self.discount = discount
self.action_space = env.action_space
self.action_space.seed(seed) # Set the seed to get reproducible results when sampling the action space
self.observation_space = env.observation_space
self.replay_memory = ReplayMemory(memory_capacity)
# Initiate the network models
self.main_network = DQN_Network(num_actions=self.action_space.n, input_dim=self.observation_space.n).to(device)
self.target_network = DQN_Network(num_actions=self.action_space.n, input_dim=self.observation_space.n).to(device).eval()
self.target_network.load_state_dict(self.main_network.state_dict())
self.clip_grad_norm = clip_grad_norm # For clipping exploding gradients caused by high reward value
self.critertion = nn.MSELoss()
self.optimizer = optim.Adam(self.main_network.parameters(), lr=learning_rate)
def select_action(self, state):
"""
Selects an action using epsilon-greedy strategy OR based on the Q-values.
Parameters:
state (torch.Tensor): Input tensor representing the state.
Returns:
action (int): The selected action.
"""
# Exploration: epsilon-greedy
if np.random.random() < self.epsilon_max:
return self.action_space.sample()
# Exploitation: the action is selected based on the Q-values.
with torch.no_grad():
Q_values = self.main_network(state)
action = torch.argmax(Q_values).item()
return action
def learn(self, batch_size, done):
"""
Train the main network using a batch of experiences sampled from the replay memory.
Parameters:
batch_size (int): The number of experiences to sample from the replay memory.
done (bool): Indicates whether the episode is done or not. If done,
calculate the loss of the episode and append it in a list for plot.
"""
# Sample a batch of experiences from the replay memory
states, actions, next_states, rewards, dones = self.replay_memory.sample(batch_size)
# # The following prints are for debugging. Use them to indicate the correct shape of the tensors.
# print('Before--------Before')
# print("states:", states.shape)
# print("actions:", actions.shape)
# print("next_states:", next_states.shape)
# print("rewards:", rewards.shape)
# print("dones:", dones.shape)
# # Preprocess the data for training
# states = states.unsqueeze(1)
# next_states = next_states.unsqueeze(1)
actions = actions.unsqueeze(1)
rewards = rewards.unsqueeze(1)
dones = dones.unsqueeze(1)
# # The following prints are for debugging. Use them to indicate the correct shape of the tensors.
# print()
# print('After--------After')
# print("states:", states.shape)
# print("actions:", actions.shape)
# print("next_states:", next_states.shape)
# print("rewards:", rewards.shape)
# print("dones:", dones.shape)
predicted_q = self.main_network(states) # forward pass through the main network to find the Q-values of the states
predicted_q = predicted_q.gather(dim=1, index=actions) # selecting the Q-values of the actions that were actually taken
# Compute the maximum Q-value for the next states using the target network
with torch.no_grad():
next_target_q_value = self.target_network(next_states).max(dim=1, keepdim=True)[0] # not argmax (cause we want the maxmimum q-value, not the action that maximize it)
next_target_q_value[dones] = 0 # Set the Q-value for terminal states to zero
y_js = rewards + (self.discount * next_target_q_value) # Compute the target Q-values
loss = self.critertion(predicted_q, y_js) # Compute the loss
# Update the running loss and learned counts for logging and plotting
self.running_loss += loss.item()
self.learned_counts += 1
if done:
episode_loss = self.running_loss / self.learned_counts # The average loss for the episode
self.loss_history.append(episode_loss) # Append the episode loss to the loss history for plotting
# Reset the running loss and learned counts
self.running_loss = 0
self.learned_counts = 0
self.optimizer.zero_grad() # Zero the gradients
loss.backward() # Perform backward pass and update the gradients
# # Uncomment the following two lines to find the best value for clipping gradient (Comment torch.nn.utils.clip_grad_norm_ while uncommenting the following two lines)
# grad_norm_before_clip = torch.nn.utils.clip_grad_norm_(self.main_network.parameters(), float('inf'))
# print("Gradient norm before clipping:", grad_norm_before_clip)
# Clip the gradients to prevent exploding gradients
torch.nn.utils.clip_grad_norm_(self.main_network.parameters(), self.clip_grad_norm)
self.optimizer.step() # Update the parameters of the main network using the optimizer
def hard_update(self):
"""
Navie update: Update the target network parameters by directly copying
the parameters from the main network.
"""
self.target_network.load_state_dict(self.main_network.state_dict())
def update_epsilon(self):
"""
Update the value of epsilon for epsilon-greedy exploration.
This method decreases epsilon over time according to a decay factor, ensuring
that the agent becomes less exploratory and more exploitative as training progresses.
"""
self.epsilon_max = max(self.epsilon_min, self.epsilon_max * self.epsilon_decay)
def save(self, path):
"""
Save the parameters of the main network to a file with .pth extention.
"""
torch.save(self.main_network.state_dict(), path)
class Model_TrainTest:
def __init__(self, hyperparams):
# Define RL Hyperparameters
self.train_mode = hyperparams["train_mode"]
self.RL_load_path = hyperparams["RL_load_path"]
self.save_path = hyperparams["save_path"]
self.save_interval = hyperparams["save_interval"]
self.clip_grad_norm = hyperparams["clip_grad_norm"]
self.learning_rate = hyperparams["learning_rate"]
self.discount_factor = hyperparams["discount_factor"]
self.batch_size = hyperparams["batch_size"]
self.update_frequency = hyperparams["update_frequency"]
self.max_episodes = hyperparams["max_episodes"]
self.max_steps = hyperparams["max_steps"]
self.render = hyperparams["render"]
self.epsilon_max = hyperparams["epsilon_max"]
self.epsilon_min = hyperparams["epsilon_min"]
self.epsilon_decay = hyperparams["epsilon_decay"]
self.memory_capacity = hyperparams["memory_capacity"]
self.num_states = hyperparams["num_states"]
self.map_size = hyperparams["map_size"]
self.render_fps = hyperparams["render_fps"]
# Define Env
self.env = gym.make('FrozenLake-v1', map_name=f"{self.map_size}x{self.map_size}",
is_slippery=False, max_episode_steps=self.max_steps,
render_mode="human" if self.render else None)
self.env.metadata['render_fps'] = self.render_fps # For max frame rate make it 0
# Define the agent class
self.agent = DQN_Agent(env = self.env,
epsilon_max = self.epsilon_max,
epsilon_min = self.epsilon_min,
epsilon_decay = self.epsilon_decay,
clip_grad_norm = self.clip_grad_norm,
learning_rate = self.learning_rate,
discount = self.discount_factor,
memory_capacity = self.memory_capacity)
def state_preprocess(self, state:int, num_states:int):
"""
Convert an state to a tensor and basically it encodes the state into
an onehot vector. For example, the return can be something like tensor([0,0,1,0,0])
which could mean agent is at state 2 from total of 5 states.
"""
onehot_vector = torch.zeros(num_states, dtype=torch.float32, device=device)
onehot_vector[state] = 1
return onehot_vector
def train(self):
"""
Reinforcement learning training loop.
"""
total_steps = 0
self.reward_history = []
# Training loop over episodes
for episode in range(1, self.max_episodes+1):
state, _ = self.env.reset(seed=seed)
state = self.state_preprocess(state, num_states=self.num_states)
done = False
truncation = False
step_size = 0
episode_reward = 0
while not done and not truncation:
action = self.agent.select_action(state)
next_state, reward, done, truncation, _ = self.env.step(action)
next_state = self.state_preprocess(next_state, num_states=self.num_states)
self.agent.replay_memory.store(state, action, next_state, reward, done)
if len(self.agent.replay_memory) > self.batch_size and sum(self.reward_history) > 0:
self.agent.learn(self.batch_size, (done or truncation))
# Update target-network weights
if total_steps % self.update_frequency == 0:
self.agent.hard_update()
state = next_state
episode_reward += reward
step_size +=1
# Appends for tracking history
self.reward_history.append(episode_reward) # episode reward
total_steps += step_size
# Decay epsilon at the end of each episode
self.agent.update_epsilon()
#-- based on interval
if episode % self.save_interval == 0:
self.agent.save(self.save_path + '_' + f'{episode}' + '.pth')
if episode != self.max_episodes:
self.plot_training(episode)
print('\n~~~~~~Interval Save: Model saved.\n')
result = (f"Episode: {episode}, "
f"Total Steps: {total_steps}, "
f"Ep Step: {step_size}, "
f"Raw Reward: {episode_reward:.2f}, "
f"Epsilon: {self.agent.epsilon_max:.2f}")
print(result)
self.plot_training(episode)
def test(self, max_episodes):
"""
Reinforcement learning policy evaluation.
"""
# Load the weights of the test_network
self.agent.main_network.load_state_dict(torch.load(self.RL_load_path))
self.agent.main_network.eval()
# Testing loop over episodes
for episode in range(1, max_episodes+1):
state, _ = self.env.reset(seed=seed)
done = False
truncation = False
step_size = 0
episode_reward = 0
while not done and not truncation:
state = self.state_preprocess(state, num_states=self.num_states)
action = self.agent.select_action(state)
next_state, reward, done, truncation, _ = self.env.step(action)
state = next_state
episode_reward += reward
step_size += 1
# Print log
result = (f"Episode: {episode}, "
f"Steps: {step_size:}, "
f"Reward: {episode_reward:.2f}, ")
print(result)
pygame.quit() # close the rendering window
def plot_training(self, episode):
# Calculate the Simple Moving Average (SMA) with a window size of 50
sma = np.convolve(self.reward_history, np.ones(50)/50, mode='valid')
plt.figure()
plt.title("Rewards")
plt.plot(self.reward_history, label='Raw Reward', color='#F6CE3B', alpha=1)
plt.plot(sma, label='SMA 50', color='#385DAA')
plt.xlabel("Episode")
plt.ylabel("Rewards")
plt.legend()
# Only save as file if last episode
if episode == self.max_episodes:
plt.savefig('./reward_plot.png', format='png', dpi=600, bbox_inches='tight')
plt.tight_layout()
plt.grid(True)
plt.show()
plt.clf()
plt.close()
plt.figure()
plt.title("Loss")
plt.plot(self.agent.loss_history, label='Loss', color='#CB291A', alpha=1)
plt.xlabel("Episode")
plt.ylabel("Loss")
# Only save as file if last episode
if episode == self.max_episodes:
plt.savefig('./Loss_plot.png', format='png', dpi=600, bbox_inches='tight')
plt.tight_layout()
plt.grid(True)
plt.show()
if __name__ == '__main__':
# Parameters:
train_mode = False
render = not train_mode
map_size = 8 # 4x4 or 8x8
RL_hyperparams = {
"train_mode" : train_mode,
"RL_load_path" : f'./{map_size}x{map_size}/final_weights' + '_' + '3000' + '.pth',
"save_path" : f'./{map_size}x{map_size}/final_weights',
"save_interval" : 500,
"clip_grad_norm" : 3,
"learning_rate" : 6e-4,
"discount_factor" : 0.93,
"batch_size" : 32,
"update_frequency" : 10,
"max_episodes" : 3000 if train_mode else 5,
"max_steps" : 200,
"render" : render,
"epsilon_max" : 0.999 if train_mode else -1,
"epsilon_min" : 0.01,
"epsilon_decay" : 0.999,
"memory_capacity" : 4_000 if train_mode else 0,
"map_size" : map_size,
"num_states" : map_size ** 2,
"render_fps" : 6,
}
# Run
DRL = Model_TrainTest(RL_hyperparams) # Define the instance
# Train
if train_mode:
DRL.train()
else:
# Test
DRL.test(max_episodes = RL_hyperparams['max_episodes'])