-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnn.py
141 lines (112 loc) · 4.37 KB
/
gnn.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch
import torch.nn as nn
import torch.nn.functional as F
from layers import GAT_gate
class gnn(torch.nn.Module):
def __init__(self, args):
super(gnn, self).__init__()
n_graph_layer = args.n_graph_layer
d_graph_layer = args.d_graph_layer
n_FC_layer = args.n_FC_layer
d_FC_layer = args.d_FC_layer
self.dropout_rate = args.dropout_rate
self.branch = args.branch
cal_nhop = None
if args.tatic == "static":
def cal_nhop(x):
return args.nhop
elif args.tatic == "cont":
def cal_nhop(x):
return x + 1
elif args.tatic == "jump":
def cal_nhop(x):
return 2 * x + 1
self.layers1 = [d_graph_layer for i in range(n_graph_layer + 1)]
self.gconv1 = nn.ModuleList(
[
GAT_gate(
self.layers1[i], self.layers1[i +
1], cal_nhop(i), args.ngpu > 0
)
for i in range(len(self.layers1) - 1)
]
)
self.FC = nn.ModuleList(
[
nn.Linear(self.layers1[-1], d_FC_layer)
if i == 0
else nn.Linear(d_FC_layer, 1)
if i == n_FC_layer - 1
else nn.Linear(d_FC_layer, d_FC_layer)
for i in range(n_FC_layer)
]
)
self.embede = nn.Linear(2 * args.embedding_dim,
d_graph_layer, bias=False)
self.theta = torch.tensor(args.al_scale)
self.zeros = torch.zeros(1)
if args.ngpu > 0:
self.theta = self.theta.cuda()
self.zeros = self.zeros.cuda()
def embede_graph(self, X):
c_hs, c_adjs1, c_adjs2, c_valid = X
c_hs = self.embede(c_hs)
attention = None
for k in range(len(self.gconv1)):
if self.branch == "left":
if k == len(self.gconv1) - 1:
c_hs1, attention = self.gconv1[k](c_hs, c_adjs1, True)
else:
c_hs1 = self.gconv1[k](c_hs, c_adjs1)
c_hs1 = - c_hs1
elif self.branch == "right":
c_hs1 = 0
else:
c_hs1 = self.gconv1[k](c_hs, c_adjs1)
if self.branch == "left":
c_hs2 = 0
else:
if k == len(self.gconv1) - 1:
c_hs2, attention = self.gconv1[k](c_hs, c_adjs2, True)
else:
c_hs2 = self.gconv1[k](c_hs, c_adjs2)
c_hs = c_hs2 - c_hs1
c_hs = F.dropout(c_hs, p=self.dropout_rate, training=self.training)
c_hs = c_hs * c_valid.unsqueeze(-1).repeat(1, 1, c_hs.size(-1))
c_hs = c_hs.sum(1) / c_valid.sum(1, keepdim=True)
return c_hs, F.normalize(attention)
def fully_connected(self, c_hs):
# regularization = torch.empty(len(self.FC)*1-1, device=c_hs.device)
for k in range(len(self.FC)):
if k < len(self.FC) - 1:
c_hs = self.FC[k](c_hs)
c_hs = F.dropout(c_hs, p=self.dropout_rate,
training=self.training)
c_hs = F.relu(c_hs)
else:
c_hs = self.FC[k](c_hs)
c_hs = torch.sigmoid(c_hs)
return c_hs
def forward(self, X, attn_masking=None, training=False):
# embede a graph to a vector
c_hs, attention = self.embede_graph(X)
# fully connected NN
c_hs = self.fully_connected(c_hs)
c_hs = c_hs.view(-1)
# note that if you don't use concrete dropout, regularization 1-2 is zero
if training:
return c_hs, self.cal_attn_loss(attention, attn_masking)
else:
return c_hs
def cal_attn_loss(self, attention, attn_masking):
mapping, samelb = attn_masking
top = torch.exp(-(attention * mapping))
top = torch.where(mapping == 1.0, top, self.zeros)
top = top.sum((1, 2))
topabot = torch.exp(-(attention * samelb))
topabot = torch.where(samelb == 1.0, topabot, self.zeros)
topabot = topabot.sum((1, 2))
return (top / (topabot - top + 1)).sum(0) * self.theta / attention.shape[0]
def get_refined_adjs2(self, X):
_, attention = self.embede_graph(X)
return attention