-
Notifications
You must be signed in to change notification settings - Fork 1
/
dqn.py
174 lines (140 loc) · 6.86 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
import random
import math
from collections import namedtuple, deque
import configuration
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
Transition = namedtuple('Transition',
('state', 'action', 'next_state', 'reward'))
class ReplayMemory(object):
def __init__(self, capacity):
self.memory = deque([], maxlen=capacity)
def push(self, *args):
self.memory.append(Transition(*args))
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
class DQN(nn.Module):
def __init__(self, x_resolution, y_resolution, n_actions):
super(DQN, self).__init__()
self.x_resolution = x_resolution
self.y_resolution = y_resolution
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.layer1 = nn.Linear(16544, 128)
self.layer2 = nn.Linear(128, 128)
self.layer3 = nn.Linear(128, n_actions)
# Called with either one element to determine next action, or a batch
# during optimization. Returns tensor([[left0exp,right0exp]...]).
def forward(self, x):
# input is a batch of flat vectors, x * y * color channels
# reshape flat vectors into batches of 3 planes of x*y each
x = x.view(-1, self.y_resolution, self.x_resolution, 3)
# change the ordering so that it is batch, channel, y, x
x = x.permute(0, 3, 1, 2)
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = torch.flatten(x, 1)
x = F.relu(self.layer1(x))
x = F.relu(self.layer2(x))
return self.layer3(x)
# BATCH_SIZE is the number of transitions sampled from the replay buffer
# GAMMA is the discount factor
# EPS_START is the starting value of epsilon
# EPS_END is the final value of epsilon
# EPS_DECAY controls the rate of exponential decay of epsilon, higher means a slower decay
# TAU is the update rate of the target network
# LR is the learning rate of the ``AdamW`` optimizer
BATCH_SIZE = 10
GAMMA = 0.99
EPS_START = 0.9
EPS_END = 0.05
EPS_DECAY = 1000
TAU = 0.005
LR = 1e-4
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class DQNModel():
def __init__(self, n_actions, policy_path=None, target_path=None):
self.steps_done = 0
self.n_actions = n_actions
self.policy_net = DQN(configuration.SCREENSHOT_X_RES, configuration.SCREENSHOT_Y_RES, n_actions).to(device)
self.target_net = DQN(configuration.SCREENSHOT_X_RES, configuration.SCREENSHOT_Y_RES, n_actions).to(device)
if policy_path is not None:
self.policy_net.load_state_dict(torch.load(policy_path))
if target_path is not None:
self.target_net.load_state_dict(torch.load(target_path))
else:
self.target_net.load_state_dict(self.policy_net.state_dict())
self.target_net.eval()
self.policy_net.eval()
self.optimizer = optim.AdamW(self.policy_net.parameters(), lr=LR, amsgrad=True)
self.memory = ReplayMemory(10000)
def save_network(self, policy_path, target_path):
torch.save(self.policy_net.state_dict(), policy_path)
torch.save(self.target_net.state_dict(), target_path)
# get the output of the network, with randomization which decreases over time for training
def get_result(self, state):
sample = random.random()
eps_threshold = EPS_END + (EPS_START - EPS_END) * math.exp(-1. * self.steps_done / EPS_DECAY)
self.steps_done += 1
if sample > eps_threshold:
with torch.no_grad():
return self.policy_net(state).max(1)[1].view(1, 1)
else:
return torch.tensor(np.random.randint(0,self.n_actions), device=device, dtype=torch.long)
# get the output of the network with no randomization, for testing purposes only
def nonrandom_result(self, state):
return self.policy_net(state).max(1)[1].view(1, 1)
def save_to_memory(self, *args):
self.memory.push(*args)
def optimize(self):
if len(self.memory) < BATCH_SIZE:
return
transitions = self.memory.sample(BATCH_SIZE)
# Transpose the batch (see https://stackoverflow.com/a/19343/3343043 for
# detailed explanation). This converts batch-array of Transitions
# to Transition of batch-arrays.
batch = Transition(*zip(*transitions))
# Compute a mask of non-final states and concatenate the batch elements
# (a final state would've been the one after which simulation ended)
non_final_mask = torch.tensor(tuple(map(lambda s: s is not None,
batch.next_state)), device=device, dtype=torch.bool)
non_final_next_states = torch.cat([s for s in batch.next_state
if s is not None])
state_batch = torch.cat(batch.state)
action_batch = torch.tensor(batch.action).view(-1,1)
reward_batch = torch.cat(batch.reward)
# Compute Q(s_t, a) - the model computes Q(s_t), then we select the
# columns of actions taken. These are the actions which would've been taken
# for each batch state according to policy_net
state_action_values = self.policy_net(state_batch).gather(1, action_batch)
# Compute V(s_{t+1}) for all next states.
# Expected values of actions for non_final_next_states are computed based
# on the "older" target_net; selecting their best reward with max(1)[0].
# This is merged based on the mask, such that we'll have either the expected
# state value or 0 in case the state was final.
next_state_values = torch.zeros(BATCH_SIZE, device = device)
with torch.no_grad():
next_state_values[non_final_mask] = self.target_net(non_final_next_states).max(1)[0]
# Compute the expected Q values
expected_state_action_values = (next_state_values * GAMMA) + reward_batch
# Compute Huber loss
criterion = nn.SmoothL1Loss()
loss = criterion(state_action_values, expected_state_action_values.unsqueeze(1))
# Optimize the model
self.optimizer.zero_grad()
loss.backward()
# In-place gradient clipping
torch.nn.utils.clip_grad_value_(self.policy_net.parameters(), 100)
self.optimizer.step()
# Soft update of the target network's weights
target_net_state_dict = self.target_net.state_dict()
policy_net_state_dict = self.policy_net.state_dict()
for key in policy_net_state_dict:
target_net_state_dict[key] = policy_net_state_dict[key] * TAU + target_net_state_dict[key] * (1-TAU)
self.target_net.load_state_dict(target_net_state_dict)