neural_lam.models.forecasters.graph_efm#
Probabilistic forecaster training a Graph-EFM predictor via its ELBO.
Classes#
Auto-regressive ensemble forecaster for Graph-EFM step predictors. |
Module Contents#
- class neural_lam.models.forecasters.graph_efm.GraphEFMForecaster(predictor: neural_lam.models.step_predictors.graph.graph_efm.BaseGraphEFM, datastore: neural_lam.datastore.BaseDatastore, config: neural_lam.config.NeuralLAMConfig | None = None, kl_beta: float = 1.0, crps_weight: float = 0.0, crps_members: int = 2)[source]#
Bases:
neural_lam.models.forecasters.probabilistic.BaseProbabilisticARForecasterAuto-regressive ensemble forecaster for Graph-EFM step predictors.
Wraps a
BaseGraphEFMpredictor (hierarchicalGraphEFMor flatGraphEFMMultiScale), a latent-variable model consisting of a conditional prior, a variational encoder and a latent decoder. Forecast sampling draws from the latent prior and decodes each step. Unrolling produces one stochastic trajectory; stacking independent trajectories gives an ensemble. Static graph embeddings are shared across members.compute_training_lossruns a variational rollout with latents drawn from the target-conditioned encoder, accumulating a reconstruction likelihood and KL term against the prior. Optional fair CRPS uses at least two separate trajectories sampled from the prior.Initialize the GraphEFMForecaster.
- Parameters:
predictor (BaseGraphEFM) – The Graph-EFM step predictor to use for each step. Each
forwardcall samples the prior and decodes; the encoder, prior and decoder sub-models are also used directly bycompute_training_lossto assemble the ELBO.datastore (BaseDatastore) – The datastore providing grid metadata and boundary masks.
config (NeuralLAMConfig or None) – Configuration used to compute the constant per-variable std substituted for
pred_stdwhenpredictordoes not output its own. Required for training when the predictor’soutput_stdis False, since the likelihood term then needs the fallback std.crps_weight (float, optional) – Weight of fair CRPS computed from independent prior trajectories. Default zero, disabling the additional sampling cost.
crps_members (int, optional) – Number of prior trajectories for CRPS. Default two.
kl_beta (float, default 1.0) – Weight of the KL term in the ELBO. When
0the prior and KL are not computed at all (pure auto-encoder training); the prior network then receives gradients only if CRPS is enabled.
- 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 ELBO training objective for one batch.
Unrolls a variational rollout over the full forecast: at each step the grid, graph and target are embedded, the latent is drawn from the encoder (variational posterior), the decoder reconstructs the next-state mean, and a reconstruction likelihood term is accumulated. When
kl_betais positive a KL term between the encoder and the prior is accumulated too. Both terms are summed over the rollout and averaged over the batch; the loss is-likelihood + kl_beta * kl + crps_weight * crps. Fair CRPS uses at least two additional prior trajectories, independent of the variational posterior rollout. It sums variables and averages over interior grid nodes, forecast steps and batch. The variational rollout advances on the predicted mean with boundary nodes overwritten by the true state.- 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. 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_windowed_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_windowed_forcing_varsincludes the concatenated past/current/future forcing windows.target_states (torch.Tensor) – Shape
(B, pred_steps, num_grid_nodes, num_state_vars). True states at each predicted step, used as the encoder conditioning and reconstruction target and to overwrite boundary nodes during the rollout. Dims: same as the prediction.interior_mask_bool (torch.Tensor) – Shape
(num_grid_nodes,), boolean.Truefor interior nodes; passed asmasktometrics.nllso only interior nodes contribute to the likelihood.
- Returns:
batch_loss (torch.Tensor) – Scalar. Negative weighted ELBO plus optional fair CRPS.
loss_components (dict of {str: torch.Tensor}) – Scalar ELBO diagnostics to log:
"elbo_likelihood"always, plus"elbo_kl"and"elbo"whenkl_beta > 0, and"crps"whencrps_weight > 0.
- Raises:
ValueError – If the predictor does not output its own std and no
per_var_stdfallback is available (this forecaster was constructed withoutconfig).
- sample_ensemble(init_states: torch.Tensor, forcing_features: torch.Tensor, boundary_states: torch.Tensor, num_members: int, graph_emb: dict[str, Any] | None = None) tuple[torch.Tensor, torch.Tensor | None][source]#
Sample independent prior trajectories using one graph embedding.
- Parameters:
init_states (torch.Tensor) – Shape (B, 2, num_grid_nodes, num_state_vars). Initial states.
forcing_features (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_windowed_forcing_vars).
boundary_states (torch.Tensor) – Shape (B, pred_steps, num_grid_nodes, num_state_vars). Boundaries.
num_members (int) – Number of independent prior trajectories, at least one.
graph_emb (dict[str, Any] or None, optional) – Static graph embeddings shared with the variational rollout. Computed once for all trajectories when omitted.
- Returns:
ensemble (torch.Tensor) – Shape (B, num_members, pred_steps, num_grid_nodes, num_state_vars).
per_member_std (torch.Tensor or None) – Conditional observation scales with the same shape, if enabled.
- Raises:
ValueError – If num_members is less than one.
- crps_members = 2#
- crps_weight = 0.0#
- kl_beta = 1.0#
- per_var_std: torch.Tensor | None#