-
Notifications
You must be signed in to change notification settings - Fork 1
/
learn.py
414 lines (325 loc) · 13 KB
/
learn.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
import os
import sys
from traceback import format_exc
from typing import *
import random
import pygame
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
import numpy as np
import copy
from collections import namedtuple, deque
from snake import Snake
import time
RIGHT = 0
DOWN = 1
LEFT = 2
UP = 3
MOVE_STRAIGHT = [1, 0, 0]
MOVE_LEFT = [0, 1, 0]
MOVE_RIGHT = [0, 0, 1]
"""
Training:
Updating Q function with Bellman equation
Q(s, a) = r + gamma * max(Q(s', a')) where s' is next state and a' is next action
We want to minimize the Huber loss between the target and prediction Q values.
Loss = HuberLoss(r + gamma * max(Q(s', a')) - Q(s, a))
We calculate the loss and perform backpropagation to update the network over
a batch of experiences sampled from the replay buffer:
L = 1 / N * sum(HuberLoss(r + gamma * max(Q(s', a')) - Q(s, a)))
where
HuberLoss(x) = 0.5 * x^2 if |x| <= 1
|x| - 0.5 otherwise
"""
__all__ = ["ReplayBuffer", "Agent"]
Transition = namedtuple('Transition', ('state', 'action', 'next_state', 'reward', 'done'))
class ReplayBuffer():
def __init__(self, size: int) -> None:
self.memory = deque([], maxlen=size)
def add_experience(
self,
state: Union[np.ndarray, torch.Tensor],
action: torch.Tensor,
next_state: Union[np.ndarray, torch.Tensor],
reward: torch.Tensor,
done: int
):
self.memory.append(Transition(state, action, next_state, reward, done))
def sample(
self,
batch_size: int
) -> Tuple:
sample1 = random.sample(self.memory, batch_size)
return sample1
def __len__(self):
return len(self.memory)
def get(self):
return self.memory
class PriorityBuffer():
def __init__(self, size: int) -> None:
self.memory = deque([], maxlen=size)
self.priorities = deque([], maxlen=size)
self.eps = 0.001
self.alpha = 0.4
def add_experience(
self,
state: Union[np.ndarray, torch.Tensor],
action: torch.Tensor,
next_state: Union[np.ndarray, torch.Tensor],
reward: torch.Tensor,
done: int
):
self.memory.append(Transition(state, action, next_state, reward, done))
self.priorities.append( (abs(reward) + self.eps)**self.alpha ) # add priority
def sample(
self,
batch_size: int
) -> Tuple:
# pick a random number 0 <= s <= sum(priorities)
# and walk self.priority left to right,
# summing up a priority of the current element until the sum is exceeded
# and that element is chosen ot be in the batch
# get probabilities
p_sum = sum(self.priorities)
# walk left to right, summing up priorities
# until the sum is exceeded
j = 0
sample = []
for _ in range(batch_size):
# get random number
s = random.uniform(0, p_sum)
su = 0
for j in range(len(self.priorities)):
su += self.priorities[j]
if su >= s:
break
# add to sample
sample.append(self.memory[j])
return sample
def __len__(self):
return len(self.memory)
def get(self):
return self.memory
# nn.Module class:
# https://pytorch.org/docs/stable/generated/torch.nn.Module.html
class DQN(nn.Module):
def __init__(self, state_size: int, action_size: int,
hidden_size: int, seed: int) -> None:
super(DQN, self).__init__()
self.seed = torch.manual_seed(seed)
self.fc1 = nn.Linear(state_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, action_size)
def forward(self, state: torch.Tensor) -> torch.Tensor:
# build network that maps states to actions
x = F.relu(self.fc1(state))
# second hidden layer with relu activation
x = F.relu(self.fc2(x))
# third hidden layer with Linear activation
x = self.fc3(x)
return x
# build a deep q network with three hidden layers
class DeepDQN(nn.Module):
def __init__(self, state_size: int, action_size: int,
hidden_size: int, seed: int) -> None:
super(DeepDQN, self).__init__()
self.seed = torch.manual_seed(seed)
self.fc1 = nn.Linear(state_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, hidden_size)
self.fc4 = nn.Linear(hidden_size, action_size)
def forward(self, state: torch.Tensor) -> torch.Tensor:
# build network that maps states to actions
x = F.relu(self.fc1(state))
# second hidden layer with relu activation
x = F.relu(self.fc2(x))
# third hidden layer with relu activation
x = F.relu(self.fc3(x))
# fourth hidden layer with Linear activation
x = self.fc4(x)
return x
class Agent:
def __init__(
self,
state_size: int, # size of the state space
action_size: int, # size of the action space
hidden_size: int, # size of the hidden layer
lr: float, # learning rate
gamma: float, # discount factor
epsilon: float, # epsilon for epsilon-greedy action selection
# minimum value of epsilon
batch_size: int, # size of the batch
memory_size: int, # size of the replay buffer
update_every: int, # how often to update the network
device: str, # device to use for training, either 'cpu' or 'cuda'
seed: Any, # random seed,
epsilon_decay: float,
epsilon_min: float,
load_model: bool = False
):
self.state_size = state_size
self.action_size = action_size
self.hidden_size = hidden_size
self.lr = lr
self.gamma = gamma
self.epsilon = epsilon
self.epsilon_decay = epsilon_decay
self.epsilon_min = epsilon_min
self.batch_size = batch_size
self.memory_size = memory_size
self.update_every = update_every
self.device = device
self.seed = random.seed(seed)
# Q-Networks to approximate Q-Value function for the given state
self.qnetwork_local = DQN(state_size, action_size, hidden_size, seed)
if load_model:
self.qnetwork_local.load_state_dict(torch.load("snake.pth"))
self.qnetwork_target = DQN(state_size, action_size, hidden_size, seed)
# self.qnetwork_target.load_state_dict(self.qnetwork_local.state_dict())
# Optimizer to update the weights of the local network via stochastic gradient descent
self.optimizer = optim.Adam(self.qnetwork_local.parameters(), lr=lr)
self.PER = False
if not self.PER:
self.memory = ReplayBuffer(memory_size)
else:
self.memory = PriorityBuffer(memory_size)
self.t_step = 1
self.loss = 0
self.Q_expected = 0
self.target = 0
def save_model(self, path):
torch.save(self.qnetwork_local.state_dict(), path)
def act(
self,
state: torch.Tensor,
)-> int:
# state = state.to(self.device)
self.qnetwork_local.eval() # set the network to evaluation mode
with torch.no_grad():
action_values = self.qnetwork_local(state) # get predictions for all actions
self.qnetwork_local.train() # set the network back to training mode
action = [0, 0, 0] # straight, left, right
# epsilon-greedy action selection
if random.random() > self.epsilon:
max_action_value = torch.argmax(action_values)
max_action_value = max_action_value.item() # get the index of the highest predicted Q-value
# print(action_values)
action[max_action_value] = 1
return action, action_values
# return np.argmax(action_values.cpu().data.numpy())
# return the action with the highest predicted Q-value
else:
action[random.randint(0, 2)] = 1
return action, action_values
def train(
self
):
if len(self.memory) < self.batch_size:
return 0
self.t_step = (self.t_step + 1) % self.update_every
if self.t_step == 0:
if len(self.memory) > self.batch_size:
experiences = self.memory.sample(self.batch_size)
self.learn_experiences_v2(experiences, self.batch_size)
def replay_experiences(
self
):
# train the q agent
if len(self.memory) > self.batch_size:
experiences = self.memory.sample(self.batch_size)
size = self.batch_size
else:
experiences = self.memory.get()
size = len(self.memory)
self.learn_experiences_v2(experiences, size)
def learn_experiences_v2(
self,
experiences: Tuple[torch.Tensor, Transition],
batch_size: int
):
# --------------------------------------------------------------------------------------
# convert experiences to tensors to feed to the torch models and optimizers
states = []
actions = []
rewards = []
next_states = []
done = []
for e in experiences:
if e is not None:
states.append(e.state)
action = torch.Tensor(e.action)
action = action.type(torch.int64)
actions.append(action)
rewards.append(e.reward)
done.append(e.done)
if e.done:
next_states.append(None)
else:
next_states.append(e.next_state)
states = torch.stack(states).to(self.device)
actions = torch.stack(actions).to(self.device)
rewards = torch.tensor(rewards, dtype=torch.float).to(self.device)
# --------------------------------------------------------------------------------------
# get current q values (best action) for all actions in current states
# from the local model
# Current model estimates
state_action_values = self.qnetwork_local(states)
# best state action values from the current model's estimates
max_state_action_values = torch.stack(
[torch.max(state_action_values[i]) for i in range(len(state_action_values))]
)
# --------------------------------------------------------------------------------------
# Get max predicted Q values (for next states) from target model
# since we only add a discount Q value for non final states,
# we need to know which states are non final
non_final_mask = torch.tensor(
tuple(map(lambda s: s is not None, next_states)),
dtype=torch.bool).to(self.device)
non_final_next_states = torch.stack(
[s for s in next_states if s is not None]
).to(self.device)
# get the predicted Q values for the next states
with torch.no_grad():
next_q_values = self.qnetwork_target(non_final_next_states)
next_state_values = torch.zeros(batch_size).to(self.device) # initialize to zeros
# (non-final next states will be updated)
next_state_values[non_final_mask] = torch.max(next_q_values, dim=1)[0]
max_expected_state_action_values = next_state_values * self.gamma
max_expected_state_action_values = max_expected_state_action_values + rewards
try:
self.optimizer.zero_grad()
loss = F.mse_loss(max_state_action_values, max_expected_state_action_values)
loss.backward()
self.optimizer.step()
except RuntimeError as e:
print(e)
print(format_exc())
print("state_action_values: ", state_action_values.squeeze(), len(state_action_values), state_action_values.squeeze().shape)
print(f"expected_state_action_values: {max_expected_state_action_values}, {len(max_expected_state_action_values)}, {max_expected_state_action_values.shape}")
print("loss: ", loss)
sys.exit()
self.loss = loss.item()
self.epsilon = max(self.epsilon_min, self.epsilon_decay * self.epsilon)
# update target network every update_every steps (load weights of local to target)
if self.t_step % self.update_every == 0:
self.qnetwork_target.load_state_dict(self.qnetwork_local.state_dict())
def get_loss(self):
return self.loss
def remember(
self,
state: Union[np.ndarray, torch.Tensor],
action: int,
next_state: Union[np.ndarray, torch.Tensor],
reward: torch.Tensor,
done: int
):
self.memory.add_experience(state, action, next_state, reward, done)
# def get_q_values(
# self,
# states: torch.Tensor,
# actions: torch.Tensor,
# rewardS: torch.Tensor,
# done: torch.Tensor
# ):