-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathagent_nn.py
46 lines (37 loc) · 1.29 KB
/
agent_nn.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
import torch
from torch import nn
import numpy as np
class AgentNN(nn.Module):
def __init__(self, input_shape, n_actions, freeze=False):
super().__init__()
# Conolutional layers
self.conv_layers = nn.Sequential(
nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU(),
)
conv_out_size = self._get_conv_out(input_shape)
# Linear layers
self.network = nn.Sequential(
self.conv_layers,
nn.Flatten(),
nn.Linear(conv_out_size, 512),
nn.ReLU(),
nn.Linear(512, n_actions)
)
if freeze:
self._freeze()
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.to(self.device)
def forward(self, x):
return self.network(x)
def _get_conv_out(self, shape):
o = self.conv_layers(torch.zeros(1, *shape))
# np.prod returns the product of array elements over a given axis
return int(np.prod(o.size()))
def _freeze(self):
for p in self.network.parameters():
p.requires_grad = False