neural_lam.metrics#
Evaluation metrics shared across training and validation routines.
Attributes#
Functions#
|
Compute ensemble CRPS with empirical, fair or almost-fair estimation. |
|
Continuous Ranked Probability Score (CRPS) for a Gaussian predictive |
|
Get a metric function by name. |
|
(Unweighted) Mean Absolute Error. |
|
Apply a boolean mask and optionally reduce a per-entry metric tensor. |
|
(Unweighted) Mean Squared Error. |
|
Negative Log Likelihood loss for an isotropic Gaussian likelihood. |
|
Return whether a registered metric scores an ensemble of forecasts. |
|
Return whether |
|
Weighted Mean Absolute Error. |
|
Weighted Mean Squared Error. |
Module Contents#
- neural_lam.metrics.crps_ens(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor | None = None, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True, ens_dim: int = 1, *, afc_alpha: float = 1.0, chunk_size: int | None = None, chunk_dim: int = 0) torch.Tensor[source]#
Compute ensemble CRPS with empirical, fair or almost-fair estimation.
Uses sorted ranks to compute the pairwise spread term with memory linear in the number of members, with a direct two-member formula. Gradients are retained for training. The almost-fair variant follows Lang et al. (2024), AIFS: https://arxiv.org/abs/2412.15832.
- Parameters:
pred (torch.Tensor) – Target shape with an additional ensemble dimension of size
Satens_dim, normally(B, S, pred_steps, num_grid_nodes, num_state_vars). At least two ensemble members are required.target (torch.Tensor) – Shape
(..., num_grid_nodes, num_state_vars). Ground truth.pred_std (torch.Tensor or None, optional) – Unused; accepted for compatibility with the shared metric interface.
mask (torch.Tensor or None, optional) – Shape
(num_grid_nodes,). Boolean mask selecting grid nodes.average_grid (bool, optional) – Average over selected grid nodes. Default True.
sum_vars (bool, optional) – Sum over state variables. Default True.
ens_dim (int, optional) – Ensemble dimension in
pred. Default 1.afc_alpha (float, optional) – Interpolates from empirical (unfair) CRPS at 0 to fair CRPS at 1 (default). Intermediate values give almost-fair CRPS.
chunk_size (int or None, optional) – Maximum entries along chunk_dim per scoring call. None disables chunking. Smaller chunks reduce peak temporary memory; gradients remain available to callers using CRPS for training.
chunk_dim (int, optional) – Dimension in pred to chunk, excluding ens_dim. Defaults to batch dimension 0; use 2 for forecast steps in (B, S, T, N, F). Negative dimension indices are supported.
- Returns:
CRPS with the ensemble dimension removed, reduced according to
mask,average_gridandsum_varsas fornll.- Return type:
- Raises:
ValueError – If alpha, ensemble layout or chunk settings are invalid.
- neural_lam.metrics.crps_gauss(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True) torch.Tensor[source]#
Continuous Ranked Probability Score (CRPS) for a Gaussian predictive distribution (closed-form expression, negated for minimisation).
- Parameters:
pred (torch.Tensor) – Shape
(..., N, num_variables). Predicted mean.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of state variables.target (torch.Tensor) – Shape
(..., N, num_variables). Ground-truth target. Dims: same aspred.pred_std (torch.Tensor) – Shape
(..., N, num_variables)or(num_variables,). Predicted standard deviation of the Gaussian.mask (torch.Tensor or None, optional) – Shape
(N,). Boolean mask over grid nodes.Noneuses all nodes.average_grid (bool, optional) – If True, average over the grid dimension (default True).
sum_vars (bool, optional) – If True, sum over the variable dimension (default True).
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.get_metric(metric_name: str) collections.abc.Callable[Ellipsis, torch.Tensor][source]#
Get a metric function by name.
- Parameters:
metric_name (str) – Name of the metric. Must be a key in
DEFINED_METRICS.- Returns:
Function implementing the requested metric.
- Return type:
callable
- Raises:
AssertionError – If
metric_name(case-insensitive) is not a key inDEFINED_METRICS.
- neural_lam.metrics.mae(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor | None = None, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True) torch.Tensor[source]#
(Unweighted) Mean Absolute Error.
- Parameters:
pred (torch.Tensor) – Shape
(..., N, num_variables). Model prediction.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of state variables.target (torch.Tensor) – Shape
(..., N, num_variables). Ground-truth target. Dims: same aspred.pred_std (torch.Tensor or None, optional) – Unused. Accepted so that a caller holding any metric can pass one the same way; optional here, unlike in the metrics that score a distribution. Default
None.mask (torch.Tensor or None, optional) – Shape
(N,). Boolean mask over grid nodes.Noneuses all nodes.average_grid (bool, optional) – If True, average over the grid dimension (default True).
sum_vars (bool, optional) – If True, sum over the variable dimension (default True).
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.mask_and_reduce_metric(metric_entry_vals: torch.Tensor, mask: torch.Tensor | None, average_grid: bool, sum_vars: bool) torch.Tensor[source]#
Apply a boolean mask and optionally reduce a per-entry metric tensor.
- Parameters:
metric_entry_vals (torch.Tensor) – Shape
(..., N, num_variables). Per-entry metric values.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of variables in the gridded representation (e.g. state features).mask (torch.Tensor or None) – Shape
(N,). Boolean mask selecting which grid nodes to include.Nonemeans all nodes are used.average_grid (bool) – If True, average over the grid dimension
N.sum_vars (bool) – If True, sum over the variable dimension
num_variables.
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.mse(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor | None = None, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True) torch.Tensor[source]#
(Unweighted) Mean Squared Error.
- Parameters:
pred (torch.Tensor) – Shape
(..., N, num_variables). Model prediction.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of state variables.target (torch.Tensor) – Shape
(..., N, num_variables). Ground-truth target. Dims: same aspred.pred_std (torch.Tensor or None, optional) – Unused. Accepted so that a caller holding any metric can pass one the same way; optional here, unlike in the metrics that score a distribution. Default
None.mask (torch.Tensor or None, optional) – Shape
(N,). Boolean mask over grid nodes.Noneuses all nodes.average_grid (bool, optional) – If True, average over the grid dimension (default True).
sum_vars (bool, optional) – If True, sum over the variable dimension (default True).
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.nll(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True) torch.Tensor[source]#
Negative Log Likelihood loss for an isotropic Gaussian likelihood.
- Parameters:
pred (torch.Tensor) – Shape
(..., N, num_variables). Predicted mean.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of state variables.target (torch.Tensor) – Shape
(..., N, num_variables). Ground-truth target. Dims: same aspred.pred_std (torch.Tensor) – Shape
(..., N, num_variables)or(num_variables,). Predicted standard deviation of the Gaussian.mask (torch.Tensor or None, optional) – Shape
(N,). Boolean mask over grid nodes.Noneuses all nodes.average_grid (bool, optional) – If True, average over the grid dimension (default True).
sum_vars (bool, optional) – If True, sum over the variable dimension (default True).
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.requires_ensemble(metric: collections.abc.Callable[Ellipsis, torch.Tensor]) bool[source]#
Return whether a registered metric scores an ensemble of forecasts.
- Parameters:
metric (callable) – A function returned by get_metric.
- Returns:
True for registered ensemble metrics, which require predictions with shape (B, S, pred_steps, num_grid_nodes, num_state_vars).
- Return type:
- neural_lam.metrics.requires_pred_std(metric: collections.abc.Callable[Ellipsis, torch.Tensor]) bool[source]#
Return whether
metricneeds apred_stdto be computed.Lets a caller holding a metric decide whether it has to come up with a standard deviation at all, rather than assuming every metric uses one. Read off the signature, so declaring
pred_stdwithout a default is the only place a metric states that it scores a distribution.- Parameters:
metric (callable) – A metric from
DEFINED_METRICS, e.g. as returned byget_metric.- Returns:
True if
metrictakespred_stdas a required argument.- Return type:
- neural_lam.metrics.wmae(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True) torch.Tensor[source]#
Weighted Mean Absolute Error.
- Parameters:
pred (torch.Tensor) – Shape
(..., N, num_variables). Model prediction.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of state variables.target (torch.Tensor) – Shape
(..., N, num_variables). Ground-truth target. Dims: same aspred.pred_std (torch.Tensor) – Shape
(..., N, num_variables)or(num_variables,). Predicted standard deviation used as per-entry weight.mask (torch.Tensor or None, optional) – Shape
(N,). Boolean mask over grid nodes.Noneuses all nodes.average_grid (bool, optional) – If True, average over the grid dimension (default True).
sum_vars (bool, optional) – If True, sum over the variable dimension (default True).
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.wmse(pred: torch.Tensor, target: torch.Tensor, pred_std: torch.Tensor, mask: torch.Tensor | None = None, average_grid: bool = True, sum_vars: bool = True) torch.Tensor[source]#
Weighted Mean Squared Error.
- Parameters:
pred (torch.Tensor) – Shape
(..., N, num_variables). Model prediction.(...)denotes any number of broadcastable batch dimensions,Nis the number of grid nodes, andnum_variablesis the number of state variables.target (torch.Tensor) – Shape
(..., N, num_variables). Ground-truth target. Dims: same aspred.pred_std (torch.Tensor) – Shape
(..., N, num_variables)or(num_variables,). Predicted standard deviation used as per-entry weight.mask (torch.Tensor or None, optional) – Shape
(N,). Boolean mask over grid nodes.Noneuses all nodes.average_grid (bool, optional) – If True, average over the grid dimension (default True).
sum_vars (bool, optional) – If True, sum over the variable dimension (default True).
- Returns:
Reduced metric values. Shape is one of
(...,),(..., num_variables),(..., N), or(..., N, num_variables)depending onaverage_gridandsum_vars.- Return type:
- neural_lam.metrics.DEFINED_METRICS: dict[str, collections.abc.Callable[Ellipsis, torch.Tensor]]#
- neural_lam.metrics.ENSEMBLE_METRICS: dict[str, collections.abc.Callable[Ellipsis, torch.Tensor]]#
- neural_lam.metrics.crps_ens_almost_fair#
- neural_lam.metrics.crps_ens_fair#
- neural_lam.metrics.crps_ens_unfair#