mirror of
https://github.com/wassname/pytorch-ts.git
synced 2026-08-20 12:40:22 +08:00
added independent normal output
This commit is contained in:
@@ -5,6 +5,7 @@ from .distribution_output import (
|
||||
StudentTOutput,
|
||||
BetaOutput,
|
||||
NegativeBinomialOutput,
|
||||
IndependentNormalOutput,
|
||||
LowRankMultivariateNormalOutput,
|
||||
)
|
||||
from .lambda_layer import LambdaLayer
|
||||
|
||||
@@ -10,7 +10,10 @@ from torch.distributions import (
|
||||
Beta,
|
||||
NegativeBinomial,
|
||||
StudentT,
|
||||
Normal,
|
||||
Independent,
|
||||
LowRankMultivariateNormal,
|
||||
MultivariateNormal,
|
||||
TransformedDistribution,
|
||||
AffineTransform,
|
||||
)
|
||||
@@ -85,7 +88,7 @@ class DistributionOutput(Output, ABC):
|
||||
|
||||
class BetaOutput(DistributionOutput):
|
||||
args_dim: Dict[str, int] = {"concentration1": 1, "concentration0": 1}
|
||||
distr_cls: type = Beta
|
||||
distr_cls: Distribution = Beta
|
||||
|
||||
@classmethod
|
||||
def domain_map(cls, concentration1, concentration0):
|
||||
@@ -100,7 +103,6 @@ class BetaOutput(DistributionOutput):
|
||||
|
||||
class NegativeBinomialOutput(DistributionOutput):
|
||||
args_dim: Dict[str, int] = {"mu": 1, "alpha": 1}
|
||||
distr_cls: Distribution = NegativeBinomial
|
||||
|
||||
@classmethod
|
||||
def domain_map(cls, mu, alpha):
|
||||
@@ -173,3 +175,26 @@ class LowRankMultivariateNormalOutput(DistributionOutput):
|
||||
@property
|
||||
def event_shape(self) -> Tuple:
|
||||
return (self.dim,)
|
||||
|
||||
|
||||
class IndependentNormalOutput(DistributionOutput):
|
||||
def __init__(self, dim: int) -> None:
|
||||
self.dim = dim
|
||||
self.args_dim = {"loc": self.dim, "scale": self.dim}
|
||||
|
||||
def domain_map(self, loc, scale):
|
||||
return loc, F.softplus(scale)
|
||||
|
||||
@property
|
||||
def event_shape(self) -> Tuple:
|
||||
return (self.dim,)
|
||||
|
||||
def distribution(
|
||||
self, distr_args, scale: Optional[torch.Tensor] = None
|
||||
) -> Distribution:
|
||||
distr = Independent(Normal(*distr_args), 1)
|
||||
|
||||
if scale is None:
|
||||
return distr
|
||||
else:
|
||||
return TransformedDistribution(distr, [AffineTransform(loc=0, scale=scale)])
|
||||
|
||||
@@ -16,8 +16,9 @@ import pytest
|
||||
|
||||
from pts.dataset.artificial import constant_dataset
|
||||
from pts.modules import (
|
||||
# MultivariateGaussianOutput,
|
||||
IndependentNormalOutput,
|
||||
LowRankMultivariateNormalOutput,
|
||||
# MultivariateNormalOutput,
|
||||
)
|
||||
from pts.evaluation import backtest_metrics
|
||||
from pts.model.deepvar import DeepVAREstimator
|
||||
@@ -45,22 +46,28 @@ metadata = dataset.metadata
|
||||
estimator = DeepVAREstimator
|
||||
|
||||
|
||||
@pytest.mark.timeout(10)
|
||||
#@pytest.mark.timeout(10)
|
||||
@pytest.mark.parametrize(
|
||||
"distr_output, num_batches_per_epoch, Estimator, " "use_marginal_transformation",
|
||||
"distr_output, num_batches_per_epoch, Estimator, use_marginal_transformation",
|
||||
[
|
||||
(
|
||||
LowRankMultivariateNormalOutput(dim=target_dim, rank=2),
|
||||
IndependentNormalOutput(dim=target_dim),
|
||||
10,
|
||||
estimator,
|
||||
True,
|
||||
),
|
||||
(
|
||||
LowRankMultivariateNormalOutput(dim=target_dim, rank=2),
|
||||
IndependentNormalOutput(dim=target_dim),
|
||||
10,
|
||||
estimator,
|
||||
False,
|
||||
),
|
||||
(
|
||||
LowRankMultivariateNormalOutput(dim=target_dim, rank=2),
|
||||
10,
|
||||
estimator,
|
||||
True,
|
||||
),
|
||||
(
|
||||
LowRankMultivariateNormalOutput(dim=target_dim, rank=2),
|
||||
10,
|
||||
@@ -78,7 +85,7 @@ estimator = DeepVAREstimator
|
||||
# MultivariateGaussianOutput(dim=target_dim),
|
||||
# 10,
|
||||
# estimator,
|
||||
# True,
|
||||
# False,
|
||||
# ),
|
||||
],
|
||||
)
|
||||
@@ -90,10 +97,10 @@ def test_deepvar(
|
||||
input_size=44,
|
||||
num_cells=20,
|
||||
num_layers=1,
|
||||
dropout_rate=0.0,
|
||||
pick_incomplete=True,
|
||||
target_dim=target_dim,
|
||||
prediction_length=metadata.prediction_length,
|
||||
# target_dim=target_dim,
|
||||
freq=metadata.freq,
|
||||
distr_output=distr_output,
|
||||
scaling=False,
|
||||
|
||||
Reference in New Issue
Block a user