BaseModelWithCovariates

class pytorch_forecasting.models.base_model.BaseModelWithCovariates(log_interval: Union[int, float] = - 1, log_val_interval: Union[int, float] = None, learning_rate: Union[float, List[float]] = 0.001, log_gradient_flow: bool = False, loss: pytorch_forecasting.metrics.Metric = SMAPE(), logging_metrics: torch.nn.modules.container.ModuleList = ModuleList(), reduce_on_plateau_patience: int = 1000, weight_decay: float = 0.0, monotone_constaints: Dict[str, int] = {}, output_transformer: Callable = None, optimizer='ranger')[source]

Bases: pytorch_forecasting.models.base_model.BaseModel

Model with additional methods using covariates.

Assumes the following hyperparameters:

Parameters
  • 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_labels – dictionary mapping (string) indices to list of categorical labels

  • 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

BaseModel for timeseries forecasting from which to inherit from

Parameters
  • log_interval (Union[int, float], optional) – Batches after which predictions are logged. If < 1.0, will log multiple entries per batch. Defaults to -1.

  • log_val_interval (Union[int, float], optional) – batches after which predictions for validation are logged. Defaults to None/log_interval.

  • learning_rate (float, optional) – Learning rate. Defaults to 1e-3.

  • log_gradient_flow (bool) – If to log gradient flow, this takes time and should be only done to diagnose training failures. Defaults to False.

  • loss (Metric, optional) – metric to optimize. Defaults to SMAPE().

  • logging_metrics (nn.ModuleList[MultiHorizonMetric]) – list of metrics that are logged during training. Defaults to [].

  • reduce_on_plateau_patience (int) – patience after which learning rate is reduced by a factor of 10. Defaults to 1000

  • weight_decay (float) – weight decay. Defaults to 0.0.

  • monotone_constaints (Dict[str, int]) – dictionary of monotonicity constraints for continuous decoder variables mapping position (e.g. "0" for first position) to constraint (-1 for negative and +1 for positive, larger numbers add more weight to the constraint vs. the loss but are usually not necessary). This constraint significantly slows down training. Defaults to {}.

  • output_transformer (Callable) – transformer that takes network output and transforms it to prediction space. Defaults to None which is equivalent to lambda out: out["prediction"].

  • optimizer (str) – optimizer

Methods

calculate_prediction_actual_by_variable(x, …)

Calculate predictions and actuals by variable averaged by bins bins spanning from -std to +std

from_dataset(dataset[, …])

Create model from dataset and set parameters related to covariates.

plot_prediction_actual_by_variable(data[, …])

Plot predicions and actual averages by variables

Attributes

categorical_groups_mapping

categoricals

decoder_variables

encoder_variables

reals

static_variables

calculate_prediction_actual_by_variable(x: Dict[str, torch.Tensor], y_pred: torch.Tensor, normalize: bool = True, bins: int = 95, std: float = 2.0) → Dict[str, Dict[str, torch.Tensor]][source]

Calculate predictions and actuals by variable averaged by bins bins spanning from -std to +std

Parameters
  • x – input as forward()

  • y_pred – predictions obtained by self.transform_output(self(x, **kwargs))

  • normalize – if to return normalized averages, i.e. mean or sum of y

  • bins – number of bins to calculate

  • std – number of standard deviations for standard scaled continuous variables

Returns

dictionary that can be used to plot averages with plot_prediction_actual_by_variable()

classmethod from_dataset(dataset: pytorch_forecasting.data.timeseries.TimeSeriesDataSet, allowed_encoder_known_variable_names: List[str] = None, **kwargs) → pytorch_lightning.core.lightning.LightningModule[source]

Create model from dataset and set parameters related to covariates.

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

LightningModule

plot_prediction_actual_by_variable(data: Dict[str, Dict[str, torch.Tensor]], name: str = None, ax=None) → Union[Dict[str, matplotlib.figure.Figure], matplotlib.figure.Figure][source]

Plot predicions and actual averages by variables

Parameters
  • data (Dict[str, Dict[str, torch.Tensor]]) – data obtained from calculate_prediction_actual_by_variable()

  • name (str, optional) – name of variable for which to plot actuals vs predictions. Defaults to None which means returning a dictionary of plots for all variables.

Raises

ValueError – if the variable name is unkown

Returns

matplotlib figure

Return type

Union[Dict[str, plt.Figure], plt.Figure]