Shourya Bose
commited on
Commit
·
6dd3ebe
1
Parent(s):
001cf70
upload model definitions and weights
Browse files- __pycache__/model_kwargs.cpython-310.pyc +0 -0
- model_kwargs.py +55 -0
- models/Autoformer.py +567 -0
- models/Informer.py +459 -0
- models/LSTM.py +57 -0
- models/LSTNet.py +95 -0
- models/TimesNet.py +262 -0
- models/Transformer.py +396 -0
- models/__pycache__/Autoformer.cpython-310.pyc +0 -0
- models/__pycache__/LSTM.cpython-310.pyc +0 -0
- models/__pycache__/LSTNet.cpython-310.pyc +0 -0
- weights/autoformer_L_96_T_48_HET.pth +3 -0
- weights/autoformer_L_96_T_48_HOM.pth +3 -0
- weights/autoformer_L_96_T_4_HET.pth +3 -0
- weights/autoformer_L_96_T_4_HOM.pth +3 -0
- weights/autoformer_L_96_T_96_HET.pth +3 -0
- weights/autoformer_L_96_T_96_HOM.pth +3 -0
- weights/informer_L_96_T_48_HET.pth +3 -0
- weights/informer_L_96_T_48_HOM.pth +3 -0
- weights/informer_L_96_T_4_HET.pth +3 -0
- weights/informer_L_96_T_4_HOM.pth +3 -0
- weights/informer_L_96_T_96_HET.pth +3 -0
- weights/informer_L_96_T_96_HOM.pth +3 -0
- weights/lstm_L_96_T_48_HET.pth +3 -0
- weights/lstm_L_96_T_48_HOM.pth +3 -0
- weights/lstm_L_96_T_4_HET.pth +3 -0
- weights/lstm_L_96_T_4_HOM.pth +3 -0
- weights/lstm_L_96_T_96_HET.pth +3 -0
- weights/lstm_L_96_T_96_HOM.pth +3 -0
- weights/lstnet_L_96_T_48_HET.pth +3 -0
- weights/lstnet_L_96_T_48_HOM.pth +3 -0
- weights/lstnet_L_96_T_4_HET.pth +3 -0
- weights/lstnet_L_96_T_4_HOM.pth +3 -0
- weights/lstnet_L_96_T_96_HET.pth +3 -0
- weights/lstnet_L_96_T_96_HOM.pth +3 -0
- weights/timesnet_L_96_T_48_HET.pth +3 -0
- weights/timesnet_L_96_T_48_HOM.pth +3 -0
- weights/timesnet_L_96_T_4_HET.pth +3 -0
- weights/timesnet_L_96_T_4_HOM.pth +3 -0
- weights/timesnet_L_96_T_96_HET.pth +3 -0
- weights/timesnet_L_96_T_96_HOM.pth +3 -0
- weights/transformer_L_96_T_48_HET.pth +3 -0
- weights/transformer_L_96_T_48_HOM.pth +3 -0
- weights/transformer_L_96_T_4_HET.pth +3 -0
- weights/transformer_L_96_T_4_HOM.pth +3 -0
- weights/transformer_L_96_T_96_HET.pth +3 -0
- weights/transformer_L_96_T_96_HOM.pth +3 -0
__pycache__/model_kwargs.cpython-310.pyc
ADDED
|
Binary file (1.26 kB). View file
|
|
|
model_kwargs.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
autoformer_kwargs = lambda lookback,lookahead:{
|
| 2 |
+
'enc_in': 6,
|
| 3 |
+
'dec_in': 2,
|
| 4 |
+
'c_out': 1,
|
| 5 |
+
'pred_len': lookahead,
|
| 6 |
+
'seq_len': lookback,
|
| 7 |
+
'd_model': 32*4,
|
| 8 |
+
'data_idx': [0,3,4,5,6,7],
|
| 9 |
+
'time_idx': [1,2]
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
informer_kwargs = lambda lookback,lookahead:{
|
| 13 |
+
'enc_in': 6,
|
| 14 |
+
'dec_in': 2,
|
| 15 |
+
'c_out': 1,
|
| 16 |
+
'pred_len': lookahead,
|
| 17 |
+
'd_model': 32*4,
|
| 18 |
+
'data_idx': [0,3,4,5,6,7],
|
| 19 |
+
'time_idx': [1,2]
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
timesnet_kwargs = lambda lookback,lookahead:{
|
| 23 |
+
'enc_in': 6,
|
| 24 |
+
'dec_in': 2,
|
| 25 |
+
'c_out': 1,
|
| 26 |
+
'pred_len': lookahead,
|
| 27 |
+
'seq_len': lookback,
|
| 28 |
+
'd_model': 32*4,
|
| 29 |
+
'data_idx': [0,3,4,5,6,7],
|
| 30 |
+
'time_idx': [1,2]
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
transformer_kwargs = lambda lookback,lookahead:{
|
| 34 |
+
'enc_in': 6,
|
| 35 |
+
'dec_in': 2,
|
| 36 |
+
'c_out': 1,
|
| 37 |
+
'pred_len': lookahead,
|
| 38 |
+
'd_model': 32*4,
|
| 39 |
+
'data_idx': [0,3,4,5,6,7],
|
| 40 |
+
'time_idx': [1,2]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
lstm_kwargs = lambda lookback,lookahead:{
|
| 44 |
+
'input_size': 8,
|
| 45 |
+
'hidden_size': 8*4,
|
| 46 |
+
'num_layers': 2,
|
| 47 |
+
'lookback': lookback
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
lstnet_kwargs = lambda lookback,lookahead:{
|
| 51 |
+
'num_features':8,
|
| 52 |
+
'conv1_out_channels':8*4,
|
| 53 |
+
'conv1_kernel_height':3*4,
|
| 54 |
+
'recc1_out_channels':32*4
|
| 55 |
+
}
|
models/Autoformer.py
ADDED
|
@@ -0,0 +1,567 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import math
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
class AutoCorrelation(nn.Module):
|
| 8 |
+
"""
|
| 9 |
+
AutoCorrelation Mechanism with the following two phases:
|
| 10 |
+
(1) period-based dependencies discovery
|
| 11 |
+
(2) time delay aggregation
|
| 12 |
+
This block can replace the self-attention family mechanism seamlessly.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def __init__(self, mask_flag=True, factor=1, scale=None, attention_dropout=0.1, output_attention=False):
|
| 16 |
+
super(AutoCorrelation, self).__init__()
|
| 17 |
+
self.factor = factor
|
| 18 |
+
self.scale = scale
|
| 19 |
+
self.mask_flag = mask_flag
|
| 20 |
+
self.output_attention = output_attention
|
| 21 |
+
self.dropout = nn.Dropout(attention_dropout)
|
| 22 |
+
|
| 23 |
+
def time_delay_agg_training(self, values, corr):
|
| 24 |
+
"""
|
| 25 |
+
SpeedUp version of Autocorrelation (a batch-normalization style design)
|
| 26 |
+
This is for the training phase.
|
| 27 |
+
"""
|
| 28 |
+
head = values.shape[1]
|
| 29 |
+
channel = values.shape[2]
|
| 30 |
+
length = values.shape[3]
|
| 31 |
+
# find top k
|
| 32 |
+
top_k = int(self.factor * math.log(length))
|
| 33 |
+
mean_value = torch.mean(torch.mean(corr, dim=1), dim=1)
|
| 34 |
+
index = torch.topk(torch.mean(mean_value, dim=0), top_k, dim=-1)[1]
|
| 35 |
+
weights = torch.stack([mean_value[:, index[i]] for i in range(top_k)], dim=-1)
|
| 36 |
+
# update corr
|
| 37 |
+
tmp_corr = torch.softmax(weights, dim=-1)
|
| 38 |
+
# aggregation
|
| 39 |
+
tmp_values = values
|
| 40 |
+
delays_agg = torch.zeros_like(values).float()
|
| 41 |
+
for i in range(top_k):
|
| 42 |
+
pattern = torch.roll(tmp_values, -int(index[i]), -1)
|
| 43 |
+
delays_agg = delays_agg + pattern * \
|
| 44 |
+
(tmp_corr[:, i].unsqueeze(1).unsqueeze(1).unsqueeze(1).repeat(1, head, channel, length))
|
| 45 |
+
return delays_agg
|
| 46 |
+
|
| 47 |
+
def time_delay_agg_inference(self, values, corr):
|
| 48 |
+
"""
|
| 49 |
+
SpeedUp version of Autocorrelation (a batch-normalization style design)
|
| 50 |
+
This is for the inference phase.
|
| 51 |
+
"""
|
| 52 |
+
batch = values.shape[0]
|
| 53 |
+
head = values.shape[1]
|
| 54 |
+
channel = values.shape[2]
|
| 55 |
+
length = values.shape[3]
|
| 56 |
+
# index init
|
| 57 |
+
init_index = torch.arange(length).unsqueeze(0).unsqueeze(0).unsqueeze(0).repeat(batch, head, channel, 1).cuda()
|
| 58 |
+
# find top k
|
| 59 |
+
top_k = int(self.factor * math.log(length))
|
| 60 |
+
mean_value = torch.mean(torch.mean(corr, dim=1), dim=1)
|
| 61 |
+
weights, delay = torch.topk(mean_value, top_k, dim=-1)
|
| 62 |
+
# update corr
|
| 63 |
+
tmp_corr = torch.softmax(weights, dim=-1)
|
| 64 |
+
# aggregation
|
| 65 |
+
tmp_values = values.repeat(1, 1, 1, 2)
|
| 66 |
+
delays_agg = torch.zeros_like(values).float()
|
| 67 |
+
for i in range(top_k):
|
| 68 |
+
tmp_delay = init_index + delay[:, i].unsqueeze(1).unsqueeze(1).unsqueeze(1).repeat(1, head, channel, length)
|
| 69 |
+
pattern = torch.gather(tmp_values, dim=-1, index=tmp_delay)
|
| 70 |
+
delays_agg = delays_agg + pattern * \
|
| 71 |
+
(tmp_corr[:, i].unsqueeze(1).unsqueeze(1).unsqueeze(1).repeat(1, head, channel, length))
|
| 72 |
+
return delays_agg
|
| 73 |
+
|
| 74 |
+
def time_delay_agg_full(self, values, corr):
|
| 75 |
+
"""
|
| 76 |
+
Standard version of Autocorrelation
|
| 77 |
+
"""
|
| 78 |
+
batch = values.shape[0]
|
| 79 |
+
head = values.shape[1]
|
| 80 |
+
channel = values.shape[2]
|
| 81 |
+
length = values.shape[3]
|
| 82 |
+
# index init
|
| 83 |
+
init_index = torch.arange(length).unsqueeze(0).unsqueeze(0).unsqueeze(0).repeat(batch, head, channel, 1).cuda()
|
| 84 |
+
# find top k
|
| 85 |
+
top_k = int(self.factor * math.log(length))
|
| 86 |
+
weights, delay = torch.topk(corr, top_k, dim=-1)
|
| 87 |
+
# update corr
|
| 88 |
+
tmp_corr = torch.softmax(weights, dim=-1)
|
| 89 |
+
# aggregation
|
| 90 |
+
tmp_values = values.repeat(1, 1, 1, 2)
|
| 91 |
+
delays_agg = torch.zeros_like(values).float()
|
| 92 |
+
for i in range(top_k):
|
| 93 |
+
tmp_delay = init_index + delay[..., i].unsqueeze(-1)
|
| 94 |
+
pattern = torch.gather(tmp_values, dim=-1, index=tmp_delay)
|
| 95 |
+
delays_agg = delays_agg + pattern * (tmp_corr[..., i].unsqueeze(-1))
|
| 96 |
+
return delays_agg
|
| 97 |
+
|
| 98 |
+
def forward(self, queries, keys, values, attn_mask):
|
| 99 |
+
B, L, H, E = queries.shape
|
| 100 |
+
_, S, _, D = values.shape
|
| 101 |
+
if L > S:
|
| 102 |
+
zeros = torch.zeros_like(queries[:, :(L - S), :]).float()
|
| 103 |
+
values = torch.cat([values, zeros], dim=1)
|
| 104 |
+
keys = torch.cat([keys, zeros], dim=1)
|
| 105 |
+
else:
|
| 106 |
+
values = values[:, :L, :, :]
|
| 107 |
+
keys = keys[:, :L, :, :]
|
| 108 |
+
|
| 109 |
+
# period-based dependencies
|
| 110 |
+
q_fft = torch.fft.rfft(queries.permute(0, 2, 3, 1).contiguous(), dim=-1)
|
| 111 |
+
k_fft = torch.fft.rfft(keys.permute(0, 2, 3, 1).contiguous(), dim=-1)
|
| 112 |
+
res = q_fft * torch.conj(k_fft)
|
| 113 |
+
corr = torch.fft.irfft(res, dim=-1)
|
| 114 |
+
|
| 115 |
+
# time delay agg
|
| 116 |
+
if self.training:
|
| 117 |
+
V = self.time_delay_agg_training(values.permute(0, 2, 3, 1).contiguous(), corr).permute(0, 3, 1, 2)
|
| 118 |
+
else:
|
| 119 |
+
V = self.time_delay_agg_inference(values.permute(0, 2, 3, 1).contiguous(), corr).permute(0, 3, 1, 2)
|
| 120 |
+
|
| 121 |
+
if self.output_attention:
|
| 122 |
+
return (V.contiguous(), corr.permute(0, 3, 1, 2))
|
| 123 |
+
else:
|
| 124 |
+
return (V.contiguous(), None)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class AutoCorrelationLayer(nn.Module):
|
| 128 |
+
def __init__(self, correlation, d_model, n_heads, d_keys=None,
|
| 129 |
+
d_values=None):
|
| 130 |
+
super(AutoCorrelationLayer, self).__init__()
|
| 131 |
+
|
| 132 |
+
d_keys = d_keys or (d_model // n_heads)
|
| 133 |
+
d_values = d_values or (d_model // n_heads)
|
| 134 |
+
|
| 135 |
+
self.inner_correlation = correlation
|
| 136 |
+
self.query_projection = nn.Linear(d_model, d_keys * n_heads)
|
| 137 |
+
self.key_projection = nn.Linear(d_model, d_keys * n_heads)
|
| 138 |
+
self.value_projection = nn.Linear(d_model, d_values * n_heads)
|
| 139 |
+
self.out_projection = nn.Linear(d_values * n_heads, d_model)
|
| 140 |
+
self.n_heads = n_heads
|
| 141 |
+
|
| 142 |
+
def forward(self, queries, keys, values, attn_mask):
|
| 143 |
+
B, L, _ = queries.shape
|
| 144 |
+
_, S, _ = keys.shape
|
| 145 |
+
H = self.n_heads
|
| 146 |
+
|
| 147 |
+
queries = self.query_projection(queries).view(B, L, H, -1)
|
| 148 |
+
keys = self.key_projection(keys).view(B, S, H, -1)
|
| 149 |
+
values = self.value_projection(values).view(B, S, H, -1)
|
| 150 |
+
|
| 151 |
+
out, attn = self.inner_correlation(
|
| 152 |
+
queries,
|
| 153 |
+
keys,
|
| 154 |
+
values,
|
| 155 |
+
attn_mask
|
| 156 |
+
)
|
| 157 |
+
out = out.view(B, L, -1)
|
| 158 |
+
|
| 159 |
+
return self.out_projection(out), attn
|
| 160 |
+
|
| 161 |
+
class my_Layernorm(nn.Module):
|
| 162 |
+
"""
|
| 163 |
+
Special designed layernorm for the seasonal part
|
| 164 |
+
"""
|
| 165 |
+
|
| 166 |
+
def __init__(self, channels):
|
| 167 |
+
super(my_Layernorm, self).__init__()
|
| 168 |
+
self.layernorm = nn.LayerNorm(channels)
|
| 169 |
+
|
| 170 |
+
def forward(self, x):
|
| 171 |
+
x_hat = self.layernorm(x)
|
| 172 |
+
bias = torch.mean(x_hat, dim=1).unsqueeze(1).repeat(1, x.shape[1], 1)
|
| 173 |
+
return x_hat - bias
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
class moving_avg(nn.Module):
|
| 177 |
+
"""
|
| 178 |
+
Moving average block to highlight the trend of time series
|
| 179 |
+
"""
|
| 180 |
+
|
| 181 |
+
def __init__(self, kernel_size, stride):
|
| 182 |
+
super(moving_avg, self).__init__()
|
| 183 |
+
self.kernel_size = kernel_size
|
| 184 |
+
self.avg = nn.AvgPool1d(kernel_size=kernel_size, stride=stride, padding=0)
|
| 185 |
+
|
| 186 |
+
def forward(self, x):
|
| 187 |
+
# padding on the both ends of time series
|
| 188 |
+
front = x[:, 0:1, :].repeat(1, (self.kernel_size - 1) // 2, 1)
|
| 189 |
+
end = x[:, -1:, :].repeat(1, (self.kernel_size - 1) // 2, 1)
|
| 190 |
+
x = torch.cat([front, x, end], dim=1)
|
| 191 |
+
x = self.avg(x.permute(0, 2, 1))
|
| 192 |
+
x = x.permute(0, 2, 1)
|
| 193 |
+
return x
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
class series_decomp(nn.Module):
|
| 197 |
+
"""
|
| 198 |
+
Series decomposition block
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
def __init__(self, kernel_size):
|
| 202 |
+
super(series_decomp, self).__init__()
|
| 203 |
+
self.moving_avg = moving_avg(kernel_size, stride=1)
|
| 204 |
+
|
| 205 |
+
def forward(self, x):
|
| 206 |
+
moving_mean = self.moving_avg(x)
|
| 207 |
+
res = x - moving_mean
|
| 208 |
+
return res, moving_mean
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
class series_decomp_multi(nn.Module):
|
| 212 |
+
"""
|
| 213 |
+
Multiple Series decomposition block from FEDformer
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
def __init__(self, kernel_size):
|
| 217 |
+
super(series_decomp_multi, self).__init__()
|
| 218 |
+
self.kernel_size = kernel_size
|
| 219 |
+
self.series_decomp = [series_decomp(kernel) for kernel in kernel_size]
|
| 220 |
+
|
| 221 |
+
def forward(self, x):
|
| 222 |
+
moving_mean = []
|
| 223 |
+
res = []
|
| 224 |
+
for func in self.series_decomp:
|
| 225 |
+
sea, moving_avg = func(x)
|
| 226 |
+
moving_mean.append(moving_avg)
|
| 227 |
+
res.append(sea)
|
| 228 |
+
|
| 229 |
+
sea = sum(res) / len(res)
|
| 230 |
+
moving_mean = sum(moving_mean) / len(moving_mean)
|
| 231 |
+
return sea, moving_mean
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class EncoderLayer(nn.Module):
|
| 235 |
+
"""
|
| 236 |
+
Autoformer encoder layer with the progressive decomposition architecture
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
def __init__(self, attention, d_model, d_ff=None, moving_avg=25, dropout=0.1, activation="relu"):
|
| 240 |
+
super(EncoderLayer, self).__init__()
|
| 241 |
+
d_ff = d_ff or 4 * d_model
|
| 242 |
+
self.attention = attention
|
| 243 |
+
self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1, bias=False)
|
| 244 |
+
self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1, bias=False)
|
| 245 |
+
self.decomp1 = series_decomp(moving_avg)
|
| 246 |
+
self.decomp2 = series_decomp(moving_avg)
|
| 247 |
+
self.dropout = nn.Dropout(dropout)
|
| 248 |
+
self.activation = F.relu if activation == "relu" else F.gelu
|
| 249 |
+
|
| 250 |
+
def forward(self, x, attn_mask=None):
|
| 251 |
+
new_x, attn = self.attention(
|
| 252 |
+
x, x, x,
|
| 253 |
+
attn_mask=attn_mask
|
| 254 |
+
)
|
| 255 |
+
x = x + self.dropout(new_x)
|
| 256 |
+
x, _ = self.decomp1(x)
|
| 257 |
+
y = x
|
| 258 |
+
y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
|
| 259 |
+
y = self.dropout(self.conv2(y).transpose(-1, 1))
|
| 260 |
+
res, _ = self.decomp2(x + y)
|
| 261 |
+
return res, attn
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
class Encoder(nn.Module):
|
| 265 |
+
"""
|
| 266 |
+
Autoformer encoder
|
| 267 |
+
"""
|
| 268 |
+
|
| 269 |
+
def __init__(self, attn_layers, conv_layers=None, norm_layer=None):
|
| 270 |
+
super(Encoder, self).__init__()
|
| 271 |
+
self.attn_layers = nn.ModuleList(attn_layers)
|
| 272 |
+
self.conv_layers = nn.ModuleList(conv_layers) if conv_layers is not None else None
|
| 273 |
+
self.norm = norm_layer
|
| 274 |
+
|
| 275 |
+
def forward(self, x, attn_mask=None):
|
| 276 |
+
attns = []
|
| 277 |
+
if self.conv_layers is not None:
|
| 278 |
+
for attn_layer, conv_layer in zip(self.attn_layers, self.conv_layers):
|
| 279 |
+
x, attn = attn_layer(x, attn_mask=attn_mask)
|
| 280 |
+
x = conv_layer(x)
|
| 281 |
+
attns.append(attn)
|
| 282 |
+
x, attn = self.attn_layers[-1](x)
|
| 283 |
+
attns.append(attn)
|
| 284 |
+
else:
|
| 285 |
+
for attn_layer in self.attn_layers:
|
| 286 |
+
x, attn = attn_layer(x, attn_mask=attn_mask)
|
| 287 |
+
attns.append(attn)
|
| 288 |
+
|
| 289 |
+
if self.norm is not None:
|
| 290 |
+
x = self.norm(x)
|
| 291 |
+
|
| 292 |
+
return x, attns
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
class DecoderLayer(nn.Module):
|
| 296 |
+
"""
|
| 297 |
+
Autoformer decoder layer with the progressive decomposition architecture
|
| 298 |
+
"""
|
| 299 |
+
|
| 300 |
+
def __init__(self, self_attention, cross_attention, d_model, c_out, d_ff=None,
|
| 301 |
+
moving_avg=25, dropout=0.1, activation="relu"):
|
| 302 |
+
super(DecoderLayer, self).__init__()
|
| 303 |
+
d_ff = d_ff or 4 * d_model
|
| 304 |
+
self.self_attention = self_attention
|
| 305 |
+
self.cross_attention = cross_attention
|
| 306 |
+
self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1, bias=False)
|
| 307 |
+
self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1, bias=False)
|
| 308 |
+
self.decomp1 = series_decomp(moving_avg)
|
| 309 |
+
self.decomp2 = series_decomp(moving_avg)
|
| 310 |
+
self.decomp3 = series_decomp(moving_avg)
|
| 311 |
+
self.dropout = nn.Dropout(dropout)
|
| 312 |
+
self.projection = nn.Conv1d(in_channels=d_model, out_channels=c_out, kernel_size=3, stride=1, padding=1,
|
| 313 |
+
padding_mode='circular', bias=False)
|
| 314 |
+
self.activation = F.relu if activation == "relu" else F.gelu
|
| 315 |
+
|
| 316 |
+
def forward(self, x, cross, x_mask=None, cross_mask=None):
|
| 317 |
+
x = x + self.dropout(self.self_attention(
|
| 318 |
+
x, x, x,
|
| 319 |
+
attn_mask=x_mask
|
| 320 |
+
)[0])
|
| 321 |
+
x, trend1 = self.decomp1(x)
|
| 322 |
+
x = x + self.dropout(self.cross_attention(
|
| 323 |
+
x, cross, cross,
|
| 324 |
+
attn_mask=cross_mask
|
| 325 |
+
)[0])
|
| 326 |
+
x, trend2 = self.decomp2(x)
|
| 327 |
+
y = x
|
| 328 |
+
y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
|
| 329 |
+
y = self.dropout(self.conv2(y).transpose(-1, 1))
|
| 330 |
+
x, trend3 = self.decomp3(x + y)
|
| 331 |
+
|
| 332 |
+
residual_trend = trend1 + trend2 + trend3
|
| 333 |
+
residual_trend = self.projection(residual_trend.permute(0, 2, 1)).transpose(1, 2)
|
| 334 |
+
return x, residual_trend
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
class Decoder(nn.Module):
|
| 338 |
+
"""
|
| 339 |
+
Autoformer encoder
|
| 340 |
+
"""
|
| 341 |
+
|
| 342 |
+
def __init__(self, layers, norm_layer=None, projection=None):
|
| 343 |
+
super(Decoder, self).__init__()
|
| 344 |
+
self.layers = nn.ModuleList(layers)
|
| 345 |
+
self.norm = norm_layer
|
| 346 |
+
self.projection = projection
|
| 347 |
+
|
| 348 |
+
def forward(self, x, cross, x_mask=None, cross_mask=None, trend=None):
|
| 349 |
+
for layer in self.layers:
|
| 350 |
+
x, residual_trend = layer(x, cross, x_mask=x_mask, cross_mask=cross_mask)
|
| 351 |
+
trend = trend + residual_trend
|
| 352 |
+
|
| 353 |
+
if self.norm is not None:
|
| 354 |
+
x = self.norm(x)
|
| 355 |
+
|
| 356 |
+
if self.projection is not None:
|
| 357 |
+
x = self.projection(x)
|
| 358 |
+
return x, trend
|
| 359 |
+
|
| 360 |
+
class FixedEmbedding(nn.Module):
|
| 361 |
+
def __init__(self, c_in, d_model):
|
| 362 |
+
super(FixedEmbedding, self).__init__()
|
| 363 |
+
|
| 364 |
+
w = torch.zeros(c_in, d_model).float()
|
| 365 |
+
w.require_grad = False
|
| 366 |
+
|
| 367 |
+
position = torch.arange(0, c_in).float().unsqueeze(1)
|
| 368 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 369 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 370 |
+
|
| 371 |
+
w[:, 0::2] = torch.sin(position * div_term)
|
| 372 |
+
w[:, 1::2] = torch.cos(position * div_term)
|
| 373 |
+
|
| 374 |
+
self.emb = nn.Embedding(c_in, d_model)
|
| 375 |
+
self.emb.weight = nn.Parameter(w, requires_grad=False)
|
| 376 |
+
|
| 377 |
+
def forward(self, x):
|
| 378 |
+
return self.emb(x).detach()
|
| 379 |
+
|
| 380 |
+
class TemporalEmbedding(nn.Module):
|
| 381 |
+
def __init__(self, d_model, embed_type='fixed', freq='h'):
|
| 382 |
+
super(TemporalEmbedding, self).__init__()
|
| 383 |
+
|
| 384 |
+
hour_size = 96
|
| 385 |
+
weekday_size = 7
|
| 386 |
+
|
| 387 |
+
Embed = FixedEmbedding if embed_type == 'fixed' else nn.Embedding
|
| 388 |
+
self.hour_embed = Embed(hour_size, d_model)
|
| 389 |
+
self.weekday_embed = Embed(weekday_size, d_model)
|
| 390 |
+
|
| 391 |
+
def forward(self, x):
|
| 392 |
+
x = x.long()
|
| 393 |
+
hour_x = self.hour_embed(x[:, :, 0])
|
| 394 |
+
weekday_x = self.weekday_embed(x[:, :, 1])
|
| 395 |
+
|
| 396 |
+
return hour_x + weekday_x
|
| 397 |
+
|
| 398 |
+
class PositionalEmbedding(nn.Module):
|
| 399 |
+
def __init__(self, d_model, max_len=5000):
|
| 400 |
+
super(PositionalEmbedding, self).__init__()
|
| 401 |
+
# Compute the positional encodings once in log space.
|
| 402 |
+
pe = torch.zeros(max_len, d_model).float()
|
| 403 |
+
pe.require_grad = False
|
| 404 |
+
|
| 405 |
+
position = torch.arange(0, max_len).float().unsqueeze(1)
|
| 406 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 407 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 408 |
+
|
| 409 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 410 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 411 |
+
|
| 412 |
+
pe = pe.unsqueeze(0)
|
| 413 |
+
self.register_buffer('pe', pe)
|
| 414 |
+
|
| 415 |
+
def forward(self, x):
|
| 416 |
+
return self.pe[:, :x.size(1)]
|
| 417 |
+
|
| 418 |
+
class TokenEmbedding(nn.Module):
|
| 419 |
+
def __init__(self, c_in, d_model):
|
| 420 |
+
super(TokenEmbedding, self).__init__()
|
| 421 |
+
padding = 1 if torch.__version__ >= '1.5.0' else 2
|
| 422 |
+
self.tokenConv = nn.Conv1d(in_channels=c_in, out_channels=d_model,
|
| 423 |
+
kernel_size=3, padding=padding, padding_mode='circular', bias=False)
|
| 424 |
+
for m in self.modules():
|
| 425 |
+
if isinstance(m, nn.Conv1d):
|
| 426 |
+
nn.init.kaiming_normal_(
|
| 427 |
+
m.weight, mode='fan_in', nonlinearity='leaky_relu')
|
| 428 |
+
|
| 429 |
+
def forward(self, x):
|
| 430 |
+
x = self.tokenConv(x.permute(0, 2, 1)).transpose(1, 2)
|
| 431 |
+
return x
|
| 432 |
+
|
| 433 |
+
class DataEmbedding_wo_pos(nn.Module):
|
| 434 |
+
def __init__(self, c_in, d_model, embed_type='fixed', freq='h', dropout=0.1):
|
| 435 |
+
super(DataEmbedding_wo_pos, self).__init__()
|
| 436 |
+
|
| 437 |
+
self.value_embedding = TokenEmbedding(c_in=c_in, d_model=d_model)
|
| 438 |
+
self.temporal_embedding = TemporalEmbedding(d_model=d_model, embed_type=embed_type,
|
| 439 |
+
freq=freq)
|
| 440 |
+
self.dropout = nn.Dropout(p=dropout)
|
| 441 |
+
|
| 442 |
+
def forward(self, x, x_mark):
|
| 443 |
+
if x_mark is None:
|
| 444 |
+
x = self.value_embedding(x)
|
| 445 |
+
else:
|
| 446 |
+
x = self.value_embedding(x) + self.temporal_embedding(x_mark)
|
| 447 |
+
return self.dropout(x)
|
| 448 |
+
|
| 449 |
+
class Autoformer(nn.Module):
|
| 450 |
+
"""
|
| 451 |
+
Autoformer is the first method to achieve the series-wise connection,
|
| 452 |
+
with inherent O(LlogL) complexity
|
| 453 |
+
Paper link: https://openreview.net/pdf?id=I55UqU-M11y
|
| 454 |
+
"""
|
| 455 |
+
|
| 456 |
+
def __init__(
|
| 457 |
+
self,
|
| 458 |
+
enc_in,
|
| 459 |
+
dec_in,
|
| 460 |
+
c_out,
|
| 461 |
+
pred_len,
|
| 462 |
+
seq_len,
|
| 463 |
+
d_model = 64,
|
| 464 |
+
data_idx = [0,3,4,5,6,7],
|
| 465 |
+
time_idx = [1,2],
|
| 466 |
+
output_attention = False,
|
| 467 |
+
moving_avg_val = 25,
|
| 468 |
+
factor = 3,
|
| 469 |
+
n_heads = 4,
|
| 470 |
+
d_ff = 512,
|
| 471 |
+
d_layers = 3,
|
| 472 |
+
e_layers = 3,
|
| 473 |
+
activation = 'gelu',
|
| 474 |
+
dropout = 0.1
|
| 475 |
+
):
|
| 476 |
+
super(Autoformer, self).__init__()
|
| 477 |
+
self.seq_len = seq_len
|
| 478 |
+
self.pred_len = pred_len
|
| 479 |
+
self.output_attention = output_attention
|
| 480 |
+
self.data_idx = data_idx
|
| 481 |
+
self.time_idx = time_idx
|
| 482 |
+
dec_in = enc_in # encoder and decoder shapes should be the same
|
| 483 |
+
self.dec_in = dec_in
|
| 484 |
+
self.label_len = self.seq_len//2
|
| 485 |
+
|
| 486 |
+
# Decomp
|
| 487 |
+
kernel_size = moving_avg_val
|
| 488 |
+
self.decomp = series_decomp(kernel_size)
|
| 489 |
+
|
| 490 |
+
# Embedding
|
| 491 |
+
self.enc_embedding = DataEmbedding_wo_pos(enc_in, d_model, 'fixed','h',
|
| 492 |
+
dropout)
|
| 493 |
+
# Encoder
|
| 494 |
+
self.encoder = Encoder(
|
| 495 |
+
[
|
| 496 |
+
EncoderLayer(
|
| 497 |
+
AutoCorrelationLayer(
|
| 498 |
+
AutoCorrelation(False, factor, attention_dropout=dropout,
|
| 499 |
+
output_attention=output_attention),
|
| 500 |
+
d_model, n_heads),
|
| 501 |
+
d_model,
|
| 502 |
+
d_ff,
|
| 503 |
+
moving_avg=moving_avg_val,
|
| 504 |
+
dropout=dropout,
|
| 505 |
+
activation=activation
|
| 506 |
+
) for l in range(e_layers)
|
| 507 |
+
],
|
| 508 |
+
norm_layer=my_Layernorm(d_model)
|
| 509 |
+
)
|
| 510 |
+
# Decoder
|
| 511 |
+
self.dec_embedding = DataEmbedding_wo_pos(dec_in, d_model, 'fixed','h',
|
| 512 |
+
dropout)
|
| 513 |
+
self.decoder = Decoder(
|
| 514 |
+
[
|
| 515 |
+
DecoderLayer(
|
| 516 |
+
AutoCorrelationLayer(
|
| 517 |
+
AutoCorrelation(True, factor, attention_dropout=dropout,
|
| 518 |
+
output_attention=False),
|
| 519 |
+
d_model, n_heads),
|
| 520 |
+
AutoCorrelationLayer(
|
| 521 |
+
AutoCorrelation(False, factor, attention_dropout=dropout,
|
| 522 |
+
output_attention=False),
|
| 523 |
+
d_model, n_heads),
|
| 524 |
+
d_model,
|
| 525 |
+
c_out,
|
| 526 |
+
d_ff,
|
| 527 |
+
moving_avg=moving_avg_val,
|
| 528 |
+
dropout=dropout,
|
| 529 |
+
activation=activation,
|
| 530 |
+
)
|
| 531 |
+
for l in range(d_layers)
|
| 532 |
+
],
|
| 533 |
+
norm_layer=my_Layernorm(d_model),
|
| 534 |
+
projection=nn.Linear(d_model, c_out, bias=True)
|
| 535 |
+
)
|
| 536 |
+
|
| 537 |
+
def forecast(self, x_enc, x_mark_enc, x_dec, x_mark_dec):
|
| 538 |
+
# decomp init
|
| 539 |
+
mean = torch.mean(x_enc, dim=1).unsqueeze(1).repeat(1, self.pred_len, 1)
|
| 540 |
+
zeros = torch.zeros([x_mark_dec.shape[0], self.pred_len,self.dec_in], device=x_enc.device)
|
| 541 |
+
seasonal_init, trend_init = self.decomp(x_enc)
|
| 542 |
+
# decoder input
|
| 543 |
+
trend_init = torch.cat([trend_init[:, -self.label_len:, :], mean], dim=1)
|
| 544 |
+
seasonal_init = torch.cat([seasonal_init[:, -self.label_len:, :], zeros], dim=1)
|
| 545 |
+
# enc
|
| 546 |
+
enc_out = self.enc_embedding(x_enc, x_mark_enc)
|
| 547 |
+
enc_out, attns = self.encoder(enc_out, attn_mask=None)
|
| 548 |
+
# dec
|
| 549 |
+
x_mark_dec = torch.cat([x_mark_enc,x_mark_dec],dim=1)[:,-(self.label_len+self.pred_len):,:]
|
| 550 |
+
dec_out = self.dec_embedding(seasonal_init, x_mark_dec)
|
| 551 |
+
seasonal_part, trend_part = self.decoder(dec_out, enc_out, x_mask=None, cross_mask=None,
|
| 552 |
+
trend=trend_init)
|
| 553 |
+
dec_out = trend_part + seasonal_part
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
return dec_out[:, -self.pred_len:, :]
|
| 557 |
+
|
| 558 |
+
def forward(self, x, fut_time):
|
| 559 |
+
|
| 560 |
+
x_enc = x[:,:,self.data_idx]
|
| 561 |
+
x_mark_enc = x[:,:,self.time_idx]
|
| 562 |
+
# x_dec = torch.zeros((fut_time.shape[0],fut_time.shape[1],self.dec_in),dtype=fut_time.dtype,device=fut_time.device)
|
| 563 |
+
x_mark_dec = fut_time
|
| 564 |
+
|
| 565 |
+
# not necessary to generate decoder input
|
| 566 |
+
# return self.forecast(x_enc, x_mark_enc, x_dec, x_mark_dec)[:,-1,[0]] # [B, 1]
|
| 567 |
+
return self.forecast(x_enc, x_mark_enc, None, x_mark_dec)[:,-1,[0]] # [B, 1]
|
models/Informer.py
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import math
|
| 4 |
+
from math import sqrt
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
|
| 8 |
+
class ConvLayer(nn.Module):
|
| 9 |
+
def __init__(self, c_in):
|
| 10 |
+
super(ConvLayer, self).__init__()
|
| 11 |
+
self.downConv = nn.Conv1d(in_channels=c_in,
|
| 12 |
+
out_channels=c_in,
|
| 13 |
+
kernel_size=3,
|
| 14 |
+
padding=2,
|
| 15 |
+
padding_mode='circular')
|
| 16 |
+
self.norm = nn.BatchNorm1d(c_in)
|
| 17 |
+
self.activation = nn.ELU()
|
| 18 |
+
self.maxPool = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)
|
| 19 |
+
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
x = self.downConv(x.permute(0, 2, 1))
|
| 22 |
+
x = self.norm(x)
|
| 23 |
+
x = self.activation(x)
|
| 24 |
+
x = self.maxPool(x)
|
| 25 |
+
x = x.transpose(1, 2)
|
| 26 |
+
return x
|
| 27 |
+
|
| 28 |
+
class ProbMask():
|
| 29 |
+
def __init__(self, B, H, L, index, scores, device="cpu"):
|
| 30 |
+
_mask = torch.ones(L, scores.shape[-1], dtype=torch.bool).to(device).triu(1)
|
| 31 |
+
_mask_ex = _mask[None, None, :].expand(B, H, L, scores.shape[-1])
|
| 32 |
+
indicator = _mask_ex[torch.arange(B)[:, None, None],
|
| 33 |
+
torch.arange(H)[None, :, None],
|
| 34 |
+
index, :].to(device)
|
| 35 |
+
self._mask = indicator.view(scores.shape).to(device)
|
| 36 |
+
|
| 37 |
+
@property
|
| 38 |
+
def mask(self):
|
| 39 |
+
return self._mask
|
| 40 |
+
|
| 41 |
+
class ProbAttention(nn.Module):
|
| 42 |
+
def __init__(self, mask_flag=True, factor=5, scale=None, attention_dropout=0.1, output_attention=False):
|
| 43 |
+
super(ProbAttention, self).__init__()
|
| 44 |
+
self.factor = factor
|
| 45 |
+
self.scale = scale
|
| 46 |
+
self.mask_flag = mask_flag
|
| 47 |
+
self.output_attention = output_attention
|
| 48 |
+
self.dropout = nn.Dropout(attention_dropout)
|
| 49 |
+
|
| 50 |
+
def _prob_QK(self, Q, K, sample_k, n_top): # n_top: c*ln(L_q)
|
| 51 |
+
# Q [B, H, L, D]
|
| 52 |
+
B, H, L_K, E = K.shape
|
| 53 |
+
_, _, L_Q, _ = Q.shape
|
| 54 |
+
|
| 55 |
+
# calculate the sampled Q_K
|
| 56 |
+
K_expand = K.unsqueeze(-3).expand(B, H, L_Q, L_K, E)
|
| 57 |
+
index_sample = torch.randint(L_K, (L_Q, sample_k)) # real U = U_part(factor*ln(L_k))*L_q
|
| 58 |
+
K_sample = K_expand[:, :, torch.arange(L_Q).unsqueeze(1), index_sample, :]
|
| 59 |
+
Q_K_sample = torch.matmul(Q.unsqueeze(-2), K_sample.transpose(-2, -1)).squeeze()
|
| 60 |
+
|
| 61 |
+
# find the Top_k query with sparisty measurement
|
| 62 |
+
M = Q_K_sample.max(-1)[0] - torch.div(Q_K_sample.sum(-1), L_K)
|
| 63 |
+
M_top = M.topk(n_top, sorted=False)[1]
|
| 64 |
+
|
| 65 |
+
# use the reduced Q to calculate Q_K
|
| 66 |
+
Q_reduce = Q[torch.arange(B)[:, None, None],
|
| 67 |
+
torch.arange(H)[None, :, None],
|
| 68 |
+
M_top, :] # factor*ln(L_q)
|
| 69 |
+
Q_K = torch.matmul(Q_reduce, K.transpose(-2, -1)) # factor*ln(L_q)*L_k
|
| 70 |
+
|
| 71 |
+
return Q_K, M_top
|
| 72 |
+
|
| 73 |
+
def _get_initial_context(self, V, L_Q):
|
| 74 |
+
B, H, L_V, D = V.shape
|
| 75 |
+
if not self.mask_flag:
|
| 76 |
+
# V_sum = V.sum(dim=-2)
|
| 77 |
+
V_sum = V.mean(dim=-2)
|
| 78 |
+
contex = V_sum.unsqueeze(-2).expand(B, H, L_Q, V_sum.shape[-1]).clone()
|
| 79 |
+
else: # use mask
|
| 80 |
+
assert (L_Q == L_V) # requires that L_Q == L_V, i.e. for self-attention only
|
| 81 |
+
contex = V.cumsum(dim=-2)
|
| 82 |
+
return contex
|
| 83 |
+
|
| 84 |
+
def _update_context(self, context_in, V, scores, index, L_Q, attn_mask):
|
| 85 |
+
B, H, L_V, D = V.shape
|
| 86 |
+
|
| 87 |
+
if self.mask_flag:
|
| 88 |
+
attn_mask = ProbMask(B, H, L_Q, index, scores, device=V.device)
|
| 89 |
+
scores.masked_fill_(attn_mask.mask, -np.inf)
|
| 90 |
+
|
| 91 |
+
attn = torch.softmax(scores, dim=-1) # nn.Softmax(dim=-1)(scores)
|
| 92 |
+
|
| 93 |
+
context_in[torch.arange(B)[:, None, None],
|
| 94 |
+
torch.arange(H)[None, :, None],
|
| 95 |
+
index, :] = torch.matmul(attn, V).type_as(context_in)
|
| 96 |
+
if self.output_attention:
|
| 97 |
+
attns = (torch.ones([B, H, L_V, L_V]) / L_V).type_as(attn).to(attn.device)
|
| 98 |
+
attns[torch.arange(B)[:, None, None], torch.arange(H)[None, :, None], index, :] = attn
|
| 99 |
+
return (context_in, attns)
|
| 100 |
+
else:
|
| 101 |
+
return (context_in, None)
|
| 102 |
+
|
| 103 |
+
def forward(self, queries, keys, values, attn_mask):
|
| 104 |
+
B, L_Q, H, D = queries.shape
|
| 105 |
+
_, L_K, _, _ = keys.shape
|
| 106 |
+
|
| 107 |
+
queries = queries.transpose(2, 1)
|
| 108 |
+
keys = keys.transpose(2, 1)
|
| 109 |
+
values = values.transpose(2, 1)
|
| 110 |
+
|
| 111 |
+
U_part = self.factor * np.ceil(np.log(L_K)).astype('int').item() # c*ln(L_k)
|
| 112 |
+
u = self.factor * np.ceil(np.log(L_Q)).astype('int').item() # c*ln(L_q)
|
| 113 |
+
|
| 114 |
+
U_part = U_part if U_part < L_K else L_K
|
| 115 |
+
u = u if u < L_Q else L_Q
|
| 116 |
+
|
| 117 |
+
scores_top, index = self._prob_QK(queries, keys, sample_k=U_part, n_top=u)
|
| 118 |
+
|
| 119 |
+
# add scale factor
|
| 120 |
+
scale = self.scale or 1. / sqrt(D)
|
| 121 |
+
if scale is not None:
|
| 122 |
+
scores_top = scores_top * scale
|
| 123 |
+
# get the context
|
| 124 |
+
context = self._get_initial_context(values, L_Q)
|
| 125 |
+
# update the context with selected top_k queries
|
| 126 |
+
context, attn = self._update_context(context, values, scores_top, index, L_Q, attn_mask)
|
| 127 |
+
|
| 128 |
+
return context.contiguous(), attn
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class AttentionLayer(nn.Module):
|
| 132 |
+
def __init__(self, attention, d_model, n_heads, d_keys=None,
|
| 133 |
+
d_values=None):
|
| 134 |
+
super(AttentionLayer, self).__init__()
|
| 135 |
+
|
| 136 |
+
d_keys = d_keys or (d_model // n_heads)
|
| 137 |
+
d_values = d_values or (d_model // n_heads)
|
| 138 |
+
|
| 139 |
+
self.inner_attention = attention
|
| 140 |
+
self.query_projection = nn.Linear(d_model, d_keys * n_heads)
|
| 141 |
+
self.key_projection = nn.Linear(d_model, d_keys * n_heads)
|
| 142 |
+
self.value_projection = nn.Linear(d_model, d_values * n_heads)
|
| 143 |
+
self.out_projection = nn.Linear(d_values * n_heads, d_model)
|
| 144 |
+
self.n_heads = n_heads
|
| 145 |
+
|
| 146 |
+
def forward(self, queries, keys, values, attn_mask):
|
| 147 |
+
B, L, _ = queries.shape
|
| 148 |
+
_, S, _ = keys.shape
|
| 149 |
+
H = self.n_heads
|
| 150 |
+
|
| 151 |
+
queries = self.query_projection(queries).view(B, L, H, -1)
|
| 152 |
+
keys = self.key_projection(keys).view(B, S, H, -1)
|
| 153 |
+
values = self.value_projection(values).view(B, S, H, -1)
|
| 154 |
+
|
| 155 |
+
out, attn = self.inner_attention(
|
| 156 |
+
queries,
|
| 157 |
+
keys,
|
| 158 |
+
values,
|
| 159 |
+
attn_mask
|
| 160 |
+
)
|
| 161 |
+
out = out.view(B, L, -1)
|
| 162 |
+
|
| 163 |
+
return self.out_projection(out), attn
|
| 164 |
+
|
| 165 |
+
class EncoderLayer(nn.Module):
|
| 166 |
+
def __init__(self, attention, d_model, d_ff=None, dropout=0.1, activation="relu"):
|
| 167 |
+
super(EncoderLayer, self).__init__()
|
| 168 |
+
d_ff = d_ff or 4 * d_model
|
| 169 |
+
self.attention = attention
|
| 170 |
+
self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
|
| 171 |
+
self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
|
| 172 |
+
self.norm1 = nn.LayerNorm(d_model)
|
| 173 |
+
self.norm2 = nn.LayerNorm(d_model)
|
| 174 |
+
self.dropout = nn.Dropout(dropout)
|
| 175 |
+
self.activation = F.relu if activation == "relu" else F.gelu
|
| 176 |
+
|
| 177 |
+
def forward(self, x, attn_mask=None):
|
| 178 |
+
new_x, attn = self.attention(
|
| 179 |
+
x, x, x,
|
| 180 |
+
attn_mask=attn_mask
|
| 181 |
+
)
|
| 182 |
+
x = x + self.dropout(new_x)
|
| 183 |
+
|
| 184 |
+
y = x = self.norm1(x)
|
| 185 |
+
y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
|
| 186 |
+
y = self.dropout(self.conv2(y).transpose(-1, 1))
|
| 187 |
+
|
| 188 |
+
return self.norm2(x + y), attn
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class Encoder(nn.Module):
|
| 192 |
+
def __init__(self, attn_layers, conv_layers=None, norm_layer=None):
|
| 193 |
+
super(Encoder, self).__init__()
|
| 194 |
+
self.attn_layers = nn.ModuleList(attn_layers)
|
| 195 |
+
self.conv_layers = nn.ModuleList(conv_layers) if conv_layers is not None else None
|
| 196 |
+
self.norm = norm_layer
|
| 197 |
+
|
| 198 |
+
def forward(self, x, attn_mask=None):
|
| 199 |
+
# x [B, L, D]
|
| 200 |
+
attns = []
|
| 201 |
+
if self.conv_layers is not None:
|
| 202 |
+
for attn_layer, conv_layer in zip(self.attn_layers, self.conv_layers):
|
| 203 |
+
x, attn = attn_layer(x, attn_mask=attn_mask)
|
| 204 |
+
x = conv_layer(x)
|
| 205 |
+
attns.append(attn)
|
| 206 |
+
x, attn = self.attn_layers[-1](x)
|
| 207 |
+
attns.append(attn)
|
| 208 |
+
else:
|
| 209 |
+
for attn_layer in self.attn_layers:
|
| 210 |
+
x, attn = attn_layer(x, attn_mask=attn_mask)
|
| 211 |
+
attns.append(attn)
|
| 212 |
+
|
| 213 |
+
if self.norm is not None:
|
| 214 |
+
x = self.norm(x)
|
| 215 |
+
|
| 216 |
+
return x, attns
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
class DecoderLayer(nn.Module):
|
| 220 |
+
def __init__(self, self_attention, cross_attention, d_model, d_ff=None,
|
| 221 |
+
dropout=0.1, activation="relu"):
|
| 222 |
+
super(DecoderLayer, self).__init__()
|
| 223 |
+
d_ff = d_ff or 4 * d_model
|
| 224 |
+
self.self_attention = self_attention
|
| 225 |
+
self.cross_attention = cross_attention
|
| 226 |
+
self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
|
| 227 |
+
self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
|
| 228 |
+
self.norm1 = nn.LayerNorm(d_model)
|
| 229 |
+
self.norm2 = nn.LayerNorm(d_model)
|
| 230 |
+
self.norm3 = nn.LayerNorm(d_model)
|
| 231 |
+
self.dropout = nn.Dropout(dropout)
|
| 232 |
+
self.activation = F.relu if activation == "relu" else F.gelu
|
| 233 |
+
|
| 234 |
+
def forward(self, x, cross, x_mask=None, cross_mask=None):
|
| 235 |
+
x = x + self.dropout(self.self_attention(
|
| 236 |
+
x, x, x,
|
| 237 |
+
attn_mask=x_mask
|
| 238 |
+
)[0])
|
| 239 |
+
x = self.norm1(x)
|
| 240 |
+
|
| 241 |
+
x = x + self.dropout(self.cross_attention(
|
| 242 |
+
x, cross, cross,
|
| 243 |
+
attn_mask=cross_mask
|
| 244 |
+
)[0])
|
| 245 |
+
|
| 246 |
+
y = x = self.norm2(x)
|
| 247 |
+
y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
|
| 248 |
+
y = self.dropout(self.conv2(y).transpose(-1, 1))
|
| 249 |
+
|
| 250 |
+
return self.norm3(x + y)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
class Decoder(nn.Module):
|
| 254 |
+
def __init__(self, layers, norm_layer=None, projection=None):
|
| 255 |
+
super(Decoder, self).__init__()
|
| 256 |
+
self.layers = nn.ModuleList(layers)
|
| 257 |
+
self.norm = norm_layer
|
| 258 |
+
self.projection = projection
|
| 259 |
+
|
| 260 |
+
def forward(self, x, cross, x_mask=None, cross_mask=None):
|
| 261 |
+
for layer in self.layers:
|
| 262 |
+
x = layer(x, cross, x_mask=x_mask, cross_mask=cross_mask)
|
| 263 |
+
|
| 264 |
+
if self.norm is not None:
|
| 265 |
+
x = self.norm(x)
|
| 266 |
+
|
| 267 |
+
if self.projection is not None:
|
| 268 |
+
x = self.projection(x)
|
| 269 |
+
return x
|
| 270 |
+
|
| 271 |
+
class PositionalEmbedding(nn.Module):
|
| 272 |
+
def __init__(self, d_model, max_len=5000):
|
| 273 |
+
super(PositionalEmbedding, self).__init__()
|
| 274 |
+
# Compute the positional encodings once in log space.
|
| 275 |
+
pe = torch.zeros(max_len, d_model).float()
|
| 276 |
+
pe.require_grad = False
|
| 277 |
+
|
| 278 |
+
position = torch.arange(0, max_len).float().unsqueeze(1)
|
| 279 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 280 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 281 |
+
|
| 282 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 283 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 284 |
+
|
| 285 |
+
pe = pe.unsqueeze(0)
|
| 286 |
+
self.register_buffer('pe', pe)
|
| 287 |
+
|
| 288 |
+
def forward(self, x):
|
| 289 |
+
return self.pe[:, :x.size(1)]
|
| 290 |
+
|
| 291 |
+
class FixedEmbedding(nn.Module):
|
| 292 |
+
def __init__(self, c_in, d_model):
|
| 293 |
+
super(FixedEmbedding, self).__init__()
|
| 294 |
+
|
| 295 |
+
w = torch.zeros(c_in, d_model).float()
|
| 296 |
+
w.require_grad = False
|
| 297 |
+
|
| 298 |
+
position = torch.arange(0, c_in).float().unsqueeze(1)
|
| 299 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 300 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 301 |
+
|
| 302 |
+
w[:, 0::2] = torch.sin(position * div_term)
|
| 303 |
+
w[:, 1::2] = torch.cos(position * div_term)
|
| 304 |
+
|
| 305 |
+
self.emb = nn.Embedding(c_in, d_model)
|
| 306 |
+
self.emb.weight = nn.Parameter(w, requires_grad=False)
|
| 307 |
+
|
| 308 |
+
def forward(self, x):
|
| 309 |
+
return self.emb(x).detach()
|
| 310 |
+
|
| 311 |
+
class TemporalEmbedding(nn.Module):
|
| 312 |
+
def __init__(self, d_model, embed_type='fixed', freq='h'):
|
| 313 |
+
super(TemporalEmbedding, self).__init__()
|
| 314 |
+
|
| 315 |
+
hour_size = 96
|
| 316 |
+
weekday_size = 7
|
| 317 |
+
|
| 318 |
+
Embed = FixedEmbedding if embed_type == 'fixed' else nn.Embedding
|
| 319 |
+
self.hour_embed = Embed(hour_size, d_model)
|
| 320 |
+
self.weekday_embed = Embed(weekday_size, d_model)
|
| 321 |
+
|
| 322 |
+
def forward(self, x):
|
| 323 |
+
x = x.long()
|
| 324 |
+
hour_x = self.hour_embed(x[:, :, 0])
|
| 325 |
+
weekday_x = self.weekday_embed(x[:, :, 1])
|
| 326 |
+
|
| 327 |
+
return hour_x + weekday_x
|
| 328 |
+
|
| 329 |
+
class TokenEmbedding(nn.Module):
|
| 330 |
+
def __init__(self, c_in, d_model):
|
| 331 |
+
super(TokenEmbedding, self).__init__()
|
| 332 |
+
padding = 1 if torch.__version__ >= '1.5.0' else 2
|
| 333 |
+
self.tokenConv = nn.Conv1d(in_channels=c_in, out_channels=d_model,
|
| 334 |
+
kernel_size=3, padding=padding, padding_mode='circular', bias=False)
|
| 335 |
+
for m in self.modules():
|
| 336 |
+
if isinstance(m, nn.Conv1d):
|
| 337 |
+
nn.init.kaiming_normal_(
|
| 338 |
+
m.weight, mode='fan_in', nonlinearity='leaky_relu')
|
| 339 |
+
|
| 340 |
+
def forward(self, x):
|
| 341 |
+
x = self.tokenConv(x.permute(0, 2, 1)).transpose(1, 2)
|
| 342 |
+
return x
|
| 343 |
+
|
| 344 |
+
class DataEmbedding(nn.Module):
|
| 345 |
+
def __init__(self, c_in, d_model, embed_type='fixed', freq='h', dropout=0.1):
|
| 346 |
+
super(DataEmbedding, self).__init__()
|
| 347 |
+
|
| 348 |
+
self.value_embedding = TokenEmbedding(c_in=c_in, d_model=d_model)
|
| 349 |
+
self.position_embedding = PositionalEmbedding(d_model=d_model)
|
| 350 |
+
self.temporal_embedding = TemporalEmbedding(d_model=d_model, embed_type=embed_type,
|
| 351 |
+
freq=freq)
|
| 352 |
+
self.dropout = nn.Dropout(p=dropout)
|
| 353 |
+
|
| 354 |
+
def forward(self, x, x_mark):
|
| 355 |
+
if x_mark is None:
|
| 356 |
+
x = self.value_embedding(x) + self.position_embedding(x)
|
| 357 |
+
else:
|
| 358 |
+
x = self.value_embedding(x) + self.temporal_embedding(x_mark) + self.position_embedding(x)
|
| 359 |
+
return self.dropout(x)
|
| 360 |
+
|
| 361 |
+
class Informer(nn.Module):
|
| 362 |
+
"""
|
| 363 |
+
Informer with Propspare attention in O(LlogL) complexity
|
| 364 |
+
"""
|
| 365 |
+
def __init__(
|
| 366 |
+
self,
|
| 367 |
+
enc_in,
|
| 368 |
+
dec_in,
|
| 369 |
+
c_out,
|
| 370 |
+
pred_len,
|
| 371 |
+
output_attention = False,
|
| 372 |
+
data_idx = [0,3,4,5,6,7],
|
| 373 |
+
time_idx = [1,2],
|
| 374 |
+
d_model = 16,
|
| 375 |
+
factor = 3,
|
| 376 |
+
n_heads = 4,
|
| 377 |
+
d_ff = 512,
|
| 378 |
+
d_layers = 3,
|
| 379 |
+
e_layers = 3,
|
| 380 |
+
activation = 'gelu',
|
| 381 |
+
dropout = 0.1
|
| 382 |
+
):
|
| 383 |
+
super(Informer, self).__init__()
|
| 384 |
+
self.pred_len = pred_len
|
| 385 |
+
self.output_attention = output_attention
|
| 386 |
+
self.data_idx = data_idx
|
| 387 |
+
self.time_idx = time_idx
|
| 388 |
+
self.dec_in = dec_in
|
| 389 |
+
|
| 390 |
+
# Embedding
|
| 391 |
+
self.enc_embedding = DataEmbedding(enc_in, d_model, 'fixed', 'h',
|
| 392 |
+
dropout)
|
| 393 |
+
self.dec_embedding = DataEmbedding(dec_in, d_model,'fixed', 'h',
|
| 394 |
+
dropout)
|
| 395 |
+
|
| 396 |
+
# Encoder
|
| 397 |
+
self.encoder = Encoder(
|
| 398 |
+
[
|
| 399 |
+
EncoderLayer(
|
| 400 |
+
AttentionLayer(
|
| 401 |
+
ProbAttention(False, factor, attention_dropout=dropout,
|
| 402 |
+
output_attention=output_attention),
|
| 403 |
+
d_model, n_heads),
|
| 404 |
+
d_model,
|
| 405 |
+
d_ff,
|
| 406 |
+
dropout=dropout,
|
| 407 |
+
activation=activation
|
| 408 |
+
) for l in range(e_layers)
|
| 409 |
+
],
|
| 410 |
+
[
|
| 411 |
+
ConvLayer(
|
| 412 |
+
d_model
|
| 413 |
+
) for l in range(e_layers - 1)
|
| 414 |
+
],
|
| 415 |
+
norm_layer=torch.nn.LayerNorm(d_model)
|
| 416 |
+
)
|
| 417 |
+
# Decoder
|
| 418 |
+
self.decoder = Decoder(
|
| 419 |
+
[
|
| 420 |
+
DecoderLayer(
|
| 421 |
+
AttentionLayer(
|
| 422 |
+
ProbAttention(True, factor, attention_dropout=dropout, output_attention=False),
|
| 423 |
+
d_model, n_heads),
|
| 424 |
+
AttentionLayer(
|
| 425 |
+
ProbAttention(False, factor, attention_dropout=dropout, output_attention=False),
|
| 426 |
+
d_model, n_heads),
|
| 427 |
+
d_model,
|
| 428 |
+
d_ff,
|
| 429 |
+
dropout=dropout,
|
| 430 |
+
activation=activation,
|
| 431 |
+
)
|
| 432 |
+
for l in range(d_layers)
|
| 433 |
+
],
|
| 434 |
+
norm_layer=torch.nn.LayerNorm(d_model),
|
| 435 |
+
projection=nn.Linear(d_model, c_out, bias=True)
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
def forecast(self, x_enc, x_mark_enc, x_dec, x_mark_dec,
|
| 439 |
+
enc_self_mask=None, dec_self_mask=None, dec_enc_mask=None):
|
| 440 |
+
|
| 441 |
+
enc_out = self.enc_embedding(x_enc, x_mark_enc)
|
| 442 |
+
enc_out, attns = self.encoder(enc_out, attn_mask=enc_self_mask)
|
| 443 |
+
|
| 444 |
+
dec_out = self.dec_embedding(x_dec, x_mark_dec)
|
| 445 |
+
dec_out = self.decoder(dec_out, enc_out, x_mask=dec_self_mask, cross_mask=dec_enc_mask)
|
| 446 |
+
|
| 447 |
+
if self.output_attention:
|
| 448 |
+
return dec_out[:, -self.pred_len:, :], attns
|
| 449 |
+
else:
|
| 450 |
+
return dec_out[:, -self.pred_len:, :] # [B, L, D]
|
| 451 |
+
|
| 452 |
+
def forward(self, x, fut_time):
|
| 453 |
+
|
| 454 |
+
x_enc = x[:,:,self.data_idx]
|
| 455 |
+
x_mark_enc = x[:,:,self.time_idx]
|
| 456 |
+
x_dec = torch.zeros((fut_time.shape[0],fut_time.shape[1],self.dec_in),dtype=fut_time.dtype,device=fut_time.device)
|
| 457 |
+
x_mark_dec = fut_time
|
| 458 |
+
|
| 459 |
+
return self.forecast(x_enc,x_mark_enc,x_dec,x_mark_dec)[:,-1,[0]]
|
models/LSTM.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from typing import Union, List, Tuple
|
| 4 |
+
|
| 5 |
+
class LSTM(nn.Module):
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
input_size: int = 8,
|
| 10 |
+
hidden_size: int = 40,
|
| 11 |
+
num_layers: int = 2,
|
| 12 |
+
dropout: float = 0.1,
|
| 13 |
+
lookback: int = 8, # this will not be used, but keeping it here for consistency
|
| 14 |
+
):
|
| 15 |
+
|
| 16 |
+
super(LSTM,self).__init__()
|
| 17 |
+
|
| 18 |
+
# save values for use outside init
|
| 19 |
+
self.hidden_size, self.num_layers = hidden_size, num_layers
|
| 20 |
+
|
| 21 |
+
# lstm
|
| 22 |
+
self.lstm = nn.LSTM(
|
| 23 |
+
input_size = input_size,
|
| 24 |
+
hidden_size = hidden_size,
|
| 25 |
+
num_layers = num_layers,
|
| 26 |
+
bias = True,
|
| 27 |
+
batch_first = True,
|
| 28 |
+
dropout = dropout,
|
| 29 |
+
bidirectional = False,
|
| 30 |
+
proj_size = 0,
|
| 31 |
+
device = None
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# projector
|
| 35 |
+
self.proj = nn.Linear(in_features=hidden_size, out_features=1, bias=False)
|
| 36 |
+
|
| 37 |
+
# dropout
|
| 38 |
+
self.dropout = nn.Dropout(p=dropout)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def init_h_c_(self, B, device, dtype):
|
| 42 |
+
|
| 43 |
+
h = torch.zeros((self.num_layers,B,self.hidden_size),dtype=dtype,device=device)
|
| 44 |
+
c = torch.zeros((self.num_layers,B,self.hidden_size),dtype=dtype,device=device)
|
| 45 |
+
|
| 46 |
+
return h,c
|
| 47 |
+
|
| 48 |
+
def forward(self, x, fut_time):
|
| 49 |
+
|
| 50 |
+
B, dev, dt = x.shape[0], x.device, x.dtype
|
| 51 |
+
|
| 52 |
+
# generate states
|
| 53 |
+
h,c = self.init_h_c_(B, dev, dt)
|
| 54 |
+
|
| 55 |
+
# iterate
|
| 56 |
+
out,(_,_) = self.lstm(x,(h,c))
|
| 57 |
+
return self.proj(self.dropout(out[:,-1,:]))
|
models/LSTNet.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# https://github.com/gokulkarthik/LSTNet.pytorch/blob/master/LSTNet.py
|
| 7 |
+
|
| 8 |
+
class LSTNet(nn.Module):
|
| 9 |
+
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
num_features: int = 8,
|
| 13 |
+
conv1_out_channels: int = 32,
|
| 14 |
+
conv1_kernel_height: int = 7,
|
| 15 |
+
recc1_out_channels: int = 64,
|
| 16 |
+
skip_steps: list[int] = [4,24],
|
| 17 |
+
skip_reccs_out_channels: list[int] = [4,4],
|
| 18 |
+
output_out_features: int = 1,
|
| 19 |
+
ar_window_size: int = 7,
|
| 20 |
+
dropout: float = 0.1
|
| 21 |
+
):
|
| 22 |
+
super(LSTNet, self).__init__()
|
| 23 |
+
self.num_features = num_features
|
| 24 |
+
self.conv1_out_channels = conv1_out_channels
|
| 25 |
+
self.conv1_kernel_height = conv1_kernel_height
|
| 26 |
+
self.recc1_out_channels = recc1_out_channels
|
| 27 |
+
self.skip_steps = skip_steps
|
| 28 |
+
self.skip_reccs_out_channels = skip_reccs_out_channels
|
| 29 |
+
self.output_out_features = output_out_features
|
| 30 |
+
self.ar_window_size = ar_window_size
|
| 31 |
+
self.dropout = nn.Dropout(p = dropout)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
self.conv1 = nn.Conv2d(1, self.conv1_out_channels,
|
| 35 |
+
kernel_size=(self.conv1_kernel_height, self.num_features))
|
| 36 |
+
self.recc1 = nn.GRU(self.conv1_out_channels, self.recc1_out_channels, batch_first=True)
|
| 37 |
+
self.skip_reccs = nn.ModuleList()
|
| 38 |
+
for i in range(len(self.skip_steps)):
|
| 39 |
+
self.skip_reccs.append(nn.GRU(self.conv1_out_channels, self.skip_reccs_out_channels[i], batch_first=True))
|
| 40 |
+
self.output_in_features = self.recc1_out_channels + np.dot(self.skip_steps, self.skip_reccs_out_channels)
|
| 41 |
+
self.output = nn.Linear(self.output_in_features, self.output_out_features)
|
| 42 |
+
if self.ar_window_size > 0:
|
| 43 |
+
self.ar = nn.Linear(self.ar_window_size, 1)
|
| 44 |
+
|
| 45 |
+
def forward(self, X, fut_time):
|
| 46 |
+
"""
|
| 47 |
+
Parameters:
|
| 48 |
+
X (tensor) [batch_size, time_steps, num_features]
|
| 49 |
+
"""
|
| 50 |
+
batch_size = X.size(0)
|
| 51 |
+
|
| 52 |
+
# Convolutional Layer
|
| 53 |
+
C = X.unsqueeze(1) # [batch_size, num_channels=1, time_steps, num_features]
|
| 54 |
+
C = F.relu(self.conv1(C)) # [batch_size, conv1_out_channels, shrinked_time_steps, 1]
|
| 55 |
+
C = self.dropout(C)
|
| 56 |
+
C = torch.squeeze(C, 3) # [batch_size, conv1_out_channels, shrinked_time_steps]
|
| 57 |
+
|
| 58 |
+
# Recurrent Layer
|
| 59 |
+
R = C.permute(0, 2, 1) # [batch_size, shrinked_time_steps, conv1_out_channels]
|
| 60 |
+
out, hidden = self.recc1(R) # [batch_size, shrinked_time_steps, recc_out_channels]
|
| 61 |
+
R = out[:, -1, :] # [batch_size, recc_out_channels]
|
| 62 |
+
R = self.dropout(R)
|
| 63 |
+
#print(R.shape)
|
| 64 |
+
|
| 65 |
+
# Skip Recurrent Layers
|
| 66 |
+
shrinked_time_steps = C.size(2)
|
| 67 |
+
for i in range(len(self.skip_steps)):
|
| 68 |
+
skip_step = self.skip_steps[i]
|
| 69 |
+
skip_sequence_len = shrinked_time_steps // skip_step
|
| 70 |
+
# shrinked_time_steps shrinked further
|
| 71 |
+
S = C[:, :, -skip_sequence_len*skip_step:] # [batch_size, conv1_out_channels, shrinked_time_steps]
|
| 72 |
+
S = S.view(S.size(0), S.size(1), skip_sequence_len, skip_step) # [batch_size, conv1_out_channels, skip_sequence_len, skip_step=num_skip_components]
|
| 73 |
+
# note that num_skip_components = skip_step
|
| 74 |
+
S = S.permute(0, 3, 2, 1).contiguous() # [batch_size, skip_step=num_skip_components, skip_sequence_len, conv1_out_channels]
|
| 75 |
+
S = S.view(S.size(0)*S.size(1), S.size(2), S.size(3)) # [batch_size*num_skip_components, skip_sequence_len, conv1_out_channels]
|
| 76 |
+
out, hidden = self.skip_reccs[i](S) # [batch_size*num_skip_components, skip_sequence_len, skip_reccs_out_channels[i]]
|
| 77 |
+
S = out[:, -1, :] # [batch_size*num_skip_components, skip_reccs_out_channels[i]]
|
| 78 |
+
S = S.view(batch_size, skip_step*S.size(1)) # [batch_size, num_skip_components*skip_reccs_out_channels[i]]
|
| 79 |
+
S = self.dropout(S)
|
| 80 |
+
R = torch.cat((R, S), 1) # [batch_size, recc_out_channels + skip_reccs_out_channels * num_skip_components]
|
| 81 |
+
#print(S.shape)
|
| 82 |
+
#print(R.shape)
|
| 83 |
+
|
| 84 |
+
# Output Layer
|
| 85 |
+
O = F.relu(self.output(R)) # [batch_size, output_out_features=1]
|
| 86 |
+
|
| 87 |
+
if self.ar_window_size > 0:
|
| 88 |
+
# set dim3 based on output_out_features
|
| 89 |
+
AR = X[:, -self.ar_window_size:, 3:4] # [batch_size, ar_window_size, output_out_features=1]
|
| 90 |
+
AR = AR.permute(0, 2, 1).contiguous() # [batch_size, output_out_features, ar_window_size]
|
| 91 |
+
AR = self.ar(AR) # [batch_size, output_out_features, 1]
|
| 92 |
+
AR = AR.squeeze(2) # [batch_size, output_out_features]
|
| 93 |
+
O = O + AR
|
| 94 |
+
|
| 95 |
+
return O
|
models/TimesNet.py
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import torch.fft
|
| 5 |
+
import math
|
| 6 |
+
|
| 7 |
+
class Inception_Block_V1(nn.Module):
|
| 8 |
+
def __init__(self, in_channels, out_channels, num_kernels=6, init_weight=True):
|
| 9 |
+
super(Inception_Block_V1, self).__init__()
|
| 10 |
+
self.in_channels = in_channels
|
| 11 |
+
self.out_channels = out_channels
|
| 12 |
+
self.num_kernels = num_kernels
|
| 13 |
+
kernels = []
|
| 14 |
+
for i in range(self.num_kernels):
|
| 15 |
+
kernels.append(nn.Conv2d(in_channels, out_channels, kernel_size=2 * i + 1, padding=i))
|
| 16 |
+
self.kernels = nn.ModuleList(kernels)
|
| 17 |
+
if init_weight:
|
| 18 |
+
self._initialize_weights()
|
| 19 |
+
|
| 20 |
+
def _initialize_weights(self):
|
| 21 |
+
for m in self.modules():
|
| 22 |
+
if isinstance(m, nn.Conv2d):
|
| 23 |
+
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
| 24 |
+
if m.bias is not None:
|
| 25 |
+
nn.init.constant_(m.bias, 0)
|
| 26 |
+
|
| 27 |
+
def forward(self, x):
|
| 28 |
+
res_list = []
|
| 29 |
+
for i in range(self.num_kernels):
|
| 30 |
+
res_list.append(self.kernels[i](x))
|
| 31 |
+
res = torch.stack(res_list, dim=-1).mean(-1)
|
| 32 |
+
return res
|
| 33 |
+
|
| 34 |
+
class PositionalEmbedding(nn.Module):
|
| 35 |
+
def __init__(self, d_model, max_len=5000):
|
| 36 |
+
super(PositionalEmbedding, self).__init__()
|
| 37 |
+
# Compute the positional encodings once in log space.
|
| 38 |
+
pe = torch.zeros(max_len, d_model).float()
|
| 39 |
+
pe.require_grad = False
|
| 40 |
+
|
| 41 |
+
position = torch.arange(0, max_len).float().unsqueeze(1)
|
| 42 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 43 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 44 |
+
|
| 45 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 46 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 47 |
+
|
| 48 |
+
pe = pe.unsqueeze(0)
|
| 49 |
+
self.register_buffer('pe', pe)
|
| 50 |
+
|
| 51 |
+
def forward(self, x):
|
| 52 |
+
return self.pe[:, :x.size(1)]
|
| 53 |
+
|
| 54 |
+
class FixedEmbedding(nn.Module):
|
| 55 |
+
def __init__(self, c_in, d_model):
|
| 56 |
+
super(FixedEmbedding, self).__init__()
|
| 57 |
+
|
| 58 |
+
w = torch.zeros(c_in, d_model).float()
|
| 59 |
+
w.require_grad = False
|
| 60 |
+
|
| 61 |
+
position = torch.arange(0, c_in).float().unsqueeze(1)
|
| 62 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 63 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 64 |
+
|
| 65 |
+
w[:, 0::2] = torch.sin(position * div_term)
|
| 66 |
+
w[:, 1::2] = torch.cos(position * div_term)
|
| 67 |
+
|
| 68 |
+
self.emb = nn.Embedding(c_in, d_model)
|
| 69 |
+
self.emb.weight = nn.Parameter(w, requires_grad=False)
|
| 70 |
+
|
| 71 |
+
def forward(self, x):
|
| 72 |
+
return self.emb(x).detach()
|
| 73 |
+
|
| 74 |
+
class TemporalEmbedding(nn.Module):
|
| 75 |
+
def __init__(self, d_model, embed_type='fixed', freq='h'):
|
| 76 |
+
super(TemporalEmbedding, self).__init__()
|
| 77 |
+
|
| 78 |
+
hour_size = 96
|
| 79 |
+
weekday_size = 7
|
| 80 |
+
|
| 81 |
+
Embed = FixedEmbedding if embed_type == 'fixed' else nn.Embedding
|
| 82 |
+
self.hour_embed = Embed(hour_size, d_model)
|
| 83 |
+
self.weekday_embed = Embed(weekday_size, d_model)
|
| 84 |
+
|
| 85 |
+
def forward(self, x):
|
| 86 |
+
x = x.long()
|
| 87 |
+
hour_x = self.hour_embed(x[:, :, 0])
|
| 88 |
+
weekday_x = self.weekday_embed(x[:, :, 1])
|
| 89 |
+
|
| 90 |
+
return hour_x + weekday_x
|
| 91 |
+
|
| 92 |
+
class TokenEmbedding(nn.Module):
|
| 93 |
+
def __init__(self, c_in, d_model):
|
| 94 |
+
super(TokenEmbedding, self).__init__()
|
| 95 |
+
padding = 1 if torch.__version__ >= '1.5.0' else 2
|
| 96 |
+
self.tokenConv = nn.Conv1d(in_channels=c_in, out_channels=d_model,
|
| 97 |
+
kernel_size=3, padding=padding, padding_mode='circular', bias=False)
|
| 98 |
+
for m in self.modules():
|
| 99 |
+
if isinstance(m, nn.Conv1d):
|
| 100 |
+
nn.init.kaiming_normal_(
|
| 101 |
+
m.weight, mode='fan_in', nonlinearity='leaky_relu')
|
| 102 |
+
|
| 103 |
+
def forward(self, x):
|
| 104 |
+
x = self.tokenConv(x.permute(0, 2, 1)).transpose(1, 2)
|
| 105 |
+
return x
|
| 106 |
+
|
| 107 |
+
class DataEmbedding(nn.Module):
|
| 108 |
+
def __init__(self, c_in, d_model, embed_type='fixed', freq='h', dropout=0.1):
|
| 109 |
+
super(DataEmbedding, self).__init__()
|
| 110 |
+
|
| 111 |
+
self.value_embedding = TokenEmbedding(c_in=c_in, d_model=d_model)
|
| 112 |
+
self.position_embedding = PositionalEmbedding(d_model=d_model)
|
| 113 |
+
self.temporal_embedding = TemporalEmbedding(d_model=d_model, embed_type=embed_type,
|
| 114 |
+
freq=freq)
|
| 115 |
+
self.dropout = nn.Dropout(p=dropout)
|
| 116 |
+
|
| 117 |
+
def forward(self, x, x_mark):
|
| 118 |
+
if x_mark is None:
|
| 119 |
+
x = self.value_embedding(x) + self.position_embedding(x)
|
| 120 |
+
else:
|
| 121 |
+
x = self.value_embedding(
|
| 122 |
+
x) + self.temporal_embedding(x_mark) + self.position_embedding(x)
|
| 123 |
+
return self.dropout(x)
|
| 124 |
+
|
| 125 |
+
def FFT_for_Period(x, k=2):
|
| 126 |
+
# [B, T, C]
|
| 127 |
+
xf = torch.fft.rfft(x, dim=1)
|
| 128 |
+
# find period by amplitudes
|
| 129 |
+
frequency_list = abs(xf).mean(0).mean(-1)
|
| 130 |
+
frequency_list[0] = 0
|
| 131 |
+
_, top_list = torch.topk(frequency_list, k)
|
| 132 |
+
top_list = top_list.detach().cpu().numpy()
|
| 133 |
+
period = x.shape[1] // top_list
|
| 134 |
+
return period, abs(xf).mean(-1)[:, top_list]
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
class TimesBlock(nn.Module):
|
| 138 |
+
def __init__(self, seq_len, pred_len, top_k, d_model, d_ff, num_kernels):
|
| 139 |
+
super(TimesBlock, self).__init__()
|
| 140 |
+
self.seq_len = seq_len
|
| 141 |
+
self.pred_len = pred_len
|
| 142 |
+
self.k = top_k
|
| 143 |
+
# parameter-efficient design
|
| 144 |
+
self.conv = nn.Sequential(
|
| 145 |
+
Inception_Block_V1(d_model, d_ff,
|
| 146 |
+
num_kernels=num_kernels),
|
| 147 |
+
nn.GELU(),
|
| 148 |
+
Inception_Block_V1(d_ff, d_model,
|
| 149 |
+
num_kernels=num_kernels)
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
def forward(self, x):
|
| 153 |
+
B, T, N = x.size()
|
| 154 |
+
period_list, period_weight = FFT_for_Period(x, self.k)
|
| 155 |
+
|
| 156 |
+
res = []
|
| 157 |
+
for i in range(self.k):
|
| 158 |
+
period = period_list[i]
|
| 159 |
+
# padding
|
| 160 |
+
if (self.seq_len + self.pred_len) % period != 0:
|
| 161 |
+
length = (
|
| 162 |
+
((self.seq_len + self.pred_len) // period) + 1) * period
|
| 163 |
+
padding = torch.zeros([x.shape[0], (length - (self.seq_len + self.pred_len)), x.shape[2]]).to(x.device)
|
| 164 |
+
out = torch.cat([x, padding], dim=1)
|
| 165 |
+
else:
|
| 166 |
+
length = (self.seq_len + self.pred_len)
|
| 167 |
+
out = x
|
| 168 |
+
# reshape
|
| 169 |
+
out = out.reshape(B, length // period, period,
|
| 170 |
+
N).permute(0, 3, 1, 2).contiguous()
|
| 171 |
+
# 2D conv: from 1d Variation to 2d Variation
|
| 172 |
+
out = self.conv(out)
|
| 173 |
+
# reshape back
|
| 174 |
+
out = out.permute(0, 2, 3, 1).reshape(B, -1, N)
|
| 175 |
+
res.append(out[:, :(self.seq_len + self.pred_len), :])
|
| 176 |
+
res = torch.stack(res, dim=-1)
|
| 177 |
+
# adaptive aggregation
|
| 178 |
+
period_weight = F.softmax(period_weight, dim=1)
|
| 179 |
+
period_weight = period_weight.unsqueeze(
|
| 180 |
+
1).unsqueeze(1).repeat(1, T, N, 1)
|
| 181 |
+
res = torch.sum(res * period_weight, -1)
|
| 182 |
+
# residual connection
|
| 183 |
+
res = res + x
|
| 184 |
+
return res
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
class TimesNet(nn.Module):
|
| 188 |
+
"""
|
| 189 |
+
Paper link: https://openreview.net/pdf?id=ju_Uqw384Oq
|
| 190 |
+
"""
|
| 191 |
+
|
| 192 |
+
def __init__(
|
| 193 |
+
self,
|
| 194 |
+
enc_in,
|
| 195 |
+
dec_in,
|
| 196 |
+
c_out,
|
| 197 |
+
pred_len,
|
| 198 |
+
seq_len,
|
| 199 |
+
output_attention = False,
|
| 200 |
+
data_idx = [0,3,4,5,6,7],
|
| 201 |
+
time_idx = [1,2],
|
| 202 |
+
d_model = 16,
|
| 203 |
+
d_ff = 64,
|
| 204 |
+
e_layers = 2,
|
| 205 |
+
top_k = 5,
|
| 206 |
+
num_kernels = 2,
|
| 207 |
+
dropout = 0.1
|
| 208 |
+
):
|
| 209 |
+
super(TimesNet, self).__init__()
|
| 210 |
+
|
| 211 |
+
self.data_idx = data_idx
|
| 212 |
+
self.time_idx = time_idx
|
| 213 |
+
self.dec_in = dec_in
|
| 214 |
+
|
| 215 |
+
self.seq_len = seq_len
|
| 216 |
+
self.pred_len = pred_len
|
| 217 |
+
self.model = nn.ModuleList([TimesBlock(seq_len, pred_len, top_k, d_model, d_ff, num_kernels)
|
| 218 |
+
for _ in range(e_layers)])
|
| 219 |
+
self.enc_embedding = DataEmbedding(enc_in, d_model, 'fixed', 'h',
|
| 220 |
+
dropout)
|
| 221 |
+
self.layer = e_layers
|
| 222 |
+
self.layer_norm = nn.LayerNorm(d_model)
|
| 223 |
+
self.predict_linear = nn.Linear(
|
| 224 |
+
self.seq_len, self.pred_len + self.seq_len)
|
| 225 |
+
self.projection = nn.Linear(
|
| 226 |
+
d_model, c_out, bias=True)
|
| 227 |
+
|
| 228 |
+
def forecast(self, x_enc, x_mark_enc, x_dec, x_mark_dec):
|
| 229 |
+
# Normalization from Non-stationary Transformer
|
| 230 |
+
means = x_enc.mean(1, keepdim=True).detach()
|
| 231 |
+
x_enc = x_enc - means
|
| 232 |
+
stdev = torch.sqrt(
|
| 233 |
+
torch.var(x_enc, dim=1, keepdim=True, unbiased=False) + 1e-5)
|
| 234 |
+
x_enc /= stdev
|
| 235 |
+
|
| 236 |
+
# embedding
|
| 237 |
+
enc_out = self.enc_embedding(x_enc, x_mark_enc) # [B,T,C]
|
| 238 |
+
enc_out = self.predict_linear(enc_out.permute(0, 2, 1)).permute(
|
| 239 |
+
0, 2, 1) # align temporal dimension
|
| 240 |
+
# TimesNet
|
| 241 |
+
for i in range(self.layer):
|
| 242 |
+
enc_out = self.layer_norm(self.model[i](enc_out))
|
| 243 |
+
# porject back
|
| 244 |
+
dec_out = self.projection(enc_out)
|
| 245 |
+
|
| 246 |
+
# De-Normalization from Non-stationary Transformer
|
| 247 |
+
dec_out = dec_out * \
|
| 248 |
+
(stdev[:, 0, :].unsqueeze(1).repeat(
|
| 249 |
+
1, self.pred_len + self.seq_len, 1))
|
| 250 |
+
dec_out = dec_out + \
|
| 251 |
+
(means[:, 0, :].unsqueeze(1).repeat(
|
| 252 |
+
1, self.pred_len + self.seq_len, 1))
|
| 253 |
+
return dec_out
|
| 254 |
+
|
| 255 |
+
def forward(self, x, fut_time):
|
| 256 |
+
|
| 257 |
+
x_enc = x[:,:,self.data_idx]
|
| 258 |
+
x_mark_enc = x[:,:,self.time_idx]
|
| 259 |
+
x_dec = torch.zeros((fut_time.shape[0],fut_time.shape[1],self.dec_in),dtype=fut_time.dtype,device=fut_time.device)
|
| 260 |
+
x_mark_dec = fut_time
|
| 261 |
+
|
| 262 |
+
return self.forecast(x_enc,x_mark_enc,x_dec,x_mark_dec)[:,-1,[0]]
|
models/Transformer.py
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
import math
|
| 6 |
+
from math import sqrt
|
| 7 |
+
|
| 8 |
+
class TriangularCausalMask():
|
| 9 |
+
def __init__(self, B, L, device="cpu"):
|
| 10 |
+
mask_shape = [B, 1, L, L]
|
| 11 |
+
with torch.no_grad():
|
| 12 |
+
self._mask = torch.triu(torch.ones(mask_shape, dtype=torch.bool), diagonal=1).to(device)
|
| 13 |
+
|
| 14 |
+
@property
|
| 15 |
+
def mask(self):
|
| 16 |
+
return self._mask
|
| 17 |
+
|
| 18 |
+
class PositionalEmbedding(nn.Module):
|
| 19 |
+
def __init__(self, d_model, max_len=5000):
|
| 20 |
+
super(PositionalEmbedding, self).__init__()
|
| 21 |
+
# Compute the positional encodings once in log space.
|
| 22 |
+
pe = torch.zeros(max_len, d_model).float()
|
| 23 |
+
pe.require_grad = False
|
| 24 |
+
|
| 25 |
+
position = torch.arange(0, max_len).float().unsqueeze(1)
|
| 26 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 27 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 28 |
+
|
| 29 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 30 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 31 |
+
|
| 32 |
+
pe = pe.unsqueeze(0)
|
| 33 |
+
self.register_buffer('pe', pe)
|
| 34 |
+
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
return self.pe[:, :x.size(1)]
|
| 37 |
+
|
| 38 |
+
class FixedEmbedding(nn.Module):
|
| 39 |
+
def __init__(self, c_in, d_model):
|
| 40 |
+
super(FixedEmbedding, self).__init__()
|
| 41 |
+
|
| 42 |
+
w = torch.zeros(c_in, d_model).float()
|
| 43 |
+
w.require_grad = False
|
| 44 |
+
|
| 45 |
+
position = torch.arange(0, c_in).float().unsqueeze(1)
|
| 46 |
+
div_term = (torch.arange(0, d_model, 2).float()
|
| 47 |
+
* -(math.log(10000.0) / d_model)).exp()
|
| 48 |
+
|
| 49 |
+
w[:, 0::2] = torch.sin(position * div_term)
|
| 50 |
+
w[:, 1::2] = torch.cos(position * div_term)
|
| 51 |
+
|
| 52 |
+
self.emb = nn.Embedding(c_in, d_model)
|
| 53 |
+
self.emb.weight = nn.Parameter(w, requires_grad=False)
|
| 54 |
+
|
| 55 |
+
def forward(self, x):
|
| 56 |
+
return self.emb(x).detach()
|
| 57 |
+
|
| 58 |
+
class TemporalEmbedding(nn.Module):
|
| 59 |
+
def __init__(self, d_model, embed_type='fixed', freq='h'):
|
| 60 |
+
super(TemporalEmbedding, self).__init__()
|
| 61 |
+
|
| 62 |
+
hour_size = 96
|
| 63 |
+
weekday_size = 7
|
| 64 |
+
|
| 65 |
+
Embed = FixedEmbedding if embed_type == 'fixed' else nn.Embedding
|
| 66 |
+
self.hour_embed = Embed(hour_size, d_model)
|
| 67 |
+
self.weekday_embed = Embed(weekday_size, d_model)
|
| 68 |
+
|
| 69 |
+
def forward(self, x):
|
| 70 |
+
x = x.long()
|
| 71 |
+
hour_x = self.hour_embed(x[:, :, 0])
|
| 72 |
+
weekday_x = self.weekday_embed(x[:, :, 1])
|
| 73 |
+
|
| 74 |
+
return hour_x + weekday_x
|
| 75 |
+
|
| 76 |
+
class TokenEmbedding(nn.Module):
|
| 77 |
+
def __init__(self, c_in, d_model):
|
| 78 |
+
super(TokenEmbedding, self).__init__()
|
| 79 |
+
padding = 1 if torch.__version__ >= '1.5.0' else 2
|
| 80 |
+
self.tokenConv = nn.Conv1d(in_channels=c_in, out_channels=d_model,
|
| 81 |
+
kernel_size=3, padding=padding, padding_mode='circular', bias=False)
|
| 82 |
+
for m in self.modules():
|
| 83 |
+
if isinstance(m, nn.Conv1d):
|
| 84 |
+
nn.init.kaiming_normal_(
|
| 85 |
+
m.weight, mode='fan_in', nonlinearity='leaky_relu')
|
| 86 |
+
|
| 87 |
+
def forward(self, x):
|
| 88 |
+
x = self.tokenConv(x.permute(0, 2, 1)).transpose(1, 2)
|
| 89 |
+
return x
|
| 90 |
+
|
| 91 |
+
class DataEmbedding(nn.Module):
|
| 92 |
+
def __init__(self, c_in, d_model, embed_type='fixed', freq='h', dropout=0.1):
|
| 93 |
+
super(DataEmbedding, self).__init__()
|
| 94 |
+
|
| 95 |
+
self.value_embedding = TokenEmbedding(c_in=c_in, d_model=d_model)
|
| 96 |
+
self.position_embedding = PositionalEmbedding(d_model=d_model)
|
| 97 |
+
self.temporal_embedding = TemporalEmbedding(d_model=d_model, embed_type=embed_type,
|
| 98 |
+
freq=freq)
|
| 99 |
+
self.dropout = nn.Dropout(p=dropout)
|
| 100 |
+
|
| 101 |
+
def forward(self, x, x_mark):
|
| 102 |
+
if x_mark is None:
|
| 103 |
+
x = self.value_embedding(x) + self.position_embedding(x)
|
| 104 |
+
else:
|
| 105 |
+
x = self.value_embedding(
|
| 106 |
+
x) + self.temporal_embedding(x_mark) + self.position_embedding(x)
|
| 107 |
+
return self.dropout(x)
|
| 108 |
+
|
| 109 |
+
class AttentionLayer(nn.Module):
|
| 110 |
+
def __init__(self, attention, d_model, n_heads, d_keys=None,
|
| 111 |
+
d_values=None):
|
| 112 |
+
super(AttentionLayer, self).__init__()
|
| 113 |
+
|
| 114 |
+
d_keys = d_keys or (d_model // n_heads)
|
| 115 |
+
d_values = d_values or (d_model // n_heads)
|
| 116 |
+
|
| 117 |
+
self.inner_attention = attention
|
| 118 |
+
self.query_projection = nn.Linear(d_model, d_keys * n_heads)
|
| 119 |
+
self.key_projection = nn.Linear(d_model, d_keys * n_heads)
|
| 120 |
+
self.value_projection = nn.Linear(d_model, d_values * n_heads)
|
| 121 |
+
self.out_projection = nn.Linear(d_values * n_heads, d_model)
|
| 122 |
+
self.n_heads = n_heads
|
| 123 |
+
|
| 124 |
+
def forward(self, queries, keys, values, attn_mask, tau=None, delta=None):
|
| 125 |
+
B, L, _ = queries.shape
|
| 126 |
+
_, S, _ = keys.shape
|
| 127 |
+
H = self.n_heads
|
| 128 |
+
|
| 129 |
+
queries = self.query_projection(queries).view(B, L, H, -1)
|
| 130 |
+
keys = self.key_projection(keys).view(B, S, H, -1)
|
| 131 |
+
values = self.value_projection(values).view(B, S, H, -1)
|
| 132 |
+
|
| 133 |
+
out, attn = self.inner_attention(
|
| 134 |
+
queries,
|
| 135 |
+
keys,
|
| 136 |
+
values,
|
| 137 |
+
attn_mask,
|
| 138 |
+
tau=tau,
|
| 139 |
+
delta=delta
|
| 140 |
+
)
|
| 141 |
+
out = out.view(B, L, -1)
|
| 142 |
+
|
| 143 |
+
return self.out_projection(out), attn
|
| 144 |
+
|
| 145 |
+
class FullAttention(nn.Module):
|
| 146 |
+
def __init__(self, mask_flag=True, factor=5, scale=None, attention_dropout=0.1, output_attention=False):
|
| 147 |
+
super(FullAttention, self).__init__()
|
| 148 |
+
self.scale = scale
|
| 149 |
+
self.mask_flag = mask_flag
|
| 150 |
+
self.output_attention = output_attention
|
| 151 |
+
self.dropout = nn.Dropout(attention_dropout)
|
| 152 |
+
|
| 153 |
+
def forward(self, queries, keys, values, attn_mask, tau=None, delta=None):
|
| 154 |
+
B, L, H, E = queries.shape
|
| 155 |
+
_, S, _, D = values.shape
|
| 156 |
+
scale = self.scale or 1. / sqrt(E)
|
| 157 |
+
|
| 158 |
+
scores = torch.einsum("blhe,bshe->bhls", queries, keys)
|
| 159 |
+
|
| 160 |
+
if self.mask_flag:
|
| 161 |
+
if attn_mask is None:
|
| 162 |
+
attn_mask = TriangularCausalMask(B, L, device=queries.device)
|
| 163 |
+
|
| 164 |
+
scores.masked_fill_(attn_mask.mask, -np.inf)
|
| 165 |
+
|
| 166 |
+
A = self.dropout(torch.softmax(scale * scores, dim=-1))
|
| 167 |
+
V = torch.einsum("bhls,bshd->blhd", A, values)
|
| 168 |
+
|
| 169 |
+
if self.output_attention:
|
| 170 |
+
return V.contiguous(), A
|
| 171 |
+
else:
|
| 172 |
+
return V.contiguous(), None
|
| 173 |
+
|
| 174 |
+
class ConvLayer(nn.Module):
|
| 175 |
+
def __init__(self, c_in):
|
| 176 |
+
super(ConvLayer, self).__init__()
|
| 177 |
+
self.downConv = nn.Conv1d(in_channels=c_in,
|
| 178 |
+
out_channels=c_in,
|
| 179 |
+
kernel_size=3,
|
| 180 |
+
padding=2,
|
| 181 |
+
padding_mode='circular')
|
| 182 |
+
self.norm = nn.BatchNorm1d(c_in)
|
| 183 |
+
self.activation = nn.ELU()
|
| 184 |
+
self.maxPool = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)
|
| 185 |
+
|
| 186 |
+
def forward(self, x):
|
| 187 |
+
x = self.downConv(x.permute(0, 2, 1))
|
| 188 |
+
x = self.norm(x)
|
| 189 |
+
x = self.activation(x)
|
| 190 |
+
x = self.maxPool(x)
|
| 191 |
+
x = x.transpose(1, 2)
|
| 192 |
+
return x
|
| 193 |
+
|
| 194 |
+
class EncoderLayer(nn.Module):
|
| 195 |
+
def __init__(self, attention, d_model, d_ff=None, dropout=0.1, activation="relu"):
|
| 196 |
+
super(EncoderLayer, self).__init__()
|
| 197 |
+
d_ff = d_ff or 4 * d_model
|
| 198 |
+
self.attention = attention
|
| 199 |
+
self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
|
| 200 |
+
self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
|
| 201 |
+
self.norm1 = nn.LayerNorm(d_model)
|
| 202 |
+
self.norm2 = nn.LayerNorm(d_model)
|
| 203 |
+
self.dropout = nn.Dropout(dropout)
|
| 204 |
+
self.activation = F.relu if activation == "relu" else F.gelu
|
| 205 |
+
|
| 206 |
+
def forward(self, x, attn_mask=None, tau=None, delta=None):
|
| 207 |
+
new_x, attn = self.attention(
|
| 208 |
+
x, x, x,
|
| 209 |
+
attn_mask=attn_mask,
|
| 210 |
+
tau=tau, delta=delta
|
| 211 |
+
)
|
| 212 |
+
x = x + self.dropout(new_x)
|
| 213 |
+
|
| 214 |
+
y = x = self.norm1(x)
|
| 215 |
+
y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
|
| 216 |
+
y = self.dropout(self.conv2(y).transpose(-1, 1))
|
| 217 |
+
|
| 218 |
+
return self.norm2(x + y), attn
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
class Encoder(nn.Module):
|
| 222 |
+
def __init__(self, attn_layers, conv_layers=None, norm_layer=None):
|
| 223 |
+
super(Encoder, self).__init__()
|
| 224 |
+
self.attn_layers = nn.ModuleList(attn_layers)
|
| 225 |
+
self.conv_layers = nn.ModuleList(conv_layers) if conv_layers is not None else None
|
| 226 |
+
self.norm = norm_layer
|
| 227 |
+
|
| 228 |
+
def forward(self, x, attn_mask=None, tau=None, delta=None):
|
| 229 |
+
# x [B, L, D]
|
| 230 |
+
attns = []
|
| 231 |
+
if self.conv_layers is not None:
|
| 232 |
+
for i, (attn_layer, conv_layer) in enumerate(zip(self.attn_layers, self.conv_layers)):
|
| 233 |
+
delta = delta if i == 0 else None
|
| 234 |
+
x, attn = attn_layer(x, attn_mask=attn_mask, tau=tau, delta=delta)
|
| 235 |
+
x = conv_layer(x)
|
| 236 |
+
attns.append(attn)
|
| 237 |
+
x, attn = self.attn_layers[-1](x, tau=tau, delta=None)
|
| 238 |
+
attns.append(attn)
|
| 239 |
+
else:
|
| 240 |
+
for attn_layer in self.attn_layers:
|
| 241 |
+
x, attn = attn_layer(x, attn_mask=attn_mask, tau=tau, delta=delta)
|
| 242 |
+
attns.append(attn)
|
| 243 |
+
|
| 244 |
+
if self.norm is not None:
|
| 245 |
+
x = self.norm(x)
|
| 246 |
+
|
| 247 |
+
return x, attns
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
class DecoderLayer(nn.Module):
|
| 251 |
+
def __init__(self, self_attention, cross_attention, d_model, d_ff=None,
|
| 252 |
+
dropout=0.1, activation="relu"):
|
| 253 |
+
super(DecoderLayer, self).__init__()
|
| 254 |
+
d_ff = d_ff or 4 * d_model
|
| 255 |
+
self.self_attention = self_attention
|
| 256 |
+
self.cross_attention = cross_attention
|
| 257 |
+
self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
|
| 258 |
+
self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
|
| 259 |
+
self.norm1 = nn.LayerNorm(d_model)
|
| 260 |
+
self.norm2 = nn.LayerNorm(d_model)
|
| 261 |
+
self.norm3 = nn.LayerNorm(d_model)
|
| 262 |
+
self.dropout = nn.Dropout(dropout)
|
| 263 |
+
self.activation = F.relu if activation == "relu" else F.gelu
|
| 264 |
+
|
| 265 |
+
def forward(self, x, cross, x_mask=None, cross_mask=None, tau=None, delta=None):
|
| 266 |
+
x = x + self.dropout(self.self_attention(
|
| 267 |
+
x, x, x,
|
| 268 |
+
attn_mask=x_mask,
|
| 269 |
+
tau=tau, delta=None
|
| 270 |
+
)[0])
|
| 271 |
+
x = self.norm1(x)
|
| 272 |
+
|
| 273 |
+
x = x + self.dropout(self.cross_attention(
|
| 274 |
+
x, cross, cross,
|
| 275 |
+
attn_mask=cross_mask,
|
| 276 |
+
tau=tau, delta=delta
|
| 277 |
+
)[0])
|
| 278 |
+
|
| 279 |
+
y = x = self.norm2(x)
|
| 280 |
+
y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
|
| 281 |
+
y = self.dropout(self.conv2(y).transpose(-1, 1))
|
| 282 |
+
|
| 283 |
+
return self.norm3(x + y)
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
class Decoder(nn.Module):
|
| 287 |
+
def __init__(self, layers, norm_layer=None, projection=None):
|
| 288 |
+
super(Decoder, self).__init__()
|
| 289 |
+
self.layers = nn.ModuleList(layers)
|
| 290 |
+
self.norm = norm_layer
|
| 291 |
+
self.projection = projection
|
| 292 |
+
|
| 293 |
+
def forward(self, x, cross, x_mask=None, cross_mask=None, tau=None, delta=None):
|
| 294 |
+
for layer in self.layers:
|
| 295 |
+
x = layer(x, cross, x_mask=x_mask, cross_mask=cross_mask, tau=tau, delta=delta)
|
| 296 |
+
|
| 297 |
+
if self.norm is not None:
|
| 298 |
+
x = self.norm(x)
|
| 299 |
+
|
| 300 |
+
if self.projection is not None:
|
| 301 |
+
x = self.projection(x)
|
| 302 |
+
return x
|
| 303 |
+
|
| 304 |
+
class Transformer(nn.Module):
|
| 305 |
+
"""
|
| 306 |
+
Vanilla Transformer
|
| 307 |
+
with O(L^2) complexity
|
| 308 |
+
Paper link: https://proceedings.neurips.cc/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf
|
| 309 |
+
"""
|
| 310 |
+
|
| 311 |
+
def __init__(
|
| 312 |
+
self,
|
| 313 |
+
enc_in,
|
| 314 |
+
dec_in,
|
| 315 |
+
c_out,
|
| 316 |
+
pred_len,
|
| 317 |
+
output_attention = False,
|
| 318 |
+
data_idx = [0,3,4,5,6,7],
|
| 319 |
+
time_idx = [1,2],
|
| 320 |
+
d_model = 16,
|
| 321 |
+
factor = 3,
|
| 322 |
+
n_heads = 4,
|
| 323 |
+
d_ff = 512,
|
| 324 |
+
d_layers = 3,
|
| 325 |
+
e_layers = 3,
|
| 326 |
+
activation = 'gelu',
|
| 327 |
+
dropout = 0.1
|
| 328 |
+
):
|
| 329 |
+
super(Transformer, self).__init__()
|
| 330 |
+
self.pred_len = pred_len
|
| 331 |
+
self.output_attention = output_attention
|
| 332 |
+
# save indices
|
| 333 |
+
self.data_idx = data_idx
|
| 334 |
+
self.time_idx = time_idx
|
| 335 |
+
self.dec_in = dec_in
|
| 336 |
+
# Embedding
|
| 337 |
+
self.enc_embedding = DataEmbedding(enc_in, d_model,'fixed', 'h',
|
| 338 |
+
dropout)
|
| 339 |
+
# Encoder
|
| 340 |
+
self.encoder = Encoder(
|
| 341 |
+
[
|
| 342 |
+
EncoderLayer(
|
| 343 |
+
AttentionLayer(
|
| 344 |
+
FullAttention(False, factor, attention_dropout=dropout,
|
| 345 |
+
output_attention=output_attention), d_model, n_heads),
|
| 346 |
+
d_model,
|
| 347 |
+
d_ff,
|
| 348 |
+
dropout=dropout,
|
| 349 |
+
activation=activation
|
| 350 |
+
) for l in range(e_layers)
|
| 351 |
+
],
|
| 352 |
+
norm_layer=torch.nn.LayerNorm(d_model)
|
| 353 |
+
)
|
| 354 |
+
# Decoder
|
| 355 |
+
self.dec_embedding = DataEmbedding(dec_in, d_model,'fixed', 'h',
|
| 356 |
+
dropout)
|
| 357 |
+
self.decoder = Decoder(
|
| 358 |
+
[
|
| 359 |
+
DecoderLayer(
|
| 360 |
+
AttentionLayer(
|
| 361 |
+
FullAttention(True, factor, attention_dropout=dropout,
|
| 362 |
+
output_attention=False),
|
| 363 |
+
d_model, n_heads),
|
| 364 |
+
AttentionLayer(
|
| 365 |
+
FullAttention(False, factor, attention_dropout=dropout,
|
| 366 |
+
output_attention=False),
|
| 367 |
+
d_model, n_heads),
|
| 368 |
+
d_model,
|
| 369 |
+
d_ff,
|
| 370 |
+
dropout=dropout,
|
| 371 |
+
activation=activation,
|
| 372 |
+
)
|
| 373 |
+
for l in range(d_layers)
|
| 374 |
+
],
|
| 375 |
+
norm_layer=torch.nn.LayerNorm(d_model),
|
| 376 |
+
projection=nn.Linear(d_model, c_out, bias=True)
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
def forecast(self, x_enc, x_mark_enc, x_dec, x_mark_dec):
|
| 380 |
+
# Embedding
|
| 381 |
+
enc_out = self.enc_embedding(x_enc, x_mark_enc)
|
| 382 |
+
enc_out, attns = self.encoder(enc_out, attn_mask=None)
|
| 383 |
+
|
| 384 |
+
dec_out = self.dec_embedding(x_dec, x_mark_dec)
|
| 385 |
+
dec_out = self.decoder(dec_out, enc_out, x_mask=None, cross_mask=None)
|
| 386 |
+
return dec_out
|
| 387 |
+
|
| 388 |
+
def forward(self, x, fut_time):
|
| 389 |
+
|
| 390 |
+
x_enc = x[:,:,self.data_idx]
|
| 391 |
+
x_mark_enc = x[:,:,self.time_idx]
|
| 392 |
+
x_dec = torch.zeros((fut_time.shape[0],fut_time.shape[1],self.dec_in),dtype=fut_time.dtype,device=fut_time.device)
|
| 393 |
+
x_mark_dec = fut_time
|
| 394 |
+
|
| 395 |
+
return self.forecast(x_enc,x_mark_enc,x_dec,x_mark_dec)[:,-1,[0]]
|
| 396 |
+
|
models/__pycache__/Autoformer.cpython-310.pyc
ADDED
|
Binary file (18.4 kB). View file
|
|
|
models/__pycache__/LSTM.cpython-310.pyc
ADDED
|
Binary file (1.63 kB). View file
|
|
|
models/__pycache__/LSTNet.cpython-310.pyc
ADDED
|
Binary file (2.54 kB). View file
|
|
|
weights/autoformer_L_96_T_48_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:18a3294861900282c8d001143dd6e61052e294465ddc1b77968cff9464eff7b2
|
| 3 |
+
size 5688452
|
weights/autoformer_L_96_T_48_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a8ba4d9cc688f64e8456f27286eb2412b844162211a07dd5d93aa9baf13acd69
|
| 3 |
+
size 5688452
|
weights/autoformer_L_96_T_4_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b6ef2bb0c2108017028fc57c70e3f9414f4abb07822ecfaf6d0387ad295a0d32
|
| 3 |
+
size 5688349
|
weights/autoformer_L_96_T_4_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1233196407b1b89c5f1c1aa69a330b9a11059d14502756c70158f020a29f43d1
|
| 3 |
+
size 5688349
|
weights/autoformer_L_96_T_96_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9efc3128fa75c1c9512e9429ec8fc930f9a4b97d9e573cf52bad5d1f4343f915
|
| 3 |
+
size 5688452
|
weights/autoformer_L_96_T_96_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:71a7b3da7d3ab3dfd4e3d91e9b24c3ddd8a1aa1414ec01b5a31dea02123305a5
|
| 3 |
+
size 5688452
|
weights/informer_L_96_T_48_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ab980f35a30ed0252b268184614bce5981a38d58de69270e8b71d546cf037e66
|
| 3 |
+
size 11244096
|
weights/informer_L_96_T_48_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5de041c5b3e5476c5e21b2549ed9ef2e42f36ef87f03eb3ea7c8a65ad41f397e
|
| 3 |
+
size 11244096
|
weights/informer_L_96_T_4_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:46f23ad6a7ad0c5a52b64f629b708d3c21fed9d8eddba0be9be136f17c4cfcbf
|
| 3 |
+
size 11243810
|
weights/informer_L_96_T_4_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:193a87c1eafeae1d193e298ef45d90b0e1c0a9e02674ba1e43d6914a769f4bf8
|
| 3 |
+
size 11243810
|
weights/informer_L_96_T_96_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a3289d7e42598e872c47c717e74534e2670d2b835149d80f134cf7bc4cef0900
|
| 3 |
+
size 11244096
|
weights/informer_L_96_T_96_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c51930737bfa82e805c4d21e5995bff2957bf2a83d74af6a9c5497915a8dafb
|
| 3 |
+
size 11244096
|
weights/lstm_L_96_T_48_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:94291c5486572cee3c7cd2c8c1b7a7415ec97484ca06173cd3a86d500b006df0
|
| 3 |
+
size 57534
|
weights/lstm_L_96_T_48_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ff835cd72503995f91b3bf7cf2032a102f80472122304258da385d56e2d4e708
|
| 3 |
+
size 57534
|
weights/lstm_L_96_T_4_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4c054d857c774996eef3b0a01225d346bcae05e1cd1de18e88e5b1ea8ec7bca7
|
| 3 |
+
size 57528
|
weights/lstm_L_96_T_4_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ee4c4e822c76f72ffda5df510cc938a3808af9e500bcf42746fbcbe53744ec9
|
| 3 |
+
size 57528
|
weights/lstm_L_96_T_96_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dcb5ceb916e486b1a6f93745b181b5b77950f746e0fcadb67b406e6e410b7c63
|
| 3 |
+
size 57534
|
weights/lstm_L_96_T_96_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:289eb2d01a3334be482f9726b87501507e405bbb6a6155b391c3f1ba3a01e7c2
|
| 3 |
+
size 57534
|
weights/lstnet_L_96_T_48_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:78c3e19fa8b924a15f3782086cff3af56955bea42e367570032f169a9688c0c6
|
| 3 |
+
size 270131
|
weights/lstnet_L_96_T_48_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:224386846b28ece3491a23994d91c7be2378e72146292c2a83375cf6f3a9bfbc
|
| 3 |
+
size 270131
|
weights/lstnet_L_96_T_4_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e39e8de2cefbff72f0afd46cdee468c0834373eaa6181f48ddc9b5c773342f2f
|
| 3 |
+
size 270118
|
weights/lstnet_L_96_T_4_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3de225ee33f9400f059fabf14d2be13ce0824ae65fdb687b6cd2b24bfc776345
|
| 3 |
+
size 270118
|
weights/lstnet_L_96_T_96_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7268ae84cc4c1ff8362ad17967d11cafc77693d4d3ba1b1236b2f9119ed07fdf
|
| 3 |
+
size 270131
|
weights/lstnet_L_96_T_96_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:23483f05784451adc89b8c02de3856ab327c33939f46f860097cf406bb60029f
|
| 3 |
+
size 270131
|
weights/timesnet_L_96_T_48_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6dfddb89e0d9d99381cbf34a0e281aa459fe3efd0867377d20a996dac7e7e5da
|
| 3 |
+
size 4001930
|
weights/timesnet_L_96_T_48_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d60566ce96344f60447743e22308020f5357ef10385ea7d32551f2ac9dd39fef
|
| 3 |
+
size 4001930
|
weights/timesnet_L_96_T_4_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:31f897bb5d72985f6edf5243b2cb36f79ebbef149f15f783e73499f13ac6d2e6
|
| 3 |
+
size 3984748
|
weights/timesnet_L_96_T_4_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8da9b302e17cadaae0d579e056cd5056bb698a6e723e4526485719cb2835f012
|
| 3 |
+
size 3984748
|
weights/timesnet_L_96_T_96_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9ae0b469bdf5e4e59bcbb1352c4c4915bf5d54329d3092e9dd7ad7fe598b3a05
|
| 3 |
+
size 4020554
|
weights/timesnet_L_96_T_96_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5caf3ecd2e1a98de5a657e691388eadfcd38c867de2d8777e66815d16fd52376
|
| 3 |
+
size 4020554
|
weights/transformer_L_96_T_48_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9880c6615410fe0beecb62b5744d94f18ea5d54aeaa360fc09bf1b854a383454
|
| 3 |
+
size 10841594
|
weights/transformer_L_96_T_48_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:60fde291ab8a817e94efc6b285f3025fb90ee16279c2b148e68743b1edd91e57
|
| 3 |
+
size 10841594
|
weights/transformer_L_96_T_4_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3ad641d745bd4c12791767d1795f437fc5f337f060796c897e76b24e543a421e
|
| 3 |
+
size 10841450
|
weights/transformer_L_96_T_4_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d839f3ab1dd0fbee2ba3b1335d2033624373098949057c0dad8788199df80615
|
| 3 |
+
size 10841450
|
weights/transformer_L_96_T_96_HET.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8d8cf4a6217b75bdb594a8976dad0844285b8780f818552211b0db170fad3da4
|
| 3 |
+
size 10841594
|
weights/transformer_L_96_T_96_HOM.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fecf4fed617509c693c275f20241a859a2087376cd4a5408afb34e1daf0454ff
|
| 3 |
+
size 10841594
|