loss

class neuralhydrology.training.loss.BaseLoss(cfg: Config, prediction_keys: List[str], ground_truth_keys: List[str], additional_data: List[str] = None, output_size_per_target: int = 1)

Bases: Module

Base loss class.

All losses extend this class by implementing _get_loss.

Parameters:
  • cfg (Config) – The run configuration.

  • prediction_keys (List[str]) – List of keys that will be predicted. During the forward pass, the passed prediction dict must contain these keys. Note that the keys listed here should be without frequency identifier.

  • ground_truth_keys (List[str]) – List of ground truth keys that will be needed to compute the loss. During the forward pass, the passed data dict must contain these keys. Note that the keys listed here should be without frequency identifier.

  • additional_data (List[str], optional) – Additional list of keys that will be taken from data in the forward pass to compute the loss. For instance, this parameter can be used to pass the variances that are needed to compute an NSE.

  • output_size_per_target (int, optional) – Number of model outputs (per element in prediction_keys) connected to a single target variable, by default 1. For example for regression, one output (last dimension in y_hat) maps to one target variable. For mixture models (e.g. GMM and CMAL) the number of outputs per target corresponds to the number of distributions (n_distributions).

forward(prediction: Dict[str, Tensor], data: Dict[str, Tensor]) Tuple[Tensor, Dict[str, Tensor]]

Calculate the loss.

Parameters:
  • prediction (Dict[str, torch.Tensor]) – Dictionary of predictions for each frequency. If more than one frequency is predicted, the keys must have suffixes _{frequency}. For the required keys, refer to the documentation of the concrete loss.

  • data (Dict[str, torch.Tensor]) – Dictionary of ground truth data for each frequency. If more than one frequency is predicted, the keys must have suffixes _{frequency}. For the required keys, refer to the documentation of the concrete loss.

Returns:

  • torch.Tensor – The overall calculated loss.

  • Dict[str, torch.Tensor] – The individual components of the loss (e.g., regularization terms). ‘total_loss’ contains the overall loss.

set_regularization_terms(regularization_modules: List[BaseRegularization])

Register the passed regularization terms to be added to the loss function.

Parameters:

regularization_modules (List[BaseRegularization]) – List of regularization functions to be added to the loss during forward.

class neuralhydrology.training.loss.MaskedCMALLoss(cfg: Config, eps: float = 1e-08)

Bases: BaseLoss

Average negative log-likelihood for a model that uses the CMAL head.

Parameters:
  • cfg (Config) – The run configuration.

  • eps (float, optional) – Small constant for numeric stability.

class neuralhydrology.training.loss.MaskedGMMLoss(cfg: Config, eps: float = 1e-10)

Bases: BaseLoss

Average negative log-likelihood for a gaussian mixture model (GMM).

This loss provides the negative log-likelihood for GMMs, which is their standard loss function. Our particular implementation is adapted from from [1].

Parameters:
  • cfg (Config) – The run configuration.

  • eps (float, optional) – Small constant for numeric stability.

References

class neuralhydrology.training.loss.MaskedMSELoss(cfg: Config)

Bases: BaseLoss

Mean squared error loss.

To use this loss in a forward pass, the passed prediction dict must contain the key y_hat, and the data dict must contain y.

Parameters:

cfg (Config) – The run configuration.

class neuralhydrology.training.loss.MaskedNSELoss(cfg: Config, eps: float = 0.1)

Bases: BaseLoss

Basin-averaged Nash–Sutcliffe Model Efficiency Coefficient loss.

To use this loss in a forward pass, the passed prediction dict must contain the key y_hat, and the data dict must contain y and per_basin_target_stds.

A description of the loss function is available in [2].

Parameters:
  • cfg (Config) – The run configuration.

  • eps (float, optional) – Small constant for numeric stability.

References

class neuralhydrology.training.loss.MaskedRMSELoss(cfg: Config)

Bases: BaseLoss

Root mean squared error loss.

To use this loss in a forward pass, the passed prediction dict must contain the key y_hat, and the data dict must contain y.

Parameters:

cfg (Config) – The run configuration.

class neuralhydrology.training.loss.MaskedUMALLoss(cfg, eps: float = 1e-05)

Bases: BaseLoss

Average negative log-likelihood for a model that uses the UMAL head.

Parameters:
  • cfg (Config) – The run configuration.

  • eps (float, optional) – Small constant for numeric stability.