neural_lam.models.forecasters.base#

Base class for forecasters.

Classes#

BaseForecaster

Generic forecaster capable of mapping from a set of initial states,

Module Contents#

class neural_lam.models.forecasters.base.BaseForecaster(datastore: neural_lam.datastore.BaseDatastore)[source]#

Bases: torch.nn.Module, abc.ABC

Generic forecaster capable of mapping from a set of initial states, forcing and forces and previous states into a full forecast of the requested length.

A forecaster owns its complete training objective (compute_training_loss) and produces forecasts through forward. Every forecaster shares that one entry point and its signature: one call produces one forecast. What differs between families is how that forecast is arrived at, not its shape. BaseDeterministicForecaster returns the same forecast every call; BaseProbabilisticForecaster samples a fresh one, so calling it repeatedly is what yields an ensemble (see BaseProbabilisticForecaster.sample_ensemble).

Initialize the forecaster.

Parameters:

datastore (BaseDatastore) – The datastore this forecaster is built for, providing grid metadata, boundary masks and standardization statistics.

abstract compute_training_loss(init_states: torch.Tensor, forcing_features: torch.Tensor, target_states: torch.Tensor, interior_mask_bool: torch.Tensor) → tuple[torch.Tensor, dict[str, torch.Tensor]][source]#

Compute the training objective for one batch.

The forecaster owns its complete training objective: which forecasts to produce from the batch, which loss terms to compute from them and how to combine those terms into a single scalar. The wrapping BaseForecastingModule only injects the interior mask, logs the returned components and optimizes the returned loss.

Parameters:
  • init_states (torch.Tensor) – Shape (B, 2, num_grid_nodes, num_state_vars). The two initial states [X_{t-1}, X_t] used to start the forecast from. Dims: B is batch size, 2 is the time index ([X_{t-1}, X_t]), num_grid_nodes is the number of spatial nodes, and num_state_vars is the state feature dimension.

  • forcing_features (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_forcing_vars). External forcings provided at each predicted step. Dims: B is batch size, pred_steps is the rollout length, num_grid_nodes is the number of spatial nodes, and num_forcing_vars is the forcing feature dimension (already concatenated past/current/future windows).

  • target_states (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). True states at each predicted step, used both as the prediction targets and to overwrite boundary nodes during forecasting. Dims: same as the prediction.

  • interior_mask_bool (torch.Tensor) – Shape (num_grid_nodes,), boolean. True for interior nodes; passed as mask to the scoring rule so that only interior nodes are scored.

Returns:

  • batch_loss (torch.Tensor) – Scalar. The full training loss for the batch, to take gradients of.

  • loss_components (dict of {str: torch.Tensor}) – Scalar loss-related quantities to log alongside the loss, keyed by component name. The wrapping module prefixes the names with the training phase. Empty when the objective has no separate components worth logging.

abstract forward(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor) → tuple[torch.Tensor, torch.Tensor | None][source]#

Produce one forecast of length pred_steps.

Every forecaster produces a forecast the same way, whether or not it is stochastic: a stochastic one draws a fresh sample per call, so an ensemble is several calls rather than a different signature.

Parameters:
  • init_states (torch.Tensor) – Shape (B, 2, num_grid_nodes, num_state_vars). The two initial states [X_{t-1}, X_t] used to start the forecast from. Dims: B is batch size, 2 is the time index ([X_{t-1}, X_t]), num_grid_nodes is the number of spatial nodes, and num_state_vars is the state feature dimension.

  • forcing_features (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_forcing_vars). External forcings provided at each predicted step. Dims: B is batch size, pred_steps is the forecast length, num_grid_nodes is the number of spatial nodes, and num_forcing_vars is the forcing feature dimension (already concatenated past/current/future windows).

  • boundary_states (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). True state values used ONLY to overwrite boundary nodes at each predicted step; interior predictions must not depend on boundary_states in any other way. Dims: B is batch size, pred_steps is the forecast length, num_grid_nodes is the number of spatial nodes, and num_state_vars is the state feature dimension. This is a temporary mechanism that mirrors the pre-refactor ARModel behavior; it will be replaced by a dedicated boundary-forcing input in #138 (training on interior + boundary datastore), at which point this parameter will be removed.

Returns:

  • prediction (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). Forecast of state at each predicted step. Dims: same as boundary_states.

  • pred_std (torch.Tensor or None) – Shape (B, pred_steps, num_grid_nodes, num_state_vars) when predicts_std is True, otherwise None. Per-feature predicted standard deviation; when None, substituting a fallback std is left to whatever consumes the forecast, not to the caller of forward. Dims: same as prediction.

boundary_mask: torch.Tensor#
datastore#
interior_mask: torch.Tensor#
property predicts_std: bool#
Abstractmethod:

Whether this forecaster outputs a predicted standard deviation.

Returns:

True if the forecaster predicts standard deviation, False otherwise.

Return type:

bool