-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_mixer.py
236 lines (188 loc) · 8.04 KB
/
mlp_mixer.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
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: Non-periodic Time-encoder
"""
class TimeEncode(torch.nn.Module):
def __init__(self, expand_dim, factor=5):
super(TimeEncode, self).__init__()
self.time_dim = expand_dim
self.factor = factor
self.basis_freq = torch.nn.Parameter((torch.from_numpy(1 / 10 ** np.linspace(0, 9, self.time_dim))).float())
self.phase = torch.nn.Parameter(torch.zeros(self.time_dim).float())
self.linear = torch.nn.Parameter(torch.zeros(1).float())
self.linear_bias = torch.nn.Parameter(torch.zeros(1).float())
def reset_parameters(self):
self.basis_freq = torch.nn.Parameter((torch.from_numpy(1 / 10 ** np.linspace(0, 9, self.time_dim))).float())
self.phase = torch.nn.Parameter(torch.zeros(self.time_dim).float())
self.linear = torch.nn.Parameter(torch.zeros(1).float())
self.linear_bias = torch.nn.Parameter(torch.zeros(1).float())
def forward(self, ts):
# ts: [N, L]
batch_size = ts.size(0)
seq_len = ts.size(1)
ts = ts.view(batch_size, seq_len, 1) # [N, L, 1]
map_ts = ts * self.basis_freq.view(1, 1, -1) # [N, L, time_dim]
map_ts += self.phase.view(1, 1, -1)
harmonic = torch.cos(map_ts) + (self.linear * ts) + self.linear_bias
return harmonic #self.dense(harmonic)
"""
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=2)
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):
# x : [ batch_size, graph_size, edge_dims+time_dims]
x = self.feat_encoder(edge_feats, edge_ts)
# 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