-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
320 lines (278 loc) · 15.4 KB
/
models.py
File metadata and controls
320 lines (278 loc) · 15.4 KB
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
import torch
from d2l import torch as d2l
from torch import nn
import math
import torch.nn.functional as F
from metaconst import TRAFFIC_SCOPE, TRAFFIC_SCOPE_TEMPORAL, TRAFFIC_SCOPE_CONTEXTUAL
class TemporalEncoder(d2l.Encoder):
def __init__(self, packet_len, key_size, query_size, value_size,
num_hiddens, norm_shape, ffn_num_input, ffn_num_hiddens,
num_heads, num_layers, dropout, use_bias=False, **kwargs):
super(TemporalEncoder, self).__init__(**kwargs)
self.num_hiddens = num_hiddens
self.embedding = nn.Linear(packet_len, num_hiddens)
self.pos_encoding = d2l.PositionalEncoding(num_hiddens, dropout)
self.blks = nn.Sequential()
for i in range(num_layers):
self.blks.add_module("block" + str(i),
d2l.EncoderBlock(key_size, query_size, value_size, num_hiddens,
norm_shape, ffn_num_input, ffn_num_hiddens,
num_heads, dropout, use_bias))
self.attention_weights = [None] * len(self.blks)
self.temporal_features = None
self.relu = nn.ReLU()
def forward(self, X, valid_lens, *args):
# Since positional encoding values are between -1 and 1, the embedding
# values are multiplied by the square root of the embedding dimension
# to rescale before they are summed up
X = self.pos_encoding(self.relu(self.embedding(X)) * math.sqrt(self.num_hiddens))
self.attention_weights = [None] * len(self.blks)
for i, blk in enumerate(self.blks):
X = blk(X, valid_lens)
self.attention_weights[i] = blk.attention.attention.attention_weights
self.temporal_features = X
return X
class ContextualEncoder(d2l.Encoder):
def __init__(self, agg_scale_num, freqs_size, key_size, query_size, value_size,
num_hiddens, norm_shape, ffn_num_input, ffn_num_hiddens,
num_heads, num_layers, dropout, use_bias=False, **kwargs):
super(ContextualEncoder, self).__init__(**kwargs)
self.num_hiddens = num_hiddens
self.embedding = nn.Linear(freqs_size, num_hiddens)
self.pos_encoding = d2l.PositionalEncoding(num_hiddens, dropout)
self.segment_encoding = nn.Embedding(agg_scale_num, num_hiddens)
self.blks = nn.Sequential()
for i in range(num_layers):
self.blks.add_module("block" + str(i),
d2l.EncoderBlock(key_size, query_size, value_size, num_hiddens,
norm_shape, ffn_num_input, ffn_num_hiddens,
num_heads, dropout, use_bias))
self.attention_weights = [None] * len(self.blks)
self.contextual_features = None
self.relu = nn.ReLU()
def forward(self, X, contextual_segments, *args):
# Since positional encoding values are between -1 and 1, the embedding
# values are multiplied by the square root of the embedding dimension
# to rescale before they are summed up
X = self.pos_encoding(self.relu(self.embedding(X)) * math.sqrt(self.num_hiddens)) + \
self.segment_encoding(contextual_segments)
self.attention_weights = [None] * len(self.blks)
for i, blk in enumerate(self.blks):
X = blk(X, torch.ones(X.size(0), device=X.device) * X.size(1))
self.attention_weights[i] = blk.attention.attention.attention_weights
self.contextual_features = X
return X
class FusionEncoder(nn.Module):
def __init__(self, temporal_dim, contextual_dim, num_hiddens, num_heads,
norm_shape, ffn_num_input, ffn_num_hiddens, dropout):
super(FusionEncoder, self).__init__()
assert num_hiddens % num_heads == 0, 'num_hiddens should be divided by num_heads'
self.num_heads = num_heads
self.num_hiddens = num_hiddens
self.depth = self.num_hiddens // self.num_heads
self.WQ = nn.Linear(temporal_dim, num_hiddens)
self.WK = nn.Linear(contextual_dim, num_hiddens)
self.WV = nn.Linear(contextual_dim, num_hiddens)
self.dropout = nn.Dropout(dropout)
self.addnorm1 = d2l.AddNorm(norm_shape, dropout)
self.ffn = d2l.PositionWiseFFN(ffn_num_input, ffn_num_hiddens, num_hiddens)
self.addnorm2 = d2l.AddNorm(norm_shape, dropout)
self.attention_weights = None
self.fusion_features = None
def forward(self, temporal_feature, contextual_feature):
batch_size = temporal_feature.shape[0]
q = self.WQ(temporal_feature) # [batch_size, time_seq_len, num_hiddens]
k = self.WK(contextual_feature) # [batch_size, time_scale_len, num_hiddens]
v = self.WV(contextual_feature) # [batch_size, time_scale_len, num_hiddens]
# --> [batch_size, num_heads, time_seq_len, depth]
Q = q.view(batch_size, -1, self.num_heads, self.depth).transpose(1, 2)
# --> [batch_size, num_heads, time_scale_len, depth]
K = k.view(batch_size, -1, self.num_heads, self.depth).transpose(1, 2)
# --> [batch_size, num_heads, time_scale_len, depth]
V = v.view(batch_size, -1, self.num_heads, self.depth).transpose(1, 2)
# --> [batch_size, num_heads, time_seq_len, time_scale_len]
attention_weights = torch.einsum('bnid,bnjd->bnij', Q, K)
attention_weights = attention_weights * math.sqrt(self.num_hiddens)
attention_weights = F.softmax(attention_weights, dim=-1)
out = torch.einsum('bnij,bnjd->bnid', self.dropout(attention_weights), V)
# --> [batch_size, time_seq_len, num_hiddens]
out = out.transpose(1, 2).contiguous().view(batch_size, -1, self.num_hiddens)
out = self.addnorm1(q, out)
out = self.addnorm2(out, self.ffn(out))
self.attention_weights = attention_weights
self.fusion_features = out
return out
class TrafficScope(nn.Module):
def __init__(self, temporal_seq_len, packet_len,
freqs_size, agg_scale_num, agg_points_num,
num_heads, num_layers, num_classes, dropout):
super(TrafficScope, self).__init__()
self.model_name = TRAFFIC_SCOPE
self.temporal_encoder = TemporalEncoder(packet_len, packet_len, packet_len, packet_len,
packet_len, (temporal_seq_len, packet_len),
packet_len, packet_len * 2, num_heads, num_layers, dropout)
self.contextual_encoder = ContextualEncoder(agg_scale_num, freqs_size, freqs_size, freqs_size, freqs_size,
freqs_size, (agg_scale_num * agg_points_num, freqs_size),
freqs_size, freqs_size * 2,
num_heads, num_layers, dropout)
self.fusion_encoder = FusionEncoder(packet_len, freqs_size, packet_len, num_heads,
(temporal_seq_len, packet_len),
packet_len, packet_len * 2, dropout)
self.fc = nn.Linear(temporal_seq_len * packet_len, num_classes)
def forward(self, temporal_data, temporal_valid_len, contextual_data, contextual_segments):
temporal_feature = self.temporal_encoder(temporal_data, temporal_valid_len)
contextual_feature = self.contextual_encoder(contextual_data, contextual_segments)
out = self.fusion_encoder(temporal_feature, contextual_feature)
out = F.softmax(self.fc(torch.flatten(out, start_dim=1)), dim=-1)
return out
def get_temporal_attention_weights(self):
"""
should only call after forward function
:return attention_weights List[attention_weight (batch_size x num_heads x query_size x key_size)]
"""
return self.temporal_encoder.attention_weights
def get_temporal_features(self):
"""
should only call after forward function
:return temporal_features ndarray batch_size x session_len x num_hiddens(=packet_len)
"""
return self.temporal_encoder.temporal_features
def get_contextual_attention_weights(self):
"""
should only call after forward function
:return attention_weights List[attention_weight (batch_size x num_heads x query_size x key_size)]
"""
return self.contextual_encoder.attention_weights
def get_contextual_features(self):
"""
should only call after forward function
:return contextual_features ndarray batch_size x (agg_scale_num x agg_points_num) x num_hiddens(=freqs)
"""
return self.contextual_encoder.contextual_features
def get_fusion_attention_weights(self):
"""
should only call after forward function
:return attention_weights (batch_size x num_heads x query_size x key_size)]
"""
return self.fusion_encoder.attention_weights
def get_fusion_features(self):
"""
should only call after forward function
:return fusion_features ndarray batch_size x temporal_seq_len x num_hiddens(=packet_len)
"""
return self.fusion_encoder.fusion_features
class TrafficScopeTemporal(nn.Module):
def __init__(self, temporal_seq_len, packet_len,
num_heads, num_layers, num_classes, dropout):
super(TrafficScopeTemporal, self).__init__()
self.model_name = TRAFFIC_SCOPE_TEMPORAL
self.temporal_encoder = TemporalEncoder(packet_len, packet_len, packet_len, packet_len,
packet_len, (temporal_seq_len, packet_len),
packet_len, packet_len * 2, num_heads, num_layers, dropout)
self.fc = nn.Linear(temporal_seq_len * packet_len, num_classes)
def forward(self, temporal_data, temporal_valid_len):
temporal_feature = self.temporal_encoder(temporal_data, temporal_valid_len)
out = F.softmax(self.fc(torch.flatten(temporal_feature, start_dim=1)), dim=-1)
return out
def get_attention_weights(self):
"""
should only call after forward function
:return attention_weights List[attention_weight (batch_size x num_heads x query_size x key_size)]
"""
return self.temporal_encoder.attention_weights
def get_temporal_features(self):
"""
should only call after forward function
:return temporal_features ndarray batch_size x session_len x num_hiddens(=packet_len)
"""
return self.temporal_encoder.temporal_features
class TrafficScopeContextual(nn.Module):
def __init__(self, agg_scale_num, agg_points_num, freqs_size,
num_heads, num_layers, num_classes, dropout):
super(TrafficScopeContextual, self).__init__()
self.model_name = TRAFFIC_SCOPE_CONTEXTUAL
self.contextual_encoder = ContextualEncoder(agg_scale_num, freqs_size, freqs_size, freqs_size, freqs_size,
freqs_size, (agg_scale_num * agg_points_num, freqs_size),
freqs_size, freqs_size * 2,
num_heads, num_layers, dropout)
self.fc = nn.Linear(agg_scale_num * agg_points_num * freqs_size, num_classes)
def forward(self, contextual_data, contextual_segments):
contextual_feature = self.contextual_encoder(contextual_data, contextual_segments)
out = F.softmax(self.fc(torch.flatten(contextual_feature, start_dim=1)), dim=-1)
return out
def get_attention_weights(self):
"""
should only call after forward function
:return attention_weights List[attention_weight (batch_size x num_heads x query_size x key_size)]
"""
return self.contextual_encoder.attention_weights
def get_contextual_features(self):
"""
should only call after forward function
:return contextual_features ndarray batch_size x (agg_scale_num x agg_points_num) x num_hiddens(=freqs)
"""
return self.contextual_encoder.contextual_features
# if __name__ == '__main__':
# temporal_encoder = TemporalEncoder(64, 64, 64, 64, 64, (64, 64), 64, 128, 8, 2, 0.5)
# contextual_encoder = ContextualEncoder(3, 128, 128, 128, 128, 128, (384, 128), 128, 256, 8, 2, 0.5)
# fusion_encoder = FusionEncoder(64, 128, 64, 8, (64, 64), 64, 128, 0.5)
#
# temporal_feature = temporal_encoder(torch.ones((2, 64, 64), dtype=torch.float), torch.tensor([32, 64]))
# print(f'{temporal_feature = }')
# print(f'{temporal_feature.size() = }')
# loss = 20000 * temporal_feature.sum()
# loss.backward()
# print(f'temporal encoder grads {temporal_encoder.embedding.weight.grad}')
#
# contextual_feature = contextual_encoder(torch.ones((2, 384, 128), dtype=torch.float),
# torch.tensor([0] * 128 + [1] * 128 + [2] * 128))
# print(f'{contextual_feature = }')
# print(f'{contextual_feature.size() = }')
# loss = 20000 * contextual_feature.sum()
# loss.backward()
# print(f'contextual encoder grads {contextual_encoder.embedding.weight.grad}')
#
# temporal_feature = temporal_encoder(torch.ones((2, 64, 64), dtype=torch.float), torch.tensor([32, 64]))
# contextual_feature = contextual_encoder(torch.ones((2, 384, 128), dtype=torch.float),
# torch.tensor([0] * 128 + [1] * 128 + [2] * 128))
# out = fusion_encoder(temporal_feature, contextual_feature)
# print(f'{out = }')
# print(f'{out.size() = }')
# loss = 20000 * out.sum()
# loss.backward()
# print(f'fusion encoder grads {fusion_encoder.WQ.weight.grad}')
#
# model = TrafficScope(64, 64, 128, 3, 128, 8, 2, 6, 0.5)
# preds = model(torch.ones((2, 64, 64), dtype=torch.float), torch.tensor([32, 64]),
# torch.ones((2, 384, 128), dtype=torch.float),
# torch.tensor([0] * 128 + [1] * 128 + [2] * 128)
# )
#
# print(f'{preds = }')
# print(f'{model.get_temporal_features().size()}')
# print(f'{model.get_contextual_features().size()}')
# print(f'{model.get_fusion_features().size()}')
# print(f'temporal attention weights = {model.get_temporal_attention_weights()}')
# print(f'contextual attention weights = {model.get_contextual_attention_weights()}')
# print(f'fusion attention_weights = {model.get_fusion_attention_weights()}')
# loss_fn = nn.CrossEntropyLoss()
# loss = loss_fn(preds, torch.tensor([0, 1]))
# loss.backward()
# print(f'TrafficScope grads {model.temporal_encoder.embedding.weight.grad}')
#
# model = TrafficScopeTemporal(64, 64, 8, 2, 6, 0.5)
# preds = model(torch.ones((2, 64, 64), dtype=torch.float), torch.tensor([32, 64]))
# print(f'{preds = }')
# loss_fn = nn.CrossEntropyLoss()
# loss = loss_fn(preds, torch.tensor([0, 1]))
# loss.backward()
# print(f'TrafficScopeTemporal grads {model.temporal_encoder.embedding.weight.grad}')
#
# model = TrafficScopeContextual(3, 128, 128, 8, 2, 6, 0.5)
# preds = model(torch.ones((2, 384, 128), dtype=torch.float),
# torch.tensor([0] * 128 + [1] * 128 + [2] * 128))
# print(f'{preds = }')
# loss_fn = nn.CrossEntropyLoss()
# loss = loss_fn(preds, torch.tensor([0, 1]))
# loss.backward()
# print(f'TrafficScopeContextual grads {model.contextual_encoder.embedding.weight.grad}')