-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
65 lines (46 loc) · 1.83 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
from imports import *
def weight_init(layer, first_layer):
with torch.no_grad():
if first_layer:
if hasattr(layer, "weight"):
num_inputs = layer.weight.size(-1)
layer.weight.uniform_(-1 / num_inputs, 1 / num_inputs)
else:
if hasattr(layer, "weight"):
num_inputs = layer.weight.size(-1)
layer.weight.uniform_(-np.sqrt(6/num_inputs) / 30, np.sqrt(6/num_inputs) / 30)
class GINR(nn.Module):
def __init__(self, input_dim, output_dim, hidden_dim=512, num_layers=6, bn=False, skip=True):
super().__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.num_layers = num_layers
self.model = nn.ModuleList()
in_dim = self.input_dim
out_dim = self.hidden_dim
for i in range(num_layers):
layer = nn.Linear(in_dim, out_dim)
weight_init(layer, i == 0)
self.model.append(layer)
if i < self.num_layers - 1:
self.model.append(nn.ReLU())
if bn:
self.model.append(nn.LayerNorm(out_dim))
# add a skip connection to the middle of the model
in_dim = hidden_dim
if (i + 1) == int(np.ceil(self.num_layers/2)) and skip:
self.skip_at = len(self.model)
in_dim += input_dim
# reached last layer
out_dim = hidden_dim
if (i + 1) == self.num_layers - 1:
out_dim = output_dim
def forward(self, x):
# feedforward through MLP
x_in = x
for i, layer in enumerate(self.model):
if i == self.skip_at:
x = torch.cat([x, x_in], dim=-1)
x = layer(x)
return x