-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
42 lines (34 loc) · 1.36 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class QNetwork(nn.Module):
def __init__(self, state_size, action_size, layer_units=[64, 64], seed=0, dueling=False):
super(QNetwork, self).__init__()
self._state_size = state_size
self._action_size = action_size
self._seed = torch.manual_seed(seed)
self._dueling = dueling
# Arquitecture (Dueling arq)
num_units = len(layer_units)
layers = [nn.Linear(state_size, layer_units[0])]
for i in range(1, num_units):
layers.append(nn.Linear(layer_units[i-1], layer_units[i]))
self.fc_layers = nn.ModuleList(layers)
self.V_layer = nn.Linear(layer_units[num_units-1], 1)
self.A_layer = nn.Linear(layer_units[num_units-1], action_size)
# Init layers
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 0)
def forward(self, state):
x = state
for layer in self.fc_layers:
x = F.relu(layer(x))
A_values = self.A_layer(x)
if not self._dueling: return A_values
V_value = self.V_layer(x)
Q_values = V_value + (A_values - A_values.mean(1, keepdim=True))
return Q_values