-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
80b4ab7
commit 8235045
Showing
19 changed files
with
240 additions
and
41 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
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
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,6 @@ | ||
from .ar import AutoRegressiveModel | ||
from .builder import build_local_scaler | ||
from .laststep import LastStep | ||
from .noop import NOOP | ||
|
||
__all__ = ["AutoRegressiveModel", "build_local_scaler", "LastStep", "NOOP"] |
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,43 @@ | ||
from torch import Tensor | ||
from torch.nn import Linear | ||
from tsts.cfg import CfgNode as CN | ||
from tsts.core import LOCALSCALERS | ||
from tsts.models import Module | ||
|
||
__all__ = ["AutoRegressiveModel"] | ||
|
||
|
||
@LOCALSCALERS.register() | ||
class AutoRegressiveModel(Module): | ||
def __init__( | ||
self, | ||
num_in_feats: int, | ||
num_out_feats: int, | ||
num_steps: int, | ||
) -> None: | ||
super(AutoRegressiveModel, self).__init__() | ||
self.num_in_feats = num_in_feats | ||
self.num_out_feats = num_out_feats | ||
self.num_steps = num_steps | ||
self._init_linear() | ||
|
||
@classmethod | ||
def from_cfg( | ||
cls, | ||
num_in_feats: int, | ||
num_out_feats: int, | ||
cfg: CN, | ||
) -> "AutoRegressiveModel": | ||
num_steps = cfg.LOCALSCALER.NUM_STEPS | ||
local_scaler = cls(num_in_feats, num_out_feats, num_steps) | ||
return local_scaler | ||
|
||
def _init_linear(self) -> None: | ||
self.linear = Linear(self.num_steps, 1) | ||
|
||
def forward(self, bias: Tensor) -> Tensor: | ||
bias = bias[:, -self.num_steps :] | ||
bias = bias.permute(0, 2, 1).reshape(-1, self.num_steps) | ||
bias = self.linear(bias) | ||
bias = bias.view(-1, 1, self.num_out_feats) | ||
return bias |
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,38 @@ | ||
from torch.nn import Module | ||
from tsts.cfg import CfgNode as CN | ||
from tsts.core import LOCALSCALERS | ||
|
||
__all__ = ["build_local_scaler"] | ||
|
||
|
||
def build_local_scaler( | ||
num_in_feats: int, | ||
num_out_feats: int, | ||
cfg: CN, | ||
) -> Module: | ||
"""Build local scaler. | ||
Parameters | ||
---------- | ||
num_in_feats : int | ||
Number of input features | ||
num_out_feats : int | ||
Number of output features | ||
cfg : CN | ||
Global configuration | ||
Returns | ||
------- | ||
Module | ||
Forecasting model | ||
""" | ||
local_scaler_name = cfg.LOCALSCALER.NAME | ||
cls = LOCALSCALERS[local_scaler_name] | ||
local_scaler = cls.from_cfg( | ||
num_in_feats, | ||
num_out_feats, | ||
cfg, | ||
) | ||
return local_scaler |
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,27 @@ | ||
from torch import Tensor | ||
from tsts.cfg import CfgNode as CN | ||
from tsts.core import LOCALSCALERS | ||
from tsts.models import Module | ||
|
||
__all__ = ["LastStep"] | ||
|
||
|
||
@LOCALSCALERS.register() | ||
class LastStep(Module): | ||
def __init__( | ||
self, | ||
) -> None: | ||
super(LastStep, self).__init__() | ||
|
||
@classmethod | ||
def from_cfg( | ||
cls, | ||
num_in_feats: int, | ||
num_out_feats: int, | ||
cfg: CN, | ||
) -> "LastStep": | ||
local_scaler = cls() | ||
return local_scaler | ||
|
||
def forward(self, bias: Tensor) -> Tensor: | ||
return bias[:, -1].unsqueeze(1) |
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,28 @@ | ||
import torch | ||
from torch import Tensor | ||
from tsts.cfg import CfgNode as CN | ||
from tsts.core import LOCALSCALERS | ||
from tsts.models import Module | ||
|
||
__all__ = ["NOOP"] | ||
|
||
|
||
@LOCALSCALERS.register() | ||
class NOOP(Module): | ||
def __init__( | ||
self, | ||
) -> None: | ||
super(NOOP, self).__init__() | ||
|
||
@classmethod | ||
def from_cfg( | ||
cls, | ||
num_in_feats: int, | ||
num_out_feats: int, | ||
cfg: CN, | ||
) -> "NOOP": | ||
local_scaler = cls() | ||
return local_scaler | ||
|
||
def forward(self, bias: Tensor) -> Tensor: | ||
return torch.zeros_like(bias)[:, 0:1] |
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
Oops, something went wrong.