-
Notifications
You must be signed in to change notification settings - Fork 12
/
xlstm.py
134 lines (82 loc) · 3.41 KB
/
xlstm.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
import torch.nn.functional as F
from torch import nn
import torch
import argparse
import numpy as np
from einops import rearrange
import argparse
from xlstm1.xlstm_block_stack import xLSTMBlockStack, xLSTMBlockStackConfig
from xlstm1.blocks.mlstm.block import mLSTMBlockConfig
from xlstm1.blocks.slstm.block import sLSTMBlockConfig
mlstm_config = mLSTMBlockConfig()
slstm_config = sLSTMBlockConfig()
config = xLSTMBlockStackConfig(
mlstm_block=mlstm_config,
slstm_block=slstm_config,
num_blocks=3,
embedding_dim=256,
add_post_blocks_norm=True,
_block_map = 1,
#slstm_at="all",
context_length=336
)
class moving_avg(nn.Module):
"""
Moving average block to highlight the trend of time series
"""
def __init__(self, kernel_size, stride):
super(moving_avg, self).__init__()
self.kernel_size = kernel_size
self.avg = nn.AvgPool1d(kernel_size=kernel_size, stride=stride, padding=0)
def forward(self, x):
# padding on the both ends of time series
front = x[:, 0:1, :].repeat(1, (self.kernel_size - 1) // 2, 1)
end = x[:, -1:, :].repeat(1, (self.kernel_size - 1) // 2, 1)
x = torch.cat([front, x, end], dim=1)
x = self.avg(x.permute(0, 2, 1))
x = x.permute(0, 2, 1)
return x
class series_decomp2(nn.Module):
"""
Series decomposition block
"""
def __init__(self, kernel_size):
super(series_decomp2, self).__init__()
self.moving_avg = moving_avg(kernel_size, stride=1)
def forward(self, x):
moving_mean = self.moving_avg(x)
res = x - moving_mean
return res, moving_mean
class xlstm(torch.nn.Module):
def __init__(self, configs, enc_in):
super(xlstm, self).__init__()
self.configs = configs
self.enc_in = enc_in
self.batch_norm = nn.BatchNorm1d(self.enc_in)
# Decompsition Kernel Size
kernel_size = 25
self.decompsition = series_decomp2(kernel_size)
self.Linear_Seasonal = nn.Linear(configs.context_points,configs.target_points)
self.Linear_Trend = nn.Linear(configs.context_points,configs.target_points)
self.Linear_Decoder = nn.Linear(configs.context_points,configs.target_points)
self.Linear_Seasonal.weight = nn.Parameter((1/configs.context_points)*torch.ones([configs.target_points,configs.context_points]))
self.Linear_Trend.weight = nn.Parameter((1/configs.context_points)*torch.ones([configs.target_points,configs.context_points]))
self.mm= nn.Linear(self.configs.target_points, self.configs.n2)
self.mm2= nn.Linear(config.embedding_dim, configs.target_points)
self.mm3= nn.Linear(configs.context_points,self.configs.n2)
self.xlstm_stack = xLSTMBlockStack(config )
def forward(self, x):
#print(x.shape)
seasonal_init, trend_init = self.decompsition(x)
seasonal_init, trend_init = seasonal_init.permute(0,2,1), trend_init.permute(0,2,1)
seasonal_output = self.Linear_Seasonal(seasonal_init)
trend_output = self.Linear_Trend(trend_init)
x = seasonal_output + trend_output
#print(x.shape)
x=self.mm(x)
#print(x.shape)
#x = self.batch_norm(x)
x = self.xlstm_stack(x)
x=self.mm2(x)
x=x.permute(0,2,1)
return x