neural_lam.models.forecasters.autoregressive#
Auto-regressive unrolling of a step predictor into a full forecast.
Functions#
|
Unroll one forecast auto-regressively. |
Module Contents#
- neural_lam.models.forecasters.autoregressive.unroll_forecast(predictor: collections.abc.Callable[[torch.Tensor, torch.Tensor, torch.Tensor], tuple[torch.Tensor, torch.Tensor | None]], init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor, boundary_mask: torch.Tensor, interior_mask: torch.Tensor, gradient_checkpointing: bool = False) tuple[torch.Tensor, torch.Tensor | None][source]#
Unroll one forecast auto-regressively.
At each step
icallspredictorto produce the next state, then overwrites boundary nodes with the true value fromboundary_states[:, i].This is a function rather than a base class or a mix-in so that both forecaster families can reuse it without either inheriting from the other; see
DeterministicARForecaster.forwardandBaseProbabilisticARForecaster.forwardfor the two call sites.- Parameters:
predictor (callable) – The predictor advancing the state one time step, applied once per predicted step. A predictor that samples its output makes each call to this function an independent trajectory.
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 rollout from. Dims:Bis batch size,2initial time steps (X_{t-1}, X_t),num_grid_nodesis the number of spatial nodes, andnum_state_varsis the number of state variables.forcing_features (torch.Tensor) – Shape
(B, pred_steps, num_grid_nodes, num_forcing_vars). Forcing features for each predicted step;pred_stepsdefines the rollout length. Dims:Bis batch size,pred_stepsis the number of predicted steps,num_grid_nodesis the number of spatial nodes, andnum_forcing_varsis the number of forcing variables (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 AR step. The interior prediction at stepimust not depend onboundary_states[:, i]in any other way. Dims: same as the prediction.boundary_mask (torch.Tensor) – Shape
(1, num_grid_nodes, 1).1on boundary nodes,0elsewhere.interior_mask (torch.Tensor) – Shape
(1, num_grid_nodes, 1). The complement ofboundary_mask, passed in rather than derived so the buffer the forecaster already holds is reused.gradient_checkpointing (bool, default False) – If True, wrap each predictor call in
torch.utils.checkpointso its activations are recomputed in the backward pass instead of stored. Lowers peak memory when unrolling more than one step, at the cost of extra compute.
- Returns:
prediction (torch.Tensor) – Shape
(B, pred_steps, num_grid_nodes, num_state_vars). Stacked per-step forecasts (with boundary overwritten by the true value). Dims: same asboundary_states.pred_std (torch.Tensor or None) – Shape
(B, pred_steps, num_grid_nodes, num_state_vars)whenpredictoroutputs an std, otherwiseNone(in which case substituting a fallback std is left to whatever consumes the forecast). Dims: same asprediction.