forked from blackredscarf/pytorch-DQN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
48 lines (39 loc) · 1.27 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
43
44
45
46
47
48
import torch
from torch import nn
class DQN(nn.Module):
def __init__(self, num_inputs, actions_dim):
super(DQN, self).__init__()
self.nn = nn.Sequential(
nn.Linear(num_inputs, 128),
nn.ReLU(),
nn.Linear(128, 128),
nn.ReLU(),
nn.Linear(128, actions_dim)
)
def forward(self, x):
return self.nn(x)
class CnnDQN(nn.Module):
def __init__(self, inputs_shape, num_actions):
super(CnnDQN, self).__init__()
self.inut_shape = inputs_shape
self.num_actions = num_actions
self.features = nn.Sequential(
nn.Conv2d(inputs_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()
)
self.fc = nn.Sequential(
nn.Linear(self.features_size(), 512),
nn.ReLU(),
nn.Linear(512, self.num_actions)
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def features_size(self):
return self.features(torch.zeros(1, *self.inut_shape)).view(1, -1).size(1)