-
Notifications
You must be signed in to change notification settings - Fork 5
/
DishTS.py
50 lines (44 loc) · 2.22 KB
/
DishTS.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class DishTS(nn.Module):
def __init__(self, args):
super().__init__()
init = args.dish_init #'standard', 'avg' or 'uniform'
activate = True
n_series = args.n_series # number of series
lookback = args.seq_len # lookback length
if init == 'standard':
self.reduce_mlayer = nn.Parameter(torch.rand(n_series, lookback, 2)/lookback)
elif init == 'avg':
self.reduce_mlayer = nn.Parameter(torch.ones(n_series, lookback, 2)/lookback)
elif init == 'uniform':
self.reduce_mlayer = nn.Parameter(torch.ones(n_series, lookback, 2)/lookback+torch.rand(n_series, lookback, 2)/lookback)
self.gamma, self.beta = nn.Parameter(torch.ones(n_series)), nn.Parameter(torch.zeros(n_series))
self.activate = activate
def forward(self, batch_x, mode='forward', dec_inp=None):
if mode == 'forward':
# batch_x: B*L*D || dec_inp: B*?*D (for xxformers)
self.preget(batch_x)
batch_x = self.forward_process(batch_x)
dec_inp = None if dec_inp is None else self.forward_process(dec_inp)
return batch_x, dec_inp
elif mode == 'inverse':
# batch_x: B*H*D (forecasts)
batch_y = self.inverse_process(batch_x)
return batch_y
def preget(self, batch_x):
x_transpose = batch_x.permute(2,0,1)
theta = torch.bmm(x_transpose, self.reduce_mlayer).permute(1,2,0)
if self.activate:
theta = F.gelu(theta)
self.phil, self.phih = theta[:,:1,:], theta[:,1:,:]
self.xil = torch.sum(torch.pow(batch_x - self.phil,2), axis=1, keepdim=True) / (batch_x.shape[1]-1)
self.xih = torch.sum(torch.pow(batch_x - self.phih,2), axis=1, keepdim=True) / (batch_x.shape[1]-1)
def forward_process(self, batch_input):
#print(batch_input.shape, self.phil.shape, self.xih.shape)
temp = (batch_input - self.phil)/torch.sqrt(self.xil + 1e-8)
rst = temp.mul(self.gamma) + self.beta
return rst
def inverse_process(self, batch_input):
return ((batch_input - self.beta) / self.gamma) * torch.sqrt(self.xih + 1e-8) + self.phih