neural_lam.models.forecasters.base#
Base class for forecasters.
Classes#
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.ABCGeneric 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 throughforward. 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.BaseDeterministicForecasterreturns the same forecast every call;BaseProbabilisticForecastersamples a fresh one, so calling it repeatedly is what yields an ensemble (seeBaseProbabilisticForecaster.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
BaseForecastingModuleonly 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:Bis batch size,2is the time index ([X_{t-1}, X_t]),num_grid_nodesis the number of spatial nodes, andnum_state_varsis 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:Bis batch size,pred_stepsis the rollout length,num_grid_nodesis the number of spatial nodes, andnum_forcing_varsis 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.Truefor interior nodes; passed asmaskto 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:Bis batch size,2is the time index ([X_{t-1}, X_t]),num_grid_nodesis the number of spatial nodes, andnum_state_varsis 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:Bis batch size,pred_stepsis the forecast length,num_grid_nodesis the number of spatial nodes, andnum_forcing_varsis 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 onboundary_statesin any other way. Dims:Bis batch size,pred_stepsis the forecast length,num_grid_nodesis the number of spatial nodes, andnum_state_varsis 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 asboundary_states.pred_std (torch.Tensor or None) – Shape
(B, pred_steps, num_grid_nodes, num_state_vars)whenpredicts_stdis True, otherwiseNone. Per-feature predicted standard deviation; whenNone, substituting a fallback std is left to whatever consumes the forecast, not to the caller offorward. Dims: same asprediction.
- boundary_mask: torch.Tensor#
- datastore#
- interior_mask: torch.Tensor#