DistributionLoss

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

Bases: pytorch_forecasting.metrics.MultiHorizonMetric

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_x_to_distribution(x)

Map the a tensor of parameters to a probability distribution.

sample(y_pred, n_samples)

Sample from distribution.

to_prediction(y_pred)

Convert network prediction into a point prediction.

to_quantiles(y_pred[, quantiles, n_samples])

Convert network prediction into a quantile prediction.

loss(y_pred: torch.Tensor, y_actual: torch.Tensor) torch.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: torch.Tensor) torch.distributions.distribution.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) torch.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: torch.Tensor) torch.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: torch.Tensor, quantiles: Optional[List[float]] = None, n_samples: int = 100) torch.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