-
-
Notifications
You must be signed in to change notification settings - Fork 107
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
Showing
8 changed files
with
508 additions
and
0 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
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,24 @@ | ||
""" | ||
The package including the modules of SegRNN. | ||
Refer to the paper | ||
`Lin, Shengsheng and Lin, Weiwei and Wu, Wentai and Zhao, Feiyu and Mo, Ruichao and Zhang, Haotong. | ||
Segrnn: Segment recurrent neural network for long-term time series forecasting. | ||
arXiv preprint arXiv:2308.11200. | ||
<https://arxiv.org/abs/2308.11200>`_ | ||
Notes | ||
----- | ||
This implementation is inspired by the official one https://github.com/lss-1138/SegRNN | ||
""" | ||
|
||
# Created by Shengsheng Lin | ||
|
||
|
||
|
||
from .model import SegRNN | ||
|
||
__all__ = [ | ||
"SegRNN", | ||
] |
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,59 @@ | ||
""" | ||
The core wrapper assembles the submodules of SegRNN imputation model | ||
and takes over the forward progress of the algorithm. | ||
""" | ||
|
||
# Created by Shengsheng Lin | ||
|
||
from typing import Optional | ||
|
||
from typing import Callable | ||
import torch.nn as nn | ||
|
||
from ...nn.modules.segrnn import BackboneSegRNN | ||
from ...nn.modules.saits import SaitsLoss | ||
|
||
class _SegRNN(nn.Module): | ||
def __init__( | ||
self, | ||
n_steps: int, | ||
n_features: int, | ||
seg_len: int = 24, | ||
d_model: int = 512, | ||
dropout: float = 0.5, | ||
ORT_weight: float = 1, | ||
MIT_weight: float = 1, | ||
): | ||
super().__init__() | ||
|
||
self.n_steps = n_steps | ||
self.n_features = n_features | ||
self.seg_len = seg_len | ||
self.d_model = d_model | ||
self.dropout = dropout | ||
|
||
self.backbone = BackboneSegRNN(n_steps, n_features, seg_len, d_model, dropout) | ||
|
||
# apply SAITS loss function to Transformer on the imputation task | ||
self.saits_loss_func = SaitsLoss(ORT_weight, MIT_weight) | ||
|
||
def forward(self, inputs: dict, training: bool = True) -> dict: | ||
X, missing_mask = inputs["X"], inputs["missing_mask"] | ||
|
||
reconstruction = self.backbone(X) | ||
|
||
imputed_data = missing_mask * X + (1 - missing_mask) * reconstruction | ||
results = { | ||
"imputed_data": imputed_data, | ||
} | ||
|
||
# if in training mode, return results with losses | ||
if training: | ||
X_ori, indicating_mask = inputs["X_ori"], inputs["indicating_mask"] | ||
loss, ORT_loss, MIT_loss = self.saits_loss_func(reconstruction, X_ori, missing_mask, indicating_mask) | ||
results["ORT_loss"] = ORT_loss | ||
results["MIT_loss"] = MIT_loss | ||
# `loss` is always the item for backward propagating to update the model | ||
results["loss"] = loss | ||
|
||
return results |
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,21 @@ | ||
""" | ||
Dataset class for the imputation model SegRNN. | ||
""" | ||
|
||
# Created by Shengsheng lin | ||
|
||
from typing import Union | ||
|
||
from pypots.imputation.saits.data import DatasetForSAITS | ||
|
||
|
||
class DatasetForSegRNN(DatasetForSAITS): | ||
def __init__( | ||
self, | ||
data: Union[dict, str], | ||
return_X_ori: bool, | ||
return_y: bool, | ||
file_type: str = "hdf5", | ||
rate: float = 0.2, | ||
): | ||
super().__init__(data, return_X_ori, return_y, file_type, rate) |
Oops, something went wrong.