MultiHorizonMetric#

class pytorch_forecasting.metrics.base_metrics.MultiHorizonMetric(reduction: str = 'mean', **kwargs)[source]#

Bases: Metric

Abstract class for defining metric for a multihorizon forecast

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

compute()

Abstract method that calcualtes metric

loss(y_pred, target)

Calculate loss without reduction.

mask_losses(losses, lengths[, reduction])

Mask losses.

reduce_loss(losses, lengths[, reduction])

Reduce loss.

update(y_pred, target)

Update method of metric that handles masking of values.

compute()[source]#

Abstract method that calcualtes metric

Should be overriden in derived classes

Parameters:
  • y_pred – network output

  • y_actual – actual values

Returns:

metric value on which backpropagation can be applied

Return type:

torch.Tensor

loss(y_pred: Dict[str, Tensor], target: Tensor) Tensor[source]#

Calculate loss without reduction. Override in derived classes

Parameters:
  • y_pred – network output

  • y_actual – actual values

Returns:

loss/metric as a single number for backpropagation

Return type:

torch.Tensor

mask_losses(losses: Tensor, lengths: Tensor, reduction: str | None = None) Tensor[source]#

Mask losses.

Parameters:
  • losses (torch.Tensor) – total loss. first dimenion are samples, second timesteps

  • lengths (torch.Tensor) – total length

  • reduction (str, optional) – type of reduction. Defaults to self.reduction.

Returns:

masked losses

Return type:

torch.Tensor

reduce_loss(losses: Tensor, lengths: Tensor, reduction: str | None = None) Tensor[source]#

Reduce loss.

Parameters:
  • losses (torch.Tensor) – total loss. first dimenion are samples, second timesteps

  • lengths (torch.Tensor) – total length

  • reduction (str, optional) – type of reduction. Defaults to self.reduction.

Returns:

reduced loss

Return type:

torch.Tensor

update(y_pred, target)[source]#

Update method of metric that handles masking of values.

Do not override this method but loss() instead

Parameters:
  • y_pred (Dict[str, torch.Tensor]) – network output

  • target (Union[torch.Tensor, rnn.PackedSequence]) – actual values

Returns:

loss as a single number for backpropagation

Return type:

torch.Tensor