forked from thuml/Time-Series-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
weiming.wsy
committed
Mar 27, 2024
1 parent
9a875da
commit adc8819
Showing
22 changed files
with
2,345 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class Normalize(nn.Module): | ||
def __init__(self, num_features: int, eps=1e-5, affine=False, subtract_last=False, non_norm=False): | ||
""" | ||
:param num_features: the number of features or channels | ||
:param eps: a value added for numerical stability | ||
:param affine: if True, RevIN has learnable affine parameters | ||
""" | ||
super(Normalize, self).__init__() | ||
self.num_features = num_features | ||
self.eps = eps | ||
self.affine = affine | ||
self.subtract_last = subtract_last | ||
self.non_norm = non_norm | ||
if self.affine: | ||
self._init_params() | ||
|
||
def forward(self, x, mode: str): | ||
if mode == 'norm': | ||
self._get_statistics(x) | ||
x = self._normalize(x) | ||
elif mode == 'denorm': | ||
x = self._denormalize(x) | ||
else: | ||
raise NotImplementedError | ||
return x | ||
|
||
def _init_params(self): | ||
# initialize RevIN params: (C,) | ||
self.affine_weight = nn.Parameter(torch.ones(self.num_features)) | ||
self.affine_bias = nn.Parameter(torch.zeros(self.num_features)) | ||
|
||
def _get_statistics(self, x): | ||
dim2reduce = tuple(range(1, x.ndim - 1)) | ||
if self.subtract_last: | ||
self.last = x[:, -1, :].unsqueeze(1) | ||
else: | ||
self.mean = torch.mean(x, dim=dim2reduce, keepdim=True).detach() | ||
self.stdev = torch.sqrt(torch.var(x, dim=dim2reduce, keepdim=True, unbiased=False) + self.eps).detach() | ||
|
||
def _normalize(self, x): | ||
if self.non_norm: | ||
return x | ||
if self.subtract_last: | ||
x = x - self.last | ||
else: | ||
x = x - self.mean | ||
x = x / self.stdev | ||
if self.affine: | ||
x = x * self.affine_weight | ||
x = x + self.affine_bias | ||
return x | ||
|
||
def _denormalize(self, x): | ||
if self.non_norm: | ||
return x | ||
if self.affine: | ||
x = x - self.affine_bias | ||
x = x / (self.affine_weight + self.eps * self.eps) | ||
x = x * self.stdev | ||
if self.subtract_last: | ||
x = x + self.last | ||
else: | ||
x = x + self.mean | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import torch.nn as nn | ||
|
||
|
||
class ResBlock(nn.Module): | ||
def __init__(self, configs): | ||
super(ResBlock, self).__init__() | ||
|
||
self.temporal = nn.Sequential( | ||
nn.Linear(configs.seq_len, configs.d_model), | ||
nn.ReLU(), | ||
nn.Linear(configs.d_model, configs.seq_len), | ||
nn.Dropout(configs.dropout) | ||
) | ||
|
||
self.channel = nn.Sequential( | ||
nn.Linear(configs.enc_in, configs.d_model), | ||
nn.ReLU(), | ||
nn.Linear(configs.d_model, configs.enc_in), | ||
nn.Dropout(configs.dropout) | ||
) | ||
|
||
def forward(self, x): | ||
# x: [B, L, D] | ||
x = x + self.temporal(x.transpose(1, 2)).transpose(1, 2) | ||
x = x + self.channel(x) | ||
|
||
return x | ||
|
||
|
||
class Model(nn.Module): | ||
def __init__(self, configs): | ||
super(Model, self).__init__() | ||
self.task_name = configs.task_name | ||
self.layer = configs.e_layers | ||
self.model = nn.ModuleList([ResBlock(configs) | ||
for _ in range(configs.e_layers)]) | ||
self.pred_len = configs.pred_len | ||
self.projection = nn.Linear(configs.seq_len, configs.pred_len) | ||
|
||
def forecast(self, x_enc, x_mark_enc, x_dec, x_mark_dec, mask=None): | ||
|
||
# x: [B, L, D] | ||
for i in range(self.layer): | ||
x_enc = self.model[i](x_enc) | ||
enc_out = self.projection(x_enc.transpose(1, 2)).transpose(1, 2) | ||
|
||
return enc_out | ||
|
||
def forward(self, x_enc, x_mark_enc, x_dec, x_mark_dec, mask=None): | ||
if self.task_name == 'long_term_forecast' or self.task_name == 'short_term_forecast': | ||
dec_out = self.forecast(x_enc, x_mark_enc, x_dec, x_mark_dec) | ||
return dec_out[:, -self.pred_len:, :] # [B, L, D] | ||
else: | ||
raise ValueError('Only forecast tasks implemented yet') |
Oops, something went wrong.