-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels_sag.py
executable file
·72 lines (53 loc) · 2.57 KB
/
models_sag.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
72
from torch_geometric.nn import GCNConv
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp
from layers import SAGPool
import torch
import torch.nn.functional as F
import Analysis
import pickle
class Net(torch.nn.Module):
def __init__(self,args):
super(Net, 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.conv1 = GCNConv(self.num_features, self.nhid)
self.pool1 = SAGPool(self.nhid, ratio=self.pooling_ratio)
self.conv2 = GCNConv(self.nhid, self.nhid)
self.pool2 = SAGPool(self.nhid, ratio=self.pooling_ratio)
self.conv3 = GCNConv(self.nhid, self.nhid)
self.pool3 = SAGPool(self.nhid, ratio=self.pooling_ratio)
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)
self.LogSoftmax = torch.nn.LogSoftmax()
def forward(self, data):
x, edge_index, edge_attr, batch = data.x, data.edge_index, data.edge_attr, data.batch
x = F.relu(self.conv1(x, edge_index, edge_weight = edge_attr))
# Save weights of SAG Model
if Analysis.is_trained:
#w_dict = {"w": x}
w_dict = {"w": self.conv1.weight}
with open("SAG_W.pickle", 'wb') as f:
pickle.dump(w_dict, f)
print("SAG Weights are saved.")
print(x)
Analysis.is_trained = False
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.float(), edge_index, edge_weight=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.float(), edge_index, edge_weight=edge_attr))
x, edge_index, edge_attr, batch, _ = self.pool3(x, edge_index, edge_attr, batch)
x3 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
x = x1 + x2 + x3
x = F.relu(self.lin1(x.float()))
x = F.dropout(x, p=self.dropout_ratio, training=self.training)
x = F.relu(self.lin2(x))
x = F.log_softmax(self.lin3(x), dim=-1) # original
#x = F.relu(self.lin3(x))
return x