-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
285 lines (221 loc) · 10.2 KB
/
model.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import dgl
import dgl.function as fn
from dgl.utils import expand_as_pair, check_eq_shape
import torch as th
import torch.nn as nn
import torch.nn.functional as F
from sklearn.metrics import roc_auc_score, precision_recall_curve, auc
def prepare_mp(g):
"""
Explicitly materialize the CSR, CSC and COO representation of the given graph
so that they could be shared via copy-on-write to sampler workers and GPU trainers.
"""
g.in_degree(0)
g.out_degree(0)
g.find_edges([0])
def load_subtensor(features, weights, input_nodes, input_eid_lst):
"""
Copys features and labels of a set of nodes onto GPU.
"""
batch_features = features[input_nodes]
batch_weights = []
for eid in input_eid_lst:
batch_weight = weights[eid]
batch_weights.append(batch_weight)
return batch_features, batch_weights
class NeighborCollector(object):
def __init__(self, g, num_layers, heads_all, tails_all, neg_heads_all, num_negs):
self.g = g
self.num_layers = num_layers
self.heads_all = heads_all
self.tails_all = tails_all
self.neg_heads_all = neg_heads_all
self.num_negs = num_negs
def get_blocks(self, seed_edges):
n_edges = len(seed_edges)
heads = self.heads_all[seed_edges]
tails = self.tails_all[seed_edges]
neg_heads = self.neg_heads_all[seed_edges].view(-1)
neg_tails = tails.view(-1, 1).expand(n_edges, self.num_negs).flatten()
# Maintain the correspondence between heads, tails and negative tails as two graphs.
# pos_graph contains the correspondence between each head and its positive tail.
# neg_graph contains the correspondence between each head and its negative tails.
# Both pos_graph and neg_graph are first constructed with the same node space as
# the original graph. Then they are compacted together with dgl.compact_graphs.
pos_graph = dgl.graph((heads, tails), num_nodes=self.g.number_of_nodes())
neg_graph = dgl.graph((neg_heads, neg_tails), num_nodes=self.g.number_of_nodes())
pos_graph, neg_graph = dgl.compact_graphs([pos_graph, neg_graph])
# Obtain the node IDs being used in either pos_graph or neg_graph.
# Since they are compacted together, pos_graph and neg_graph share
# the same compacted node space.
seeds = pos_graph.ndata[dgl.NID]
blocks = []
for i in range(self.num_layers):
# For each seed node, get the neighbors.
frontier = dgl.in_subgraph(self.g, seeds)
# Then we compact the frontier into a bipartite graph for message passing.
block = dgl.to_block(frontier, seeds)
block.edata[dgl.EID] = frontier.edata[dgl.EID]
# Obtain the seed nodes for next layer.
seeds = block.srcdata[dgl.NID]
blocks.insert(0, block)
return pos_graph, neg_graph, blocks
def get_neighbors(g, batch_nodes, layer_num):
blocks = []
block_eids = []
seeds = batch_nodes
for i in range(layer_num):
g_sub = dgl.in_subgraph(g, seeds)
block = dgl.to_block(g_sub, seeds)
block_eid = g_sub.edata[dgl.EID]
blocks.insert(0, block)
block_eids.insert(0, block_eid)
seeds = block.srcdata[dgl.NID]
return blocks, block_eids
class ModelLayer(nn.Module):
def __init__(self,
in_feats,
out_feats,
feat_drop=0.,
bias=True,
norm=None,
activation=None):
super(ModelLayer, self).__init__()
self._in_src_feats, self._in_dst_feats = expand_as_pair(in_feats)
self._out_feats = out_feats
self.norm = norm
self.feat_drop = nn.Dropout(feat_drop)
self.activation = activation
self.fc_pool = nn.Linear(self._in_src_feats, self._in_src_feats)
self.fc_self = nn.Linear(self._in_dst_feats, out_feats, bias=bias)
self.fc_neigh = nn.Linear(self._in_src_feats, out_feats, bias=bias)
self.reset_parameters()
def reset_parameters(self):
"""Reinitialize learnable parameters."""
gain = nn.init.calculate_gain('relu')
nn.init.xavier_uniform_(self.fc_pool.weight, gain=gain)
nn.init.xavier_uniform_(self.fc_neigh.weight, gain=gain)
def forward(self, graph, feat, weight):
graph = graph.local_var()
if isinstance(feat, tuple):
feat_src = self.feat_drop(feat[0])
feat_dst = self.feat_drop(feat[1])
else:
feat_src = feat_dst = self.feat_drop(feat)
h_self = feat_dst
graph.srcdata['h'] = F.relu(self.fc_pool(feat_src))
graph.edata['w'] = weight
graph.update_all(fn.u_mul_e('h', 'w', 'm'), fn.max('m', 'neigh'))
h_neigh = graph.dstdata['neigh']
rst = self.fc_self(h_self) + self.fc_neigh(h_neigh)
# activation
if self.activation is not None:
rst = self.activation(rst)
# normalization
if self.norm is not None:
rst = self.norm(rst)
return rst
class Model(nn.Module):
def __init__(self,
in_feats,
n_hidden,
n_layers,
activation,
layer_dropout,
emb_dropout):
super().__init__()
self.n_layers = n_layers
self.n_hidden = n_hidden
self.layers = nn.ModuleList()
self.layers.append(ModelLayer(in_feats, n_hidden[0], layer_dropout))
for i in range(1, n_layers):
self.layers.append(ModelLayer(n_hidden[i-1], n_hidden[i], layer_dropout))
self.dropout = nn.Dropout(emb_dropout)
self.activation = activation
def forward(self, blocks, x, weights):
h = x
for l, (layer, block, weight) in enumerate(zip(self.layers, blocks, weights)):
# We need to first copy the representation of nodes on the RHS from the
# appropriate nodes on the LHS.
# Note that the shape of h is (num_nodes_LHS, D) and the shape of h_dst
# would be (num_nodes_RHS, D)
h_dst = h[:block.number_of_dst_nodes()]
# Then we compute the updated representation on the RHS.
# The shape of h now becomes (num_nodes_RHS, D)
h = layer(block, (h, h_dst),weight)
if l != len(self.layers) - 1:
h = self.activation(h)
h = self.dropout(h)
return h
def inference(self, g, nodes, x, weights, batch_size):
y = th.zeros(len(nodes), self.n_hidden[-1])
layer_num = len(self.layers)
for start in range(0, len(nodes), batch_size):
end = start + batch_size
batch_nodes = nodes[start:end]
blocks, block_eids = get_neighbors(g, batch_nodes, layer_num)
input_nodes = blocks[0].srcdata[dgl.NID]
batch_features, batch_weights = load_subtensor(x, weights, input_nodes, block_eids)
h = self.forward(blocks, batch_features, batch_weights)
y[start:end] = h.cpu()
return y
def score_calculate(embs, pos_graph, neg_graph):
with pos_graph.local_scope():
pos_graph.ndata['h'] = embs
pos_graph.apply_edges(fn.u_dot_v('h', 'h', 'score'))
pos_score = pos_graph.edata['score']
with neg_graph.local_scope():
neg_graph.ndata['h'] = embs
neg_graph.apply_edges(fn.u_dot_v('h', 'h', 'score'))
neg_score = neg_graph.edata['score']
return pos_score, neg_score
class FocalLoss(nn.Module):
def __init__(self, alpha=0.25, gamma=2., logits='sigmoid', reduce=True):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduce = reduce
def forward(self, block_outputs, pos_graph, neg_graph):
pos_score, neg_score = score_calculate(block_outputs, pos_graph, neg_graph)
scores = th.cat([pos_score, neg_score])
labels = th.cat([th.ones_like(pos_score), th.zeros_like(neg_score)])
if self.logits == 'sigmoid':
BCE_loss = F.binary_cross_entropy_with_logits(scores, labels, reduction='none')
elif self.logits == 'relu':
# relu not work for backward
scores = nn.Hardtanh(0., 1.)(scores)
BCE_loss = F.binary_cross_entropy(scores, labels, reduction='none')
pt = th.exp(-BCE_loss)
alpha_t = labels*self.alpha + (th.ones_like(labels)-labels)*(1-self.alpha)
f_loss = alpha_t * (1-pt)**self.gamma * BCE_loss
if self.reduce:
return th.mean(f_loss), pos_score, neg_score
else:
return f_loss, pos_score, neg_score
def index_calculate(pos_score, neg_score):
pos_score = th.sigmoid(pos_score)
neg_score = th.sigmoid(neg_score)
true_pos = th.sum(pos_score >= 0.5).item()
all_pos = len(pos_score)
true_neg = th.sum(neg_score < 0.5).item()
all_neg = len(neg_score)
accuracy = (true_pos + true_neg) / (all_pos + all_neg)
labels = th.cat([th.ones_like(pos_score), th.zeros_like(neg_score)]).detach().numpy()
scores = th.cat([pos_score, neg_score]).detach().numpy()
precision_feat, recall_feat, thresholds_feat = precision_recall_curve(labels, scores)
auprc = auc(recall_feat, precision_feat)
auroc = roc_auc_score(labels, scores)
return accuracy, auprc, auroc
def evaluate(model, g, nodes, node_features, edge_weights, batch_size, heads, tails, neg_heads, neg_tails):
model.eval()
with th.no_grad():
preds = model.inference(g, nodes, node_features, edge_weights, batch_size)
embs = th.zeros(g.number_of_nodes(), preds.size()[-1])
embs[nodes] = preds
pos_graph = dgl.graph((heads, tails), num_nodes=g.number_of_nodes())
neg_graph = dgl.graph((neg_heads, neg_tails), num_nodes=g.number_of_nodes())
pos_graph, neg_graph = dgl.compact_graphs([pos_graph, neg_graph])
pos_score, neg_score = score_calculate(embs[pos_graph.ndata[dgl.NID]], pos_graph, neg_graph)
accuracy, auprc, auroc = index_calculate(pos_score, neg_score)
return accuracy, auprc, auroc