-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
61 lines (54 loc) · 2.27 KB
/
models.py
File metadata and controls
61 lines (54 loc) · 2.27 KB
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
class GraphNet(nn.Module):
def __init__(self, input_dim = 12, hidden_dim = 64, output_dim = 1, aggr = 'add', niters = 4):
super(GraphNet, self).__init__()
# transform to latent space
self.inputnetwork = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim),
nn.ELU(),
# nn.Dropout(0.2),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
# nn.Dropout(0.2),
# nn.Linear(2*hidden_dim, hidden_dim),
# nn.ReLU()
)
# to compute messages
convnetwork = nn.Sequential(
nn.Linear(2 * hidden_dim, 2 * hidden_dim),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(2 * hidden_dim, hidden_dim),
nn.ReLU()
)
# EdgeConv
self.graphconv = EdgeConv(nn=convnetwork, aggr=aggr)
# edge features from node embeddings for classification
self.edgenetwork = nn.Sequential(
nn.Linear(2 * hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(hidden_dim, output_dim),
nn.Sigmoid()
)
self.niters = niters
def forward(self, data):
X = data.x
#print(f"edges {data.num_edges}")
#print(f"initial X size {X.size()}")
H = self.inputnetwork(X)
#print(f"size after inputnet {H.size()}")
for i in range(self.niters):
(prepared_edges, _) = Utils.add_self_loops(data.edge_index)
H = self.graphconv(H, Utils.to_undirected(prepared_edges))
#print(f"size of H after {i}th iter {H.size()}")
src, dst = data.edge_index
#print(f"src size {H[src].size()} dest size {H[dst].size()}")
#print(f"torch cat {torch.cat([H[src], H[dst]], dim=-1).size()}")
#print(f"GraphNet returns {self.edgenetwork(torch.cat([H[src], H[dst]], dim=-1)).squeeze(-1).size()}")
#print(f"score size {(H[src] * H[dst]).sum(dim=-1)}")
#return (H[src] * H[dst]).sum(dim=-1)
return self.edgenetwork(torch.cat([H[src], H[dst]], dim=-1)).squeeze(-1)