-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
83 lines (73 loc) · 2.78 KB
/
models.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
77
78
79
80
81
82
83
import torch
import torch.distributions as distributions
import torch.nn as nn
import torch.nn.functional as F
def normalized_columns_initializer(size, std=1.0):
"""
Normalizing over a matrix.
:param weights: given matrix
:param std: standard deviation
:return: normalized matrix
"""
out = torch.randn(size)
out *= std / torch.sqrt(out.pow(2).unsqueeze(0).sum(1).expand_as(out))
return out
class AttentionNet(nn.Module):
def __init__(self, input_size, is_weight_size=500, gru_size=1024):
super(AttentionNet, self).__init__()
self.fc1 = nn.Linear(input_size, 64)
self.fc2 = nn.Linear(64, 64)
self.fc3 = nn.Linear(64, 128)
self.fc4 = nn.Linear(128, 128)
self.fc5 = nn.Linear(128, 256)
self.fc6 = nn.Linear(256, 256)
self.fc7 = nn.Linear(256, 512)
self.fc8 = nn.Linear(512, gru_size)
self.gru = nn.GRUCell(gru_size, gru_size)
self.fc9 = nn.Linear(gru_size, 512)
self.fc10_mean = nn.Linear(512, is_weight_size)
self.fc10_std = nn.Linear(512, is_weight_size)
for layer in self.modules():
if isinstance(layer, nn.Linear):
layer.weight.data = normalized_columns_initializer(layer.weight.data.size(), 0.5)
layer.bias.data.fill_(0)
self.gru.bias_ih.data.fill_(0)
self.gru.bias_hh.data.fill_(0)
torch.nn.init.constant_(self.fc10_std.bias, 1)
def forward(self, data, h):
x = F.relu(self.fc1(data))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = F.relu(self.fc5(x))
x = F.relu(self.fc6(x))
x = F.relu(self.fc7(x))
x = self.fc8(x)
grux = self.gru(x, h)
x = F.relu(self.fc9(grux))
return F.softmax(self.fc10_mean(x)), grux
class CriticNet(nn.Module):
def __init__(self, input_size, gru_size=1024):
super(CriticNet, self).__init__()
self.fc1 = nn.Linear(input_size, 4096)
self.fc2 = nn.Linear(4096, 2048)
self.fc3 = nn.Linear(2048, gru_size)
self.gru = nn.GRUCell(gru_size, gru_size)
self.fc4 = nn.Linear(gru_size, 256)
self.fc5 = nn.Linear(256, 32)
self.fc6 = nn.Linear(32, 1)
for layer in self.modules():
if isinstance(layer, nn.Linear):
layer.weight.data = normalized_columns_initializer(layer.weight.data.size(), 0.5)
layer.bias.data.fill_(0)
self.gru.bias_ih.data.fill_(0)
self.gru.bias_hh.data.fill_(0)
def forward(self, data, h):
x = F.relu(self.fc1(data))
x = F.relu(self.fc2(x))
x = self.fc3(x)
grux = self.gru(x, h)
x = F.relu(self.fc4(grux))
x = F.relu(self.fc5(x))
x = self.fc6(x)
return x, grux