-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmodels.py
57 lines (44 loc) · 2.19 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
import torch
import torch.nn.functional as F
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp
from torch_geometric.nn import GCNConv
from layers import GCN, HGPSLPool
class Model(torch.nn.Module):
def __init__(self, args):
super(Model, self).__init__()
self.args = args
self.num_features = args.num_features
self.nhid = args.nhid
self.num_classes = args.num_classes
self.pooling_ratio = args.pooling_ratio
self.dropout_ratio = args.dropout_ratio
self.sample = args.sample_neighbor
self.sparse = args.sparse_attention
self.sl = args.structure_learning
self.lamb = args.lamb
self.conv1 = GCNConv(self.num_features, self.nhid)
self.conv2 = GCN(self.nhid, self.nhid)
self.conv3 = GCN(self.nhid, self.nhid)
self.pool1 = HGPSLPool(self.nhid, self.pooling_ratio, self.sample, self.sparse, self.sl, self.lamb)
self.pool2 = HGPSLPool(self.nhid, self.pooling_ratio, self.sample, self.sparse, self.sl, self.lamb)
self.lin1 = torch.nn.Linear(self.nhid * 2, self.nhid)
self.lin2 = torch.nn.Linear(self.nhid, self.nhid // 2)
self.lin3 = torch.nn.Linear(self.nhid // 2, self.num_classes)
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
edge_attr = None
x = F.relu(self.conv1(x, edge_index, edge_attr))
x, edge_index, edge_attr, batch = self.pool1(x, edge_index, edge_attr, batch)
x1 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
x = F.relu(self.conv2(x, edge_index, edge_attr))
x, edge_index, edge_attr, batch = self.pool2(x, edge_index, edge_attr, batch)
x2 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
x = F.relu(self.conv3(x, edge_index, edge_attr))
x3 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
x = F.relu(x1) + F.relu(x2) + F.relu(x3)
x = F.relu(self.lin1(x))
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
x = F.relu(self.lin2(x))
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
x = F.log_softmax(self.lin3(x), dim=-1)
return x