# DistributionLoss#

class pytorch_forecasting.metrics.base_metrics.DistributionLoss(name: str | None = None, quantiles: List[float] = [0.02, 0.1, 0.25, 0.5, 0.75, 0.9, 0.98], reduction='mean')[source]#

DistributionLoss base class.

Class should be inherited for all distribution losses, i.e. if a network predicts the parameters of a probability distribution, DistributionLoss can be used to score those parameters and calculate loss for given true values.

Define two class attributes in a child class:

distribution_class#

torch probability distribution

Type:

distributions.Distribution

distribution_arguments#

list of parameter names for the distribution

Type:

List[str]

Further, implement the methods `map_x_to_distribution()` and `rescale_parameters()`.

Initialize metric

Parameters:
• name (str) – metric name. Defaults to class name.

• quantiles (List[float], optional) – quantiles for probability range. Defaults to [0.02, 0.1, 0.25, 0.5, 0.75, 0.9, 0.98].

• reduction (str, optional) – Reduction, “none”, “mean” or “sqrt-mean”. Defaults to “mean”.

Methods

 `loss`(y_pred, y_actual) Calculate negative likelihood Map the a tensor of parameters to a probability distribution. `sample`(y_pred, n_samples) Sample from distribution. `to_prediction`(y_pred[, n_samples]) Convert network prediction into a point prediction. `to_quantiles`(y_pred[, quantiles, n_samples]) Convert network prediction into a quantile prediction.
loss(y_pred: Tensor, y_actual: Tensor) Tensor[source]#

Calculate negative likelihood

Parameters:
• y_pred – network output

• y_actual – actual values

Returns:

metric value on which backpropagation can be applied

Return type:

torch.Tensor

map_x_to_distribution(x: Tensor) Distribution[source]#

Map the a tensor of parameters to a probability distribution.

Parameters:

x (torch.Tensor) – parameters for probability distribution. Last dimension will index the parameters

Returns:

torch probability distribution as defined in the

class attribute `distribution_class`

Return type:

distributions.Distribution

sample(y_pred, n_samples: int) Tensor[source]#

Sample from distribution.

Parameters:
• y_pred – prediction output of network (shape batch_size x n_timesteps x n_paramters)

• n_samples (int) – number of samples to draw

Returns:

tensor with samples (shape batch_size x n_timesteps x n_samples)

Return type:

torch.Tensor

to_prediction(y_pred: Tensor, n_samples: int = 100) Tensor[source]#

Convert network prediction into a point prediction.

Parameters:

y_pred – prediction output of network

Returns:

mean prediction

Return type:

torch.Tensor

to_quantiles(y_pred: Tensor, quantiles: List[float] | None = None, n_samples: int = 100) Tensor[source]#

Convert network prediction into a quantile prediction.

Parameters:
• y_pred – prediction output of network

• quantiles (List[float], optional) – quantiles for probability range. Defaults to quantiles as as defined in the class initialization.

• n_samples (int) – number of samples to draw for quantiles. Defaults to 100.

Returns:

prediction quantiles (last dimension)

Return type:

torch.Tensor