-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathnet.py
76 lines (64 loc) · 2.05 KB
/
net.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
import torch
from torch import nn
class AtariNet(nn.Module):
def __init__(self, num_actions):
super(AtariNet, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=8, stride=4),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.ReLU()
)
self.conv3 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU()
)
self.hidden = nn.Sequential(
nn.Linear(64 * 7 * 7, 512, bias=True),
nn.ReLU()
)
self.out = nn.Sequential(
nn.Linear(512, num_actions, bias=True)
)
self.apply(self.init_weights)
def init_weights(self, m):
if type(m) == nn.Conv2d:
m.weight.data.normal_(0.0, 0.02)
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = x.view(x.size(0), -1)
x = self.hidden(x)
x = self.out(x)
return 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)