mirror of
https://github.com/wassname/pytorch-ts.git
synced 2026-07-26 13:37:40 +08:00
initial deepar
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from pts.feature.time_feature import (
|
||||
TimeFeature,
|
||||
MinuteOfHour,
|
||||
HourOfDay,
|
||||
DayOfWeek,
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
from typing import List, Optional
|
||||
|
||||
import numpy as np
|
||||
|
||||
from pts.model import PTSEstimator
|
||||
from pts.feature import TimeFeature, get_lags_for_frequency, time_features_from_frequency_str
|
||||
from pts import Trainer
|
||||
|
||||
class DeepAREstimator(PTSEstimator):
|
||||
def __init__(self,
|
||||
@@ -20,8 +26,45 @@ class DeepAREstimator(PTSEstimator):
|
||||
lags_seq: Optional[List[int]] = None,
|
||||
time_features: Optional[List[TimeFeature]] = None,
|
||||
num_parallel_samples: int = 100,
|
||||
dtype : np.dtype = np.float32,
|
||||
) -> None:
|
||||
super().__init__(trainer=trainer)
|
||||
|
||||
|
||||
|
||||
|
||||
self.freq = freq
|
||||
self.context_length = (
|
||||
context_length if context_length is not None else prediction_length
|
||||
)
|
||||
self.prediction_length = prediction_length
|
||||
self.distr_output = distr_output
|
||||
self.distr_output.dtype = dtype
|
||||
self.num_layers = num_layers
|
||||
self.num_cells = num_cells
|
||||
self.cell_type = cell_type
|
||||
self.dropout_rate = dropout_rate
|
||||
self.use_feat_dynamic_real = use_feat_dynamic_real
|
||||
self.use_feat_static_cat = use_feat_static_cat
|
||||
self.use_feat_static_real = use_feat_static_real
|
||||
self.cardinality = (
|
||||
cardinality if cardinality and use_feat_static_cat else [1]
|
||||
)
|
||||
self.embedding_dimension = (
|
||||
embedding_dimension
|
||||
if embedding_dimension is not None
|
||||
else [min(50, (cat + 1) // 2) for cat in self.cardinality]
|
||||
)
|
||||
self.scaling = scaling
|
||||
self.lags_seq = (
|
||||
lags_seq
|
||||
if lags_seq is not None
|
||||
else get_lags_for_frequency(freq_str=freq)
|
||||
)
|
||||
self.time_features = (
|
||||
time_features
|
||||
if time_features is not None
|
||||
else time_features_from_frequency_str(self.freq)
|
||||
)
|
||||
|
||||
self.history_length = self.context_length + max(self.lags_seq)
|
||||
|
||||
self.num_parallel_samples = num_parallel_samples
|
||||
@@ -12,6 +12,7 @@ import torch.nn as nn
|
||||
from .predictor import Predictor
|
||||
from .utils import get_module_forward_input_names
|
||||
|
||||
|
||||
class Estimator(ABC):
|
||||
prediction_length: int
|
||||
freq: str
|
||||
@@ -47,11 +48,9 @@ class TrainOutput(NamedTuple):
|
||||
|
||||
|
||||
class PTSEstimator(Estimator):
|
||||
def __init__(self, trainer: Trainer,
|
||||
dtype: np.dtype = np.float32) -> None:
|
||||
def __init__(self, trainer: Trainer, dtype: np.dtype = np.float32) -> None:
|
||||
self.trainer = trainer
|
||||
self.dtype = dtype
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def create_transformation(self) -> Transformation:
|
||||
|
||||
@@ -113,14 +113,15 @@ class SampleForecast(Forecast):
|
||||
info: Optional[Dict] = None,
|
||||
):
|
||||
assert isinstance(
|
||||
samples, (np.ndarray, torch.Tensor
|
||||
)), "samples should be either a numpy array or an torch tensor"
|
||||
samples,
|
||||
(np.ndarray, torch.Tensor
|
||||
)), "samples should be either a numpy array or an torch tensor"
|
||||
assert (
|
||||
len(np.shape(samples)) == 2 or len(np.shape(samples)) == 3
|
||||
), "samples should be a 2-dimensional or 3-dimensional array. Dimensions found: {}".format(
|
||||
len(np.shape(samples)))
|
||||
self.samples = (samples if (isinstance(samples, np.ndarray)) else
|
||||
samples.numpy())
|
||||
self.samples = (samples if
|
||||
(isinstance(samples, np.ndarray)) else samples.numpy())
|
||||
self._sorted_samples_value = None
|
||||
self._mean = None
|
||||
self._dim = None
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from typing import NamedTuple, Union
|
||||
import re
|
||||
|
||||
|
||||
class Quantile(NamedTuple):
|
||||
value: float
|
||||
name: str
|
||||
|
||||
@@ -2,6 +2,7 @@ import inspect
|
||||
|
||||
import torch.nn as nn
|
||||
|
||||
|
||||
def get_module_forward_input_names(module: nn.Module):
|
||||
params = inspect.signature(module.forward).parameters
|
||||
return list(params)
|
||||
|
||||
Reference in New Issue
Block a user