neural_lam.models.forecasters.probabilistic#
Forecasters that sample their forecast from a predictive distribution.
Classes#
Probabilistic forecaster sampling each forecast auto-regressively. |
|
Forecaster whose |
Module Contents#
- class neural_lam.models.forecasters.probabilistic.BaseProbabilisticARForecaster(predictor: neural_lam.models.step_predictors.base.StepPredictor, datastore: neural_lam.datastore.BaseDatastore)[source]#
Bases:
BaseProbabilisticForecasterProbabilistic forecaster sampling each forecast auto-regressively.
Each call to the wrapped predictor draws a fresh sample of the next state, so one unrolling is one member.
It supplies no training objective, and so remains abstract in
compute_training_loss. Concrete subclasses define an objective appropriate to how they are meant to be trained (e.g. an ensemble scoring rule such as CRPS, or a variational objective), along with whatever configuration that objective needs.Initialize the BaseProbabilisticARForecaster.
- Parameters:
predictor (StepPredictor) – The predictor to use for each AR step. Samples its output, so that each rollout is an independent member.
datastore (BaseDatastore) – The datastore providing grid metadata and boundary masks.
- forward(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor) tuple[torch.Tensor, torch.Tensor | None][source]#
Unroll one sampled auto-regressive forecast.
- 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 rollout from.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.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.
- Returns:
prediction (torch.Tensor) – Shape
(B, pred_steps, num_grid_nodes, num_state_vars). One sampled trajectory. Dims: same asboundary_states.pred_std (torch.Tensor or None) – Shape
(B, pred_steps, num_grid_nodes, num_state_vars)when the wrapped predictor outputs an std, otherwiseNone. Dims: same asprediction.
- predictor#
- class neural_lam.models.forecasters.probabilistic.BaseProbabilisticForecaster(datastore: neural_lam.datastore.BaseDatastore)[source]#
Bases:
neural_lam.models.forecasters.base.BaseForecasterForecaster whose
forwardsamples from a predictive distribution.forwardkeeps the signature every forecaster shares and returns one forecast, drawn afresh on each call. An ensemble is therefore not a different kind of output but repeated sampling, which is allsample_ensembledoes. How a single sample is produced (auto-regressive sampling, diffusion, …) is left to subclasses.When
forwardreturns apred_std, it is that one sample’s own predicted std, not a std describing the spread across members. Stacked bysample_ensemble, the predictive distribution is then a mixture ofSGaussians, one per member:p(x) = mean_s N(x; ensemble[:, s], per_member_std[:, s]**2), not a single Gaussian. In particular, the variance of that mixture is not the average of the per-member variances: it also includes the spread between the member means.Initialize the forecaster.
- Parameters:
datastore (BaseDatastore) – The datastore this forecaster is built for, providing grid metadata, boundary masks and standardization statistics.
- sample_ensemble(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor, num_members: int) tuple[torch.Tensor, torch.Tensor | None][source]#
Sample an ensemble of forecasts.
Calls
forwardnum_memberstimes, each drawing fresh randomness, and stacks the results along a new ensemble dimension after the batch dimension.Members are drawn sequentially, one full forecast at a time, so cost grows linearly with
num_members. They are independent given the inputs, so this is only an implementation choice: a subclass whose sampling batches cheaply can override this to fold the member dimension into the batch dimension, at proportionally higher peak memory.- 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 each member 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, identically in every member. Dims: same as one member.num_members (int) – Number of ensemble members
Sto sample.
- Returns:
ensemble (torch.Tensor) – Shape
(B, S, pred_steps, num_grid_nodes, num_state_vars). The sampled forecasts, stacked along the ensemble dimensionS.per_member_std (torch.Tensor or None) – Shape
(B, S, pred_steps, num_grid_nodes, num_state_vars). Each member’s own predicted std (see the class docstring for why the ensemble is then a mixture, not this averaged with the others), when the forecaster predicts an std, otherwiseNone. Dims: same asensemble.
- Raises:
ValueError – If
num_membersis less than 1.