-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.py
44 lines (39 loc) · 1.7 KB
/
Network.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
import torch.nn as nn
class Net(nn.Module):
"""
Convolutional Neural Network for PPO
"""
def __init__(self, img_stack):
super(Net, self).__init__()
self.cnn_base = nn.Sequential( # input shape (4, 96, 96)
nn.Conv2d(img_stack, 8, kernel_size=4, stride=2),
nn.ReLU(), # activation
nn.Conv2d(8, 16, kernel_size=3, stride=2), # (8, 47, 47)
nn.ReLU(), # activation
nn.Conv2d(16, 32, kernel_size=3, stride=2), # (16, 23, 23)
nn.ReLU(), # activation
nn.Conv2d(32, 64, kernel_size=3, stride=2), # (32, 11, 11)
nn.ReLU(), # activation
nn.Conv2d(64, 128, kernel_size=3, stride=1), # (64, 5, 5)
nn.ReLU(), # activation
nn.Conv2d(128, 256, kernel_size=3, stride=1), # (128, 3, 3)
nn.ReLU(), # activation
) # output shape (256, 1, 1)
self.v = nn.Sequential(nn.Linear(256, 100), nn.ReLU(), nn.Linear(100, 1))
self.fc = nn.Sequential(nn.Linear(256, 100), nn.ReLU())
self.alpha_head = nn.Sequential(nn.Linear(100, 3), nn.Softplus())
self.beta_head = nn.Sequential(nn.Linear(100, 3), nn.Softplus())
self.apply(self._weights_init)
@staticmethod
def _weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight, gain=nn.init.calculate_gain('relu'))
nn.init.constant_(m.bias, 0.1)
def forward(self, x):
x = self.cnn_base(x)
x = x.view(-1, 256)
v = self.v(x)
x = self.fc(x)
alpha = self.alpha_head(x) + 1
beta = self.beta_head(x) + 1
return (alpha, beta), v