DistributionLoss

class pytorch_forecasting.metrics.DistributionLoss(name: 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 None.

  • 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.

rescale_parameters(parameters, target_scale, …)

Rescale normalized parameters into the scale required for the distribution.

sample_n(y_pred, n_samples)

Sample from distribution.

to_prediction(y_pred)

Convert network prediction into a point prediction.

to_quantiles(y_pred)

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

rescale_parameters(parameters: torch.Tensor, target_scale: torch.Tensor, transformer: sklearn.base.BaseEstimator) → torch.Tensor[source]

Rescale normalized parameters into the scale required for the distribution.

[extended_summary]

Parameters
  • parameters (torch.Tensor) – normalized parameters (indexed by last dimension)

  • target_scale (torch.Tensor) – scale of parameters (n_batch_samples x (center, scale))

  • transformer (BaseEstimator) – original transformer that normalized the target in the first place

Returns

parameters in real/not normalized space

Return type

torch.Tensor

sample_n(y_pred, n_samples: int) → torch.Tensor[source]

Sample from distribution.

Parameters
  • y_pred – prediction output of network

  • n_samples (int) – number of samples to draw

Returns

tensor where first dimensionare 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 (with output_type = samples)

Returns

mean prediction

Return type

torch.Tensor

to_quantiles(y_pred: torch.Tensor) → torch.Tensor[source]

Convert network prediction into a quantile prediction.

Parameters

y_pred – prediction output of network (with output_type = samples)

Returns

prediction quantiles (last dimension)

Return type

torch.Tensor