-
Notifications
You must be signed in to change notification settings - Fork 0
/
MgRL.py
480 lines (415 loc) · 26.5 KB
/
MgRL.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# -*- coding: utf-8 -*-
# @Time : 2024/4/2 15:31
# @Author : Karry Ren
""" Three models:
- The basic Multi-Granularity Residual Learning Net: MgRLNet.
- The attention Multi-Granularity Residual Learning Net: MgRL_Attention_Net.
- The Multi-granularity Residual Learning Framework with Confidence Estimation: MgRL_CE_Net.
"""
from typing import Dict, List
import torch
from torch import nn
import torch.nn.functional as F
from models.modules import FeatureEncoder, FeatureEncoderCE, NoPred_FeatureEncoder
from models.modules import ScaledDotProductAttention
class MgRL_Net(nn.Module):
""" The basic Multi-Granularity Residual Learning Net. """
def __init__(
self, granularity_dict: Dict[str, int], ga_K: int,
encoding_input_size: int, encoding_hidden_size: int, device: torch.device
):
""" The init function of MgRL Net.
There are 2 main parts of Multi-Granularity Residual Learning Net:
- Part 1. Granularity Alignment Module (granularity_alignment): Align the different granularity features to K dim
- Part 2. Feature Encoding Module (feature_encoder): Encoding the K dim feature and get the 3 outputs
:param granularity_dict: the dict of input data granularity, should be format like: { "g1": g_1, "g2": g_2, ..., "gG":g_G}
:param ga_K: the K of Granularity Alignment
:param encoding_input_size: the input size of encoding feature
:param encoding_hidden_size: the hidden size of encoding feature
:param device: the computing device
"""
super(MgRL_Net, self).__init__()
self.device = device
# ---- Part 1. Granularity Alignment Module (Each Module includes 1 Linear Layer) ---- #
self.granularity_alignment_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.granularity_alignment_dict["g1"] = nn.Linear(in_features=granularity_dict["g1"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 2 (fine)
self.granularity_alignment_dict["g2"] = nn.Linear(in_features=granularity_dict["g2"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 3 (finer)
self.granularity_alignment_dict["g3"] = nn.Linear(in_features=granularity_dict["g3"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 4 (finer)
self.granularity_alignment_dict["g4"] = nn.Linear(in_features=granularity_dict["g4"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 5 (finest)
self.granularity_alignment_dict["g5"] = nn.Linear(in_features=granularity_dict["g5"], out_features=ga_K,
bias=False).to(device=device)
# ---- Part 2. Feature Encoding Module (Each Module includes 3 Parts) ---- #
self.feature_encoder_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.feature_encoder_dict["g1"] = FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 2 (fine)
self.feature_encoder_dict["g2"] = FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 3 (finer)
self.feature_encoder_dict["g3"] = FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 4 (finer)
self.feature_encoder_dict["g4"] = FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 5 (finest)
self.feature_encoder_dict["g5"] = FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
def forward(self, mul_granularity_input: Dict[str, torch.Tensor]) -> dict:
""" The forward function of MgRL Net.
There are 2 main steps during forward:
- Step 1. Align the granularity
- Step 2. Encoding feature with the residual learning framework
:param mul_granularity_input: the input multi granularity, a dict with the format:
{
"g1": feature_g1,
"g2": feature_g2,
...,
"gG": feature_gG
}
returns: output, a dict with format:
{
"pred" : the prediction result, shape=(bs, 1),
"rec_residuals" : a tuple of reconstruction residual, each residual have the same shape=(bs, T, D*K)
}
"""
# ---- Step 0. Get the different granularity feature ---- #
# - g1 feature (coarsest), shape=(bs, T, K^g1, D)
feature_g1 = mul_granularity_input["g1"].to(dtype=torch.float32, device=self.device)
# - g2 feature (fine), shape=(bs, T, K^g2, D)
feature_g2 = mul_granularity_input["g2"].to(dtype=torch.float32, device=self.device)
# - g3 feature (finer), shape=(bs, T, K^g3, D)
feature_g3 = mul_granularity_input["g3"].to(dtype=torch.float32, device=self.device)
# - g4 feature (finer), shape=(bs, T, K^g4, D)
feature_g4 = mul_granularity_input["g4"].to(dtype=torch.float32, device=self.device)
# - g5 feature (finest), shape=(bs, T, K^g5, D)
feature_g5 = mul_granularity_input["g5"].to(dtype=torch.float32, device=self.device)
# get the shape
bs, T = feature_g1.shape[0], feature_g1.shape[1]
# ---- Step 1. Align the granularity ---- #
# transpose the feature for alignment
feature_g1 = feature_g1.permute(0, 1, 3, 2) # shape from (bs, T, K^g1, D) to (bs, T, D, K^g1)
feature_g2 = feature_g2.permute(0, 1, 3, 2) # shape from (bs, T, K^g2, D) to (bs, T, D, K^g2)
feature_g3 = feature_g3.permute(0, 1, 3, 2) # shape from (bs, T, K^g3, D) to (bs, T, D, K^g3)
feature_g4 = feature_g4.permute(0, 1, 3, 2) # shape from (bs, T, K^g4, D) to (bs, T, D, K^g4)
feature_g5 = feature_g5.permute(0, 1, 3, 2) # shape from (bs, T, K^g5, D) to (bs, T, D, K^g5)
# align the feature with linear transform, get the `F`
F_g1 = self.granularity_alignment_dict["g1"](feature_g1).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g2 = self.granularity_alignment_dict["g2"](feature_g2).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g3 = self.granularity_alignment_dict["g3"](feature_g3).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g4 = self.granularity_alignment_dict["g4"](feature_g4).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g5 = self.granularity_alignment_dict["g5"](feature_g5).reshape(bs, T, -1) # shape=(bs, T, D*K)
# ---- Step 2. Encoding Feature with the residual learning framework ---- #
# - g1 feature encoding
P_g1 = F_g1 # the P of granularity is F
H_g1, y_g1, R_g1 = self.feature_encoder_dict["g1"](P_g1)
# - g2 feature encoding
P_g2 = F_g2 - R_g1 # residual learning, shape=(bs, T, D*K)
H_g2, y_g2, R_g2 = self.feature_encoder_dict["g2"](P_g2)
# - g3 feature encoding
P_g3 = F_g3 - R_g2 # residual learning, shape=(bs, T, D*K)
H_g3, y_g3, R_g3 = self.feature_encoder_dict["g3"](P_g3)
# - g4 feature encoding
P_g4 = F_g4 - R_g3 # residual learning, shape=(bs, T, D*K)
H_g4, y_g4, R_g4 = self.feature_encoder_dict["g4"](P_g4)
# - g5 feature encoding
P_g5 = F_g5 - R_g4 # residual learning, shape=(bs, T, D*K)
H_g5, y_g5, R_g5_ = self.feature_encoder_dict["g5"](P_g5)
# ---- Step 3. Return ---- #
# concat the prediction of all granularity
y_all_g = torch.cat([y_g1, y_g2, y_g3, y_g4, y_g5], dim=1) # shape=(bs, 5)
# construct the output and return
output = {
"pred": torch.mean(y_all_g, -1, keepdim=True),
"rec_residuals": (P_g2, P_g3, P_g4, P_g5)
}
return output
class MgRL_Attention_Net(nn.Module):
""" The basic Multi-Granularity Residual Learning Net. """
def __init__(
self, granularity_dict: Dict[str, int], ga_K: int,
encoding_input_size: int, encoding_hidden_size: int, device: torch.device
):
""" The init function of MgRL_Attention Net.
There are 2 main parts of Multi-Granularity Residual Learning Net:
- Part 1. Granularity Alignment Module (granularity_alignment): Align the different granularity features to K dim
- Part 2. Feature Encoding Module (feature_encoder): Encoding the K dim feature and get the 3 outputs
:param granularity_dict: the dict of input data granularity, should be format like: { "g1": g_1, "g2": g_2, ..., "gG":g_G}
:param ga_K: the K of Granularity Alignment
:param encoding_input_size: the input size of encoding feature
:param encoding_hidden_size: the hidden size of encoding feature
:param device: the computing device
"""
super(MgRL_Attention_Net, self).__init__()
self.device = device
# ---- Part 1. Granularity Alignment Module (Each Module includes 1 Linear Layer) ---- #
self.granularity_alignment_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.granularity_alignment_dict["g1"] = nn.Linear(in_features=granularity_dict["g1"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 2 (fine)
self.granularity_alignment_dict["g2"] = nn.Linear(in_features=granularity_dict["g2"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 3 (finer)
self.granularity_alignment_dict["g3"] = nn.Linear(in_features=granularity_dict["g3"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 4 (finer)
self.granularity_alignment_dict["g4"] = nn.Linear(in_features=granularity_dict["g4"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 5 (finest)
self.granularity_alignment_dict["g5"] = nn.Linear(in_features=granularity_dict["g5"], out_features=ga_K,
bias=False).to(device=device)
# ---- Part 2. Feature Encoding Module (Each Module includes 2 Parts) ---- #
self.feature_encoder_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.feature_encoder_dict["g1"] = NoPred_FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 2 (fine)
self.feature_encoder_dict["g2"] = NoPred_FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 3 (finer)
self.feature_encoder_dict["g3"] = NoPred_FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 4 (finer)
self.feature_encoder_dict["g4"] = NoPred_FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# Granularity 5 (finest)
self.feature_encoder_dict["g5"] = NoPred_FeatureEncoder(encoding_input_size, encoding_hidden_size).to(device=device)
# ---- Part 3. Attention Module ---- #
self.attention = ScaledDotProductAttention().to(device=device)
# ---- Part 4. Pred Net ---- #
self.pred_net_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.pred_net_dict["g1"] = nn.Linear(in_features=encoding_hidden_size, out_features=1).to(device=device)
# Granularity 2 (fine)
self.pred_net_dict["g2"] = nn.Linear(in_features=encoding_hidden_size, out_features=1).to(device=device)
# Granularity 3 (finer)
self.pred_net_dict["g3"] = nn.Linear(in_features=encoding_hidden_size, out_features=1).to(device=device)
# Granularity 4 (finer)
self.pred_net_dict["g4"] = nn.Linear(in_features=encoding_hidden_size, out_features=1).to(device=device)
# Granularity 5 (finest)
self.pred_net_dict["g5"] = nn.Linear(in_features=encoding_hidden_size, out_features=1).to(device=device)
def forward(self, mul_granularity_input: Dict[str, torch.Tensor]) -> dict:
""" The forward function of MgRL_Attention Net.
There are 2 main steps during forward:
- Step 1. Align the granularity
- Step 2. Encoding feature with the residual learning framework
:param mul_granularity_input: the input multi granularity, a dict with the format:
{
"g1": feature_g1,
"g2": feature_g2,
...,
"gG": feature_gG
}
returns: output, a dict with format:
{
"pred" : the prediction result, shape=(bs, 1),
"rec_residuals" : a tuple of reconstruction residual, each residual have the same shape=(bs, T, D*K)
}
"""
# ---- Step 0. Get the different granularity feature ---- #
# - g1 feature (coarsest), shape=(bs, T, K^g1, D)
feature_g1 = mul_granularity_input["g1"].to(dtype=torch.float32, device=self.device)
# - g2 feature (fine), shape=(bs, T, K^g2, D)
feature_g2 = mul_granularity_input["g2"].to(dtype=torch.float32, device=self.device)
# - g3 feature (finer), shape=(bs, T, K^g3, D)
feature_g3 = mul_granularity_input["g3"].to(dtype=torch.float32, device=self.device)
# - g4 feature (finer), shape=(bs, T, K^g4, D)
feature_g4 = mul_granularity_input["g4"].to(dtype=torch.float32, device=self.device)
# - g5 feature (finest), shape=(bs, T, K^g5, D)
feature_g5 = mul_granularity_input["g5"].to(dtype=torch.float32, device=self.device)
# get the shape
bs, T = feature_g1.shape[0], feature_g1.shape[1]
# ---- Step 1. Align the granularity ---- #
# transpose the feature for alignment
feature_g1 = feature_g1.permute(0, 1, 3, 2) # shape from (bs, T, K^g1, D) to (bs, T, D, K^g1)
feature_g2 = feature_g2.permute(0, 1, 3, 2) # shape from (bs, T, K^g2, D) to (bs, T, D, K^g2)
feature_g3 = feature_g3.permute(0, 1, 3, 2) # shape from (bs, T, K^g3, D) to (bs, T, D, K^g3)
feature_g4 = feature_g4.permute(0, 1, 3, 2) # shape from (bs, T, K^g4, D) to (bs, T, D, K^g4)
feature_g5 = feature_g5.permute(0, 1, 3, 2) # shape from (bs, T, K^g5, D) to (bs, T, D, K^g5)
# align the feature with linear transform, get the `F`
F_g1 = self.granularity_alignment_dict["g1"](feature_g1).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g2 = self.granularity_alignment_dict["g2"](feature_g2).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g3 = self.granularity_alignment_dict["g3"](feature_g3).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g4 = self.granularity_alignment_dict["g4"](feature_g4).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g5 = self.granularity_alignment_dict["g5"](feature_g5).reshape(bs, T, -1) # shape=(bs, T, D*K)
# ---- Step 2. Encoding Feature with the residual learning framework ---- #
# - g1 feature encoding
P_g1 = F_g1 # the P of granularity is F
H_g1, R_g1 = self.feature_encoder_dict["g1"](P_g1)
# - g2 feature encoding
P_g2 = F_g2 - R_g1 # residual learning, shape=(bs, T, D*K)
H_g2, R_g2 = self.feature_encoder_dict["g2"](P_g2)
# - g3 feature encoding
P_g3 = F_g3 - R_g2 # residual learning, shape=(bs, T, D*K)
H_g3, R_g3 = self.feature_encoder_dict["g3"](P_g3)
# - g4 feature encoding
P_g4 = F_g4 - R_g3 # residual learning, shape=(bs, T, D*K)
H_g4, R_g4 = self.feature_encoder_dict["g4"](P_g4)
# - g5 feature encoding
P_g5 = F_g5 - R_g4 # residual learning, shape=(bs, T, D*K)
H_g5, R_g5_ = self.feature_encoder_dict["g5"](P_g5)
# ---- Step 3. Soft attention of last step hidden ---- #
# be careful about the concat sequence, shape=(bs, 5, hidden_size)
H_last_step_g = torch.cat([H_g1[:, -1:, :], H_g2[:, -1:, :], H_g3[:, -1:, :], H_g4[:, -1:, :], H_g5[:, -1:, :]], dim=1)
# do the attention
H_attention_g = self.attention(queries=H_last_step_g, keys=H_last_step_g, values=H_last_step_g)
# ---- Step 4. Get the prediction ---- #
y_g1 = self.pred_net_dict["g1"](H_attention_g[:, 0, :]) # for g1
y_g2 = self.pred_net_dict["g2"](H_attention_g[:, 1, :]) # for g2
y_g3 = self.pred_net_dict["g3"](H_attention_g[:, 2, :]) # for g3
y_g4 = self.pred_net_dict["g4"](H_attention_g[:, 3, :]) # for g4
y_g5 = self.pred_net_dict["g5"](H_attention_g[:, 4, :]) # for g5
# ---- Step 5. Return ---- #
# concat the prediction of all granularity
y_all_g = torch.cat([y_g1, y_g2, y_g3, y_g4, y_g5], dim=1) # shape=(bs, 5)
# construct the output and return
output = {
"pred": torch.mean(y_all_g, -1, keepdim=True),
"rec_residuals": (P_g2, P_g3, P_g4, P_g5)
}
return output
class MgRL_CE_Net(nn.Module):
""" The Multi-granularity Residual Learning Framework with Confidence Estimation (CE). """
def __init__(
self, granularity_dict: Dict[str, int], ga_K: int,
encoding_input_size: int, encoding_hidden_size: int, negative_sample_num: int, device: torch.device
):
""" The init function of MgRL_CE_Net.
There are 2 main parts of Multi-Granularity Residual Learning Net:
- Part 1. Granularity Alignment Module (granularity_alignment): Align the different granularity features to K dim
- Part 2. Feature Encoding With CE Module (feature_encoder): Encoding the K dim feature and get the 3 outputs
:param granularity_dict: the dict of input data granularity, should be format like: { "g1": g_1, "g2": g_2, ..., "gG":g_G}
:param ga_K: the K of Granularity Alignment
:param encoding_input_size: the input size of encoding feature
:param encoding_hidden_size: the hidden size of encoding feature
:param negative_sample_num: the number of negative samples
:param device: the computing device
"""
super(MgRL_CE_Net, self).__init__()
self.device = device
# ---- Part 1. Granularity Alignment Module (Each Module includes 1 Linear Layer) ---- #
self.granularity_alignment_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.granularity_alignment_dict["g1"] = nn.Linear(in_features=granularity_dict["g1"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 2 (fine)
self.granularity_alignment_dict["g2"] = nn.Linear(in_features=granularity_dict["g2"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 3 (finer)
self.granularity_alignment_dict["g3"] = nn.Linear(in_features=granularity_dict["g3"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 4 (finer)
self.granularity_alignment_dict["g4"] = nn.Linear(in_features=granularity_dict["g4"], out_features=ga_K,
bias=False).to(device=device)
# Granularity 5 (finest)
self.granularity_alignment_dict["g5"] = nn.Linear(in_features=granularity_dict["g5"], out_features=ga_K,
bias=False).to(device=device)
# ---- Part 2. Feature Encoding Module (Each Module includes 3 Parts) ---- #
self.feature_encoder_ce_dict = nn.ModuleDict({}) # must use nn.ModuleDict or backward wrong !
# Granularity 1 (coarsest)
self.feature_encoder_ce_dict["g1"] = FeatureEncoderCE(encoding_input_size, encoding_hidden_size,
negative_sample_num).to(device=device)
# Granularity 2 (fine)
self.feature_encoder_ce_dict["g2"] = FeatureEncoderCE(encoding_input_size, encoding_hidden_size,
negative_sample_num).to(device=device)
# Granularity 3 (finer)
self.feature_encoder_ce_dict["g3"] = FeatureEncoderCE(encoding_input_size, encoding_hidden_size,
negative_sample_num).to(device=device)
# Granularity 4 (finer)
self.feature_encoder_ce_dict["g4"] = FeatureEncoderCE(encoding_input_size, encoding_hidden_size,
negative_sample_num).to(device=device)
# Granularity 5 (finest)
self.feature_encoder_ce_dict["g5"] = FeatureEncoderCE(encoding_input_size, encoding_hidden_size,
negative_sample_num).to(device=device)
def forward(self, mul_granularity_input: Dict[str, torch.Tensor]) -> dict:
""" The forward function of MgRL_CE_Net.
There are 2 main steps during forward:
- Step 1. Align the granularity
- Step 2. Encoding feature with the residual learning framework
:param mul_granularity_input: the input multi granularity, a dict with the format:
{
"g1": feature_g1,
"g2": feature_g2,
...,
"gG": feature_gG
}
returns: output, a dict with format:
{
"pred" : the prediction result, shape=(bs, 1),
"rec_residuals" : a tuple of reconstruction residual, each residual have the same shape=(bs, T, D*K),
"contrastive_loss" : a tensor of the trend contrastive loss, shape=(bs, 1)
}
"""
# ---- Step 0. Get the different granularity feature ---- #
# - g1 feature (coarsest), shape=(bs, T, K^g1, D)
feature_g1 = mul_granularity_input["g1"].to(dtype=torch.float32, device=self.device)
# - g2 feature, shape=(bs, T, K^g2, D)
feature_g2 = mul_granularity_input["g2"].to(dtype=torch.float32, device=self.device)
# - g3 feature, shape=(bs, T, K^g3, D)
feature_g3 = mul_granularity_input["g3"].to(dtype=torch.float32, device=self.device)
# - g4 feature, shape=(bs, T, K^g4, D)
feature_g4 = mul_granularity_input["g4"].to(dtype=torch.float32, device=self.device)
# - g5 feature (finest), shape=(bs, T, K^g5, D)
feature_g5 = mul_granularity_input["g5"].to(dtype=torch.float32, device=self.device)
# get the shape
bs, T = feature_g1.shape[0], feature_g1.shape[1]
# ---- Step 1. Align the granularity ---- #
# transpose the feature for alignment
feature_g1 = feature_g1.permute(0, 1, 3, 2) # shape from (bs, T, K^g1, D) to (bs, T, D, K^g1)
feature_g2 = feature_g2.permute(0, 1, 3, 2) # shape from (bs, T, K^g2, D) to (bs, T, D, K^g2)
feature_g3 = feature_g3.permute(0, 1, 3, 2) # shape from (bs, T, K^g3, D) to (bs, T, D, K^g3)
feature_g4 = feature_g4.permute(0, 1, 3, 2) # shape from (bs, T, K^g4, D) to (bs, T, D, K^g4)
feature_g5 = feature_g5.permute(0, 1, 3, 2) # shape from (bs, T, K^g5, D) to (bs, T, D, K^g5)
# align the feature with linear transform, get the `F`
F_g1 = self.granularity_alignment_dict["g1"](feature_g1).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g2 = self.granularity_alignment_dict["g2"](feature_g2).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g3 = self.granularity_alignment_dict["g3"](feature_g3).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g4 = self.granularity_alignment_dict["g4"](feature_g4).reshape(bs, T, -1) # shape=(bs, T, D*K)
F_g5 = self.granularity_alignment_dict["g5"](feature_g5).reshape(bs, T, -1) # shape=(bs, T, D*K)
# ---- Step 2. Encoding Feature with the residual learning framework ---- #
# - g1 feature encoding
P_g1 = F_g1 # the P of granularity is F
H_g1, y_g1, R_g1, alpha_g1, con_l_g1 = self.feature_encoder_ce_dict["g1"](P_g1)
# - g2 feature encoding
P_g2 = F_g2 - R_g1 # residual learning, shape=(bs, T, D*K)
H_g2, y_g2, R_g2, alpha_g2, con_l_g2 = self.feature_encoder_ce_dict["g2"](P_g2)
# - g3 feature encoding
P_g3 = F_g3 - R_g2 # residual learning, shape=(bs, T, D*K)
H_g3, y_g3, R_g3, alpha_g3, con_l_g3 = self.feature_encoder_ce_dict["g3"](P_g3)
# - g4 feature encoding
P_g4 = F_g4 - R_g3 # residual learning, shape=(bs, T, D*K)
H_g4, y_g4, R_g4, alpha_g4, con_l_g4 = self.feature_encoder_ce_dict["g4"](P_g4)
# - g5 feature encoding
P_g5 = F_g5 - R_g4 # residual learning, shape=(bs, T, D*K)
H_g5, y_g5, R_g5, alpha_g5, con_l_g5 = self.feature_encoder_ce_dict["g5"](P_g5)
# ---- Step 3. Return ---- #
# concat the prediction of all granularity
y_all_g = torch.cat([y_g1, y_g2, y_g3, y_g4, y_g5], dim=1) # shape=(bs, 5)
# concat the alpha of all granularity
alpha_all_g = torch.cat([alpha_g1, alpha_g2, alpha_g3, alpha_g4, alpha_g5], dim=1) # shape=(bs, 5)
alpha_all_g = F.softmax(alpha_all_g, dim=1) # use the soft_max to weight, shape=(bs, 5)
# use the alpha to weight the y
y_all_g = y_all_g * alpha_all_g
# construct the output and return, contrastive_loss is sum of all granularity
output = {
"pred": torch.mean(y_all_g, -1, keepdim=True),
"rec_residuals": (P_g2, P_g3, P_g4, P_g5),
"contrastive_loss": con_l_g1 + con_l_g2 + con_l_g3 + con_l_g4 + con_l_g5
}
return output
if __name__ == "__main__":
bath_size, time_steps, D = 64, 4, 1
mg_input = {
"g1": torch.ones((bath_size, time_steps, 1, D)),
"g2": torch.ones((bath_size, time_steps, 2, D)),
"g3": torch.ones((bath_size, time_steps, 6, D)),
"g4": torch.ones((bath_size, time_steps, 24, D)),
"g5": torch.ones((bath_size, time_steps, 96, D))
}
g_dict = {"g1": 1, "g2": 2, "g3": 6, "g4": 24, "g5": 96}
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MgRL_CE_Net(granularity_dict=g_dict, ga_K=2, encoding_input_size=2, encoding_hidden_size=7, device=dev, negative_sample_num=5)
out = model(mg_input)
print(out["pred"].shape)
# print(out["rec_residuals"])