-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.py
71 lines (54 loc) · 2.15 KB
/
common.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
from collections import OrderedDict
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.nn import ModuleList, Linear as Lin
from torch_geometric.nn import BatchNorm, ARMAConv
class MLP(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, act=nn.Tanh()):
super(MLP, self).__init__()
self.mlp = nn.Sequential(OrderedDict([
('lin1', Lin(in_channels, hidden_channels)),
('act', act),
('lin2', Lin(hidden_channels, out_channels))
]))
def forward(self, x):
return self.mlp(x)
class EdgeMaskNet(torch.nn.Module):
def __init__(self,
n_in_channels,
e_in_channels,
hid=72, n_layers=3):
super(EdgeMaskNet, self).__init__()
self.node_lin = Lin(n_in_channels, hid)
self.convs = ModuleList()
self.batch_norms = ModuleList()
for _ in range(n_layers):
conv = ARMAConv(in_channels=hid, out_channels=hid)
self.convs.append(conv)
self.batch_norms.append(BatchNorm(hid))
if e_in_channels > 1:
self.edge_lin1 = Lin(2 * hid, hid)
self.edge_lin2 = Lin(e_in_channels, hid)
self.mlp = MLP(2 * hid, hid, 1)
else:
self.mlp = MLP(2 * hid, hid, 1)
self._initialize_weights()
def forward(self, x, edge_index, edge_attr):
x = torch.flatten(x, 1, -1)
x = F.relu(self.node_lin(x))
for conv, batch_norm in zip(self.convs, self.batch_norms):
x = F.relu(conv(x, edge_index))
x = batch_norm(x)
e = torch.cat([x[edge_index[0, :]], x[edge_index[1, :]]], dim=1)
if edge_attr.size(-1) > 1:
e1 = self.edge_lin1(e)
e2 = self.edge_lin2(edge_attr)
e = torch.cat([e1, e2], dim=1) # connection
return self.mlp(e)
def __repr__(self):
return f'{self.__class__.__name__}()'
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)