BaseModel#

class pytorch_forecasting.models.base._base_model_v2.BaseModel(loss: Module, logging_metrics: list[Module] | None = None, optimizer: Optimizer | str | None = 'adam', optimizer_params: dict | None = None, lr_scheduler: str | None = None, lr_scheduler_params: dict | None = None)[source]#

Bases: LightningModule

Base model for time series forecasting.

Parameters:
  • loss (nn.Module) – Loss function to use for training.

  • logging_metrics (Optional[List[nn.Module]], optional) – List of metrics to log during training, validation, and testing.

  • optimizer (Optional[Union[Optimizer, str]], optional) – Optimizer to use for training. Can be a string (“adam”, “sgd”) or an instance of torch.optim.Optimizer.

  • optimizer_params (Optional[Dict], optional) – Parameters for the optimizer.

  • lr_scheduler (Optional[str], optional) – Learning rate scheduler to use. Supported values: “reduce_lr_on_plateau”, “step_lr”.

  • lr_scheduler_params (Optional[Dict], optional) – Parameters for the learning rate scheduler.

Methods

configure_optimizers()

Configure the optimizer and learning rate scheduler.

forward(x)

Forward pass of the model.

log_metrics(y_hat, y[, prefix])

Log additional metrics during training, validation, or testing.

predict_step(batch, batch_idx[, dataloader_idx])

Prediction step for the model.

test_step(batch, batch_idx)

Test step for the model.

training_step(batch, batch_idx)

Training step for the model.

validation_step(batch, batch_idx)

Validation step for the model.

configure_optimizers() dict[source]#

Configure the optimizer and learning rate scheduler.

Returns:

Dictionary containing the optimizer and scheduler configuration.

Return type:

Dict

forward(x: dict[str, Tensor]) dict[str, Tensor][source]#

Forward pass of the model.

Parameters:

x (Dict[str, torch.Tensor]) – Dictionary containing input tensors

Returns:

Dictionary containing output tensors

Return type:

Dict[str, torch.Tensor]

log_metrics(y_hat: Tensor, y: Tensor, prefix: str = 'val') None[source]#

Log additional metrics during training, validation, or testing.

Parameters:
  • y_hat (torch.Tensor) – Predicted output tensor.

  • y (torch.Tensor) – Target output tensor.

  • prefix (str) – Prefix for the logged metrics (e.g., “train”, “val”, “test”).

predict_step(batch: tuple[dict[str, Tensor]], batch_idx: int, dataloader_idx: int = 0) Tensor[source]#

Prediction step for the model.

Parameters:
  • batch (Tuple[Dict[str, torch.Tensor]]) – Batch of data containing input tensors.

  • batch_idx (int) – Index of the batch.

  • dataloader_idx (int) – Index of the dataloader.

Returns:

Predicted output tensor.

Return type:

torch.Tensor

test_step(batch: tuple[dict[str, Tensor]], batch_idx: int) Tensor | Mapping[str, Any] | None[source]#

Test step for the model.

Parameters:
  • batch (Tuple[Dict[str, torch.Tensor]]) – Batch of data containing input and target tensors.

  • batch_idx (int) – Index of the batch.

Returns:

Dictionary containing the loss and other metrics.

Return type:

STEP_OUTPUT

training_step(batch: tuple[dict[str, Tensor]], batch_idx: int) Tensor | Mapping[str, Any] | None[source]#

Training step for the model.

Parameters:
  • batch (Tuple[Dict[str, torch.Tensor]]) – Batch of data containing input and target tensors.

  • batch_idx (int) – Index of the batch.

Returns:

Dictionary containing the loss and other metrics.

Return type:

STEP_OUTPUT

validation_step(batch: tuple[dict[str, Tensor]], batch_idx: int) Tensor | Mapping[str, Any] | None[source]#

Validation step for the model.

Parameters:
  • batch (Tuple[Dict[str, torch.Tensor]]) – Batch of data containing input and target tensors.

  • batch_idx (int) – Index of the batch.

Returns:

Dictionary containing the loss and other metrics.

Return type:

STEP_OUTPUT