-
Notifications
You must be signed in to change notification settings - Fork 11
/
models.py
52 lines (43 loc) · 2.11 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
import torch
import torch.nn.functional as F
from torch_geometric.nn import GINConv, global_add_pool
class serverGIN(torch.nn.Module):
def __init__(self, nlayer, nhid):
super(serverGIN, self).__init__()
self.graph_convs = torch.nn.ModuleList()
self.nn1 = torch.nn.Sequential(torch.nn.Linear(nhid, nhid), torch.nn.ReLU(),
torch.nn.Linear(nhid, nhid))
self.graph_convs.append(GINConv(self.nn1))
for l in range(nlayer - 1):
self.nnk = torch.nn.Sequential(torch.nn.Linear(nhid, nhid), torch.nn.ReLU(),
torch.nn.Linear(nhid, nhid))
self.graph_convs.append(GINConv(self.nnk))
class GIN(torch.nn.Module):
def __init__(self, nfeat, nhid, nclass, nlayer, dropout):
super(GIN, self).__init__()
self.num_layers = nlayer
self.dropout = dropout
self.pre = torch.nn.Sequential(torch.nn.Linear(nfeat, nhid))
self.graph_convs = torch.nn.ModuleList()
self.nn1 = torch.nn.Sequential(torch.nn.Linear(nhid, nhid), torch.nn.ReLU(), torch.nn.Linear(nhid, nhid))
self.graph_convs.append(GINConv(self.nn1))
for l in range(nlayer - 1):
self.nnk = torch.nn.Sequential(torch.nn.Linear(nhid, nhid), torch.nn.ReLU(), torch.nn.Linear(nhid, nhid))
self.graph_convs.append(GINConv(self.nnk))
self.post = torch.nn.Sequential(torch.nn.Linear(nhid, nhid), torch.nn.ReLU())
self.readout = torch.nn.Sequential(torch.nn.Linear(nhid, nclass))
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
x = self.pre(x)
for i in range(len(self.graph_convs)):
x = self.graph_convs[i](x, edge_index)
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
x = global_add_pool(x, batch)
x = self.post(x)
x = F.dropout(x, self.dropout, training=self.training)
x = self.readout(x)
x = F.log_softmax(x, dim=1)
return x
def loss(self, pred, label):
return F.nll_loss(pred, label)