RecurrentNetwork#

class pytorch_forecasting.models.rnn.RecurrentNetwork(cell_type: str = 'LSTM', hidden_size: int = 10, rnn_layers: int = 2, dropout: float = 0.1, static_categoricals: List[str] = [], static_reals: List[str] = [], time_varying_categoricals_encoder: List[str] = [], time_varying_categoricals_decoder: List[str] = [], categorical_groups: Dict[str, List[str]] = {}, time_varying_reals_encoder: List[str] = [], time_varying_reals_decoder: List[str] = [], embedding_sizes: Dict[str, Tuple[int, int]] = {}, embedding_paddings: List[str] = [], embedding_labels: Dict[str, ndarray] = {}, x_reals: List[str] = [], x_categoricals: List[str] = [], output_size: Union[int, List[int]] = 1, target: Optional[Union[str, List[str]]] = None, target_lags: Dict[str, List[int]] = {}, loss: Optional[MultiHorizonMetric] = None, logging_metrics: Optional[ModuleList] = None, **kwargs)[source]#

Bases: AutoRegressiveBaseModelWithCovariates

Recurrent Network.

Simple LSTM or GRU layer followed by output layer

Parameters
  • cell_type (str, optional) – Recurrent cell type [“LSTM”, “GRU”]. Defaults to “LSTM”.

  • hidden_size (int, optional) – hidden recurrent size - the most important hyperparameter along with rnn_layers. Defaults to 10.

  • rnn_layers (int, optional) – Number of RNN layers - important hyperparameter. Defaults to 2.

  • dropout (float, optional) – Dropout in RNN layers. Defaults to 0.1.

  • static_categoricals – integer of positions of static categorical variables

  • static_reals – integer of positions of static continuous variables

  • time_varying_categoricals_encoder – integer of positions of categorical variables for encoder

  • time_varying_categoricals_decoder – integer of positions of categorical variables for decoder

  • time_varying_reals_encoder – integer of positions of continuous variables for encoder

  • time_varying_reals_decoder – integer of positions of continuous variables for decoder

  • categorical_groups – dictionary where values are list of categorical variables that are forming together a new categorical variable which is the key in the dictionary

  • x_reals – order of continuous variables in tensor passed to forward function

  • x_categoricals – order of categorical variables in tensor passed to forward function

  • embedding_sizes – dictionary mapping (string) indices to tuple of number of categorical classes and embedding size

  • embedding_paddings – list of indices for embeddings which transform the zero’s embedding to a zero vector

  • embedding_labels – dictionary mapping (string) indices to list of categorical labels

  • output_size (Union[int, List[int]], optional) – number of outputs (e.g. number of quantiles for QuantileLoss and one target or list of output sizes).

  • target (str, optional) – Target variable or list of target variables. Defaults to None.

  • target_lags (Dict[str, Dict[str, int]]) – dictionary of target names mapped to list of time steps by which the variable should be lagged. Lags can be useful to indicate seasonality to the models. If you know the seasonalit(ies) of your data, add at least the target variables with the corresponding lags to improve performance. Defaults to no lags, i.e. an empty dictionary.

  • loss (MultiHorizonMetric, optional) – loss: loss function taking prediction and targets.

  • logging_metrics (nn.ModuleList, optional) – Metrics to log during training. Defaults to nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]).

Methods

construct_input_vector(x_cat, x_cont[, ...])

Create input vector into RNN network

decode(input_vector, target_scale, ...[, ...])

Decode hidden state of RNN into prediction.

decode_all(x, hidden_state[, lengths])

encode(x)

Encode sequence into hidden state

forward(x[, n_samples])

Forward network

from_dataset(dataset[, ...])

Create model from dataset.

construct_input_vector(x_cat: Tensor, x_cont: Tensor, one_off_target: Optional[Tensor] = None) Tensor[source]#

Create input vector into RNN network

Parameters

one_off_target – tensor to insert into first position of target. If None (default), remove first time step.

decode(input_vector: Tensor, target_scale: Tensor, decoder_lengths: Tensor, hidden_state: Union[Tuple[Tensor, Tensor], Tensor], n_samples: Optional[int] = None) Tuple[Tensor, bool][source]#

Decode hidden state of RNN into prediction. If n_smaples is given, decode not by using actual values but rather by sampling new targets from past predictions iteratively

encode(x: Dict[str, Tensor]) Union[Tuple[Tensor, Tensor], Tensor][source]#

Encode sequence into hidden state

forward(x: Dict[str, Tensor], n_samples: Optional[int] = None) Dict[str, Tensor][source]#

Forward network

classmethod from_dataset(dataset: TimeSeriesDataSet, allowed_encoder_known_variable_names: Optional[List[str]] = None, **kwargs)[source]#

Create model from dataset.

Parameters
  • dataset – timeseries dataset

  • allowed_encoder_known_variable_names – List of known variables that are allowed in encoder, defaults to all

  • **kwargs – additional arguments such as hyperparameters for model (see __init__())

Returns

Recurrent network