-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.py
366 lines (292 loc) · 13.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import numpy as np
from sklearn.metrics import average_precision_score, roc_auc_score
################################################################################################
################################################################################################
################################################################################################
def compute_ap_score(pred_pos, pred_neg, neg_samples):
y_pred = torch.cat([pred_pos, pred_neg], dim=0).sigmoid().cpu().detach()
y_true = torch.cat([torch.ones_like(pred_pos), torch.zeros_like(pred_neg)], dim=0).cpu().detach()
acc = average_precision_score(y_true, y_pred)
if neg_samples > 1:
auc = torch.sum(pred_pos.squeeze() < pred_neg.squeeze().reshape(neg_samples, -1), dim=0)
auc = 1 / (auc+1)
else:
auc = roc_auc_score(y_true, y_pred)
return acc, auc
################################################################################################
################################################################################################
################################################################################################
"""
Module: Time-encoder
"""
class TimeEncode(nn.Module):
"""
out = linear(time_scatter): 1-->time_dims
out = cos(out)
"""
def __init__(self, dim):
super(TimeEncode, self).__init__()
self.dim = dim
self.w = nn.Linear(1, dim)
self.reset_parameters()
def reset_parameters(self, ):
self.w.weight = nn.Parameter((torch.from_numpy(1 / 10 ** np.linspace(0, 9, self.dim, dtype=np.float32))).reshape(self.dim, -1))
self.w.bias = nn.Parameter(torch.zeros(self.dim))
self.w.weight.requires_grad = False
self.w.bias.requires_grad = False
@torch.no_grad()
def forward(self, t):
output = torch.cos(self.w(t.reshape((-1, 1))))
return output
################################################################################################
################################################################################################
################################################################################################
"""
Module: MLP-Mixer
"""
class FeedForward(nn.Module):
"""
2-layer MLP with GeLU (fancy version of ReLU) as activation
"""
def __init__(self, dims, expansion_factor, dropout=0, use_single_layer=False):
super().__init__()
self.dims = dims
self.use_single_layer = use_single_layer
self.expansion_factor = expansion_factor
self.dropout = dropout
if use_single_layer:
self.linear_0 = nn.Linear(dims, dims)
else:
self.linear_0 = nn.Linear(dims, int(expansion_factor * dims))
self.linear_1 = nn.Linear(int(expansion_factor * dims), dims)
self.reset_parameters()
def reset_parameters(self):
self.linear_0.reset_parameters()
if self.use_single_layer==False:
self.linear_1.reset_parameters()
def forward(self, x):
x = self.linear_0(x)
x = F.gelu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
if self.use_single_layer==False:
x = self.linear_1(x)
x = F.dropout(x, p=self.dropout, training=self.training)
return x
class MixerBlock(nn.Module):
"""
out = X.T + MLP_Layernorm(X.T) # apply token mixing
out = out.T + MLP_Layernorm(out.T) # apply channel mixing
"""
def __init__(self, per_graph_size, dims,
token_expansion_factor=0.5,
channel_expansion_factor=4,
dropout=0,
module_spec=None, use_single_layer=False):
super().__init__()
if module_spec == None:
self.module_spec = ['token', 'channel']
else:
self.module_spec = module_spec.split('+')
if 'token' in self.module_spec:
self.token_layernorm = nn.LayerNorm(dims)
self.token_forward = FeedForward(per_graph_size, token_expansion_factor, dropout, use_single_layer)
if 'channel' in self.module_spec:
self.channel_layernorm = nn.LayerNorm(dims)
self.channel_forward = FeedForward(dims, channel_expansion_factor, dropout, use_single_layer)
def reset_parameters(self):
if 'token' in self.module_spec:
self.token_layernorm.reset_parameters()
self.token_forward.reset_parameters()
if 'channel' in self.module_spec:
self.channel_layernorm.reset_parameters()
self.channel_forward.reset_parameters()
def token_mixer(self, x):
x = self.token_layernorm(x).permute(0, 2, 1)
x = self.token_forward(x).permute(0, 2, 1)
return x
def channel_mixer(self, x):
x = self.channel_layernorm(x)
x = self.channel_forward(x)
return x
def forward(self, x):
if 'token' in self.module_spec:
x = x + self.token_mixer(x)
if 'channel' in self.module_spec:
x = x + self.channel_mixer(x)
return x
class FeatEncode(nn.Module):
"""
Return [raw_edge_feat | TimeEncode(edge_time_stamp)]
"""
def __init__(self, time_dims, feat_dims, out_dims):
super().__init__()
self.time_encoder = TimeEncode(time_dims)
self.feat_encoder = nn.Linear(time_dims + feat_dims, out_dims)
self.reset_parameters()
def reset_parameters(self):
self.time_encoder.reset_parameters()
self.feat_encoder.reset_parameters()
def forward(self, edge_feats, edge_ts):
edge_time_feats = self.time_encoder(edge_ts)
x = torch.cat([edge_feats, edge_time_feats], dim=1)
return self.feat_encoder(x)
class MLPMixer(nn.Module):
"""
Input : [ batch_size, graph_size, edge_dims+time_dims]
Output: [ batch_size, graph_size, output_dims]
"""
def __init__(self, per_graph_size, time_channels,
input_channels, hidden_channels, out_channels,
num_layers=2, dropout=0.5,
token_expansion_factor=0.5,
channel_expansion_factor=4,
module_spec=None, use_single_layer=False
):
super().__init__()
self.per_graph_size = per_graph_size
self.num_layers = num_layers
# input & output classifer
self.feat_encoder = FeatEncode(time_channels, input_channels, hidden_channels)
self.layernorm = nn.LayerNorm(hidden_channels)
self.mlp_head = nn.Linear(hidden_channels, out_channels)
# inner layers
self.mixer_blocks = torch.nn.ModuleList()
for ell in range(num_layers):
if module_spec is None:
self.mixer_blocks.append(
MixerBlock(per_graph_size, hidden_channels,
token_expansion_factor,
channel_expansion_factor,
dropout, module_spec=None,
use_single_layer=use_single_layer)
)
else:
self.mixer_blocks.append(
MixerBlock(per_graph_size, hidden_channels,
token_expansion_factor,
channel_expansion_factor,
dropout, module_spec=module_spec[ell],
use_single_layer=use_single_layer)
)
# init
self.reset_parameters()
def reset_parameters(self):
for layer in self.mixer_blocks:
layer.reset_parameters()
self.feat_encoder.reset_parameters()
self.layernorm.reset_parameters()
self.mlp_head.reset_parameters()
def forward(self, edge_feats, edge_ts, batch_size, inds):
# x : [ batch_size, graph_size, edge_dims+time_dims]
edge_time_feats = self.feat_encoder(edge_feats, edge_ts)
x = torch.zeros((batch_size * self.per_graph_size,
edge_time_feats.size(1))).to(edge_feats.device)
x[inds] = x[inds] + edge_time_feats
x = torch.split(x, self.per_graph_size)
x = torch.stack(x)
# apply to original feats
for i in range(self.num_layers):
# apply to channel + feat dim
x = self.mixer_blocks[i](x)
x = self.layernorm(x)
x = torch.mean(x, dim=1)
x = self.mlp_head(x)
return x
################################################################################################
################################################################################################
################################################################################################
"""
Edge predictor
"""
class EdgePredictor_per_node(torch.nn.Module):
"""
out = linear(src_node_feats) + linear(dst_node_feats)
out = ReLU(out)
"""
def __init__(self, dim_in_time, dim_in_node):
super().__init__()
self.dim_in_time = dim_in_time
self.dim_in_node = dim_in_node
self.src_fc = torch.nn.Linear(dim_in_time + dim_in_node, 100)
self.dst_fc = torch.nn.Linear(dim_in_time + dim_in_node, 100)
self.out_fc = torch.nn.Linear(100, 1)
self.reset_parameters()
def reset_parameters(self, ):
self.src_fc.reset_parameters()
self.dst_fc.reset_parameters()
self.out_fc.reset_parameters()
def forward(self, h, neg_samples=1):
num_edge = h.shape[0] // (neg_samples + 2)
h_src = self.src_fc(h[:num_edge])
h_pos_dst = self.dst_fc(h[num_edge:2 * num_edge])
h_neg_dst = self.dst_fc(h[2 * num_edge:])
h_pos_edge = torch.nn.functional.relu(h_src + h_pos_dst)
h_neg_edge = torch.nn.functional.relu(h_src.tile(neg_samples, 1) + h_neg_dst)
# h_pos_edge = torch.nn.functional.relu(h_pos_dst)
# h_neg_edge = torch.nn.functional.relu(h_neg_dst)
return self.out_fc(h_pos_edge), self.out_fc(h_neg_edge)
class Mixer_per_node(nn.Module):
"""
Wrapper of MLPMixer and EdgePredictor
"""
def __init__(self, mlp_mixer_configs, edge_predictor_configs):
super(Mixer_per_node, self).__init__()
self.time_feats_dim = edge_predictor_configs['dim_in_time']
self.node_feats_dim = edge_predictor_configs['dim_in_node']
if self.time_feats_dim > 0:
self.base_model = MLPMixer(**mlp_mixer_configs)
self.edge_predictor = EdgePredictor_per_node(**edge_predictor_configs)
self.creterion = nn.BCEWithLogitsLoss(reduction='none')
self.reset_parameters()
def reset_parameters(self):
if self.time_feats_dim > 0:
self.base_model.reset_parameters()
self.edge_predictor.reset_parameters()
def forward(self, model_inputs, has_temporal_neighbors, neg_samples, node_feats):
pred_pos, pred_neg = self.predict(model_inputs, has_temporal_neighbors, neg_samples, node_feats)
pos_mask, neg_mask = self.pos_neg_mask(has_temporal_neighbors, neg_samples)
loss_pos = self.creterion(pred_pos, torch.ones_like(pred_pos))[pos_mask].mean()
loss_neg = self.creterion(pred_neg, torch.zeros_like(pred_neg))[neg_mask].mean()
# compute roc and precision score
acc, auc = compute_ap_score(pred_pos, pred_neg, neg_samples)
return loss_pos + loss_neg, acc, auc
def predict(self, model_inputs, has_temporal_neighbors, neg_samples, node_feats):
if self.time_feats_dim > 0 and self.node_feats_dim == 0:
x = self.base_model(*model_inputs)
elif self.time_feats_dim > 0 and self.node_feats_dim > 0:
x = self.base_model(*model_inputs)
x = torch.cat([x, node_feats], dim=1)
elif self.time_feats_dim == 0 and self.node_feats_dim > 0:
x = node_feats
else:
print('Either time_feats_dim or node_feats_dim must larger than 0!')
pred_pos, pred_neg = self.edge_predictor(x, neg_samples=neg_samples)
return pred_pos, pred_neg
def pos_neg_mask(self, mask, neg_samples):
num_edge = len(mask) // (neg_samples + 2)
src_mask = mask[:num_edge]
pos_dst_mask = mask[num_edge:2 * num_edge]
neg_dst_mask = mask[2 * num_edge:]
pos_mask = [(i and j) for i,j in zip(src_mask, pos_dst_mask)]
neg_mask = [(i and j) for i,j in zip(src_mask * neg_samples, neg_dst_mask)]
return pos_mask, neg_mask
################################################################################################
################################################################################################
################################################################################################
"""
Module: Node classifier
"""
class NodeClassificationModel(nn.Module):
def __init__(self, dim_in, dim_hid, num_class):
super(NodeClassificationModel, self).__init__()
self.fc1 = torch.nn.Linear(dim_in, dim_hid)
self.fc2 = torch.nn.Linear(dim_hid, num_class)
def forward(self, x):
x = self.fc1(x)
x = torch.nn.functional.relu(x)
x = self.fc2(x)
return x