Metric#

class pytorch_forecasting.metrics.base_metrics.Metric(name: str | None = None, quantiles: List[float] | None = None, reduction='mean', **kwargs)[source]#

Bases: Metric

Base metric class that has basic functions that can handle predicting quantiles and operate in log space. See the Lightning documentation for details of how to implement a new metric

Other metrics should inherit from this base class

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

extra_repr()

Set the extra representation of the module

rescale_parameters(parameters, target_scale, ...)

Rescale normalized parameters into the scale required for the output.

to_prediction(y_pred)

Convert network prediction into a point prediction.

to_quantiles(y_pred[, quantiles])

Convert network prediction into a quantile prediction.

update(y_pred, y_actual)

Override this method to update the state variables of your metric class.

compute() Tensor[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

extra_repr() str[source]#

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

rescale_parameters(parameters: Tensor, target_scale: Tensor, encoder: BaseEstimator) Tensor[source]#

Rescale normalized parameters into the scale required for the output.

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

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

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

Returns:

parameters in real/not normalized space

Return type:

torch.Tensor

to_prediction(y_pred: Tensor) Tensor[source]#

Convert network prediction into a point prediction.

Parameters:

y_pred – prediction output of network

Returns:

point prediction

Return type:

torch.Tensor

to_quantiles(y_pred: Tensor, quantiles: List[float] | None = None) 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.

Returns:

prediction quantiles

Return type:

torch.Tensor

update(y_pred: Tensor, y_actual: Tensor)[source]#

Override this method to update the state variables of your metric class.