gluonts.torch.model.deepar.module module#

class gluonts.torch.model.deepar.module.DeepARModel(freq: str, context_length: int, prediction_length: int, num_feat_dynamic_real: int = 1, num_feat_static_real: int = 1, num_feat_static_cat: int = 1, cardinality: List[int] = [1], embedding_dimension: Optional[List[int]] = None, num_layers: int = 2, hidden_size: int = 40, dropout_rate: float = 0.1, distr_output: gluonts.torch.distributions.distribution_output.DistributionOutput = gluonts.torch.distributions.studentT.StudentTOutput(beta=0.0), lags_seq: Optional[List[int]] = None, scaling: bool = True, default_scale: Optional[float] = None, num_parallel_samples: int = 100, nonnegative_pred_samples: bool = False)[source]#

Bases: torch.nn.modules.module.Module

Module implementing the DeepAR model, see [SFG17].

Note: the code of this model is unrelated to the implementation behind SageMaker’s DeepAR Forecasting Algorithm.

Parameters
  • freq – String indicating the sampling frequency of the data to be processed.

  • context_length – Length of the RNN unrolling prior to the forecast date.

  • prediction_length – Number of time points to predict.

  • num_feat_dynamic_real – Number of dynamic real features that will be provided to forward.

  • num_feat_static_real – Number of static real features that will be provided to forward.

  • num_feat_static_cat – Number of static categorical features that will be provided to forward.

  • cardinality – List of cardinalities, one for each static categorical feature.

  • embedding_dimension – Dimension of the embedding space, one for each static categorical feature.

  • num_layers – Number of layers in the RNN.

  • hidden_size – Size of the hidden layers in the RNN.

  • dropout_rate – Dropout rate to be applied at training time.

  • distr_output – Type of distribution to be output by the model at each time step

  • lags_seq – Indices of the lagged observations that the RNN takes as input. For example, [1] indicates that the RNN only takes the observation at time t-1 to produce the output for time t; instead, [1, 25] indicates that the RNN takes observations at times t-1 and t-25 as input.

  • scaling – Whether to apply mean scaling to the observations (target).

  • default_scale – Default scale that is applied if the context length window is completely unobserved. If not set, the scale in this case will be the mean scale in the batch.

  • num_parallel_samples – Number of samples to produce when unrolling the RNN in the prediction time range.

  • nonnegative_pred_samples – Should final prediction samples be non-negative? If yes, an activation function is applied to ensure non-negative. Observe that this is applied only to the final samples and this is not applied during training.

describe_inputs(batch_size=1) gluonts.model.inputs.InputSpec[source]#
forward(feat_static_cat: torch.Tensor, feat_static_real: torch.Tensor, past_time_feat: torch.Tensor, past_target: torch.Tensor, past_observed_values: torch.Tensor, future_time_feat: torch.Tensor, num_parallel_samples: Optional[int] = None) torch.Tensor[source]#

Invokes the model on input data, and produce outputs future samples.

Parameters
  • feat_static_cat – Tensor of static categorical features, shape: (batch_size, num_feat_static_cat).

  • feat_static_real – Tensor of static real features, shape: (batch_size, num_feat_static_real).

  • past_time_feat – Tensor of dynamic real features in the past, shape: (batch_size, past_length, num_feat_dynamic_real).

  • past_target – Tensor of past target values, shape: (batch_size, past_length).

  • past_observed_values – Tensor of observed values indicators, shape: (batch_size, past_length).

  • future_time_feat – (Optional) tensor of dynamic real features in the past, shape: (batch_size, prediction_length, num_feat_dynamic_real).

  • num_parallel_samples – How many future samples to produce. By default, self.num_parallel_samples is used.

log_prob(feat_static_cat: torch.Tensor, feat_static_real: torch.Tensor, past_time_feat: torch.Tensor, past_target: torch.Tensor, past_observed_values: torch.Tensor, future_time_feat: torch.Tensor, future_target: torch.Tensor) torch.Tensor[source]#
loss(feat_static_cat: torch.Tensor, feat_static_real: torch.Tensor, past_time_feat: torch.Tensor, past_target: torch.Tensor, past_observed_values: torch.Tensor, future_time_feat: torch.Tensor, future_target: torch.Tensor, future_observed_values: torch.Tensor, future_only: bool = False, aggregate_by=<built-in method mean of type object>) torch.Tensor[source]#
output_distribution(params, scale=None, trailing_n=None) torch.distributions.distribution.Distribution[source]#

Instantiate the output distribution.

Parameters
  • params – Tuple of distribution parameters.

  • scale – (Optional) scale tensor.

  • trailing_n – If set, the output distribution is created only for the last trailing_n time points.

Returns

Output distribution from the model.

Return type

torch.distributions.Distribution

post_process_samples(samples: torch.Tensor) torch.Tensor[source]#

Method to enforce domain-specific constraints on the generated samples. For example, we can enforce forecasts to be nonnegative. :param samples: Tensor of samples

Return type

Tensor of processed samples with the same shape.

prepare_rnn_input(feat_static_cat: torch.Tensor, feat_static_real: torch.Tensor, past_time_feat: torch.Tensor, past_target: torch.Tensor, past_observed_values: torch.Tensor, future_time_feat: torch.Tensor, future_target: Optional[torch.Tensor] = None) Tuple[torch.Tensor, torch.Tensor, torch.Tensor][source]#
scaler: Scaler#
training: bool#
unroll_lagged_rnn(feat_static_cat: torch.Tensor, feat_static_real: torch.Tensor, past_time_feat: torch.Tensor, past_target: torch.Tensor, past_observed_values: torch.Tensor, future_time_feat: torch.Tensor, future_target: Optional[torch.Tensor] = None) Tuple[Tuple[torch.Tensor, ...], torch.Tensor, torch.Tensor, torch.Tensor, Tuple[torch.Tensor, torch.Tensor]][source]#

Applies the underlying RNN to the provided target data and covariates.

Parameters
  • feat_static_cat – Tensor of static categorical features, shape: (batch_size, num_feat_static_cat).

  • feat_static_real – Tensor of static real features, shape: (batch_size, num_feat_static_real).

  • past_time_feat – Tensor of dynamic real features in the past, shape: (batch_size, past_length, num_feat_dynamic_real).

  • past_target – Tensor of past target values, shape: (batch_size, past_length).

  • past_observed_values – Tensor of observed values indicators, shape: (batch_size, past_length).

  • future_time_feat – Tensor of dynamic real features in the future, shape: (batch_size, prediction_length, num_feat_dynamic_real).

  • future_target – (Optional) tensor of future target values, shape: (batch_size, prediction_length).

Returns

A tuple containing, in this order: - Parameters of the output distribution - Scaling factor applied to the target - Raw output of the RNN - Static input to the RNN - Output state from the RNN

Return type

Tuple