CompositeMetric#

class pytorch_forecasting.metrics.base_metrics.CompositeMetric(metrics: List[Metric] = [], weights: List[float] | None = None)[source]#

Bases: Metric

Metric that combines multiple metrics.

Metric does not have to be called explicitly but is automatically created when adding and multiplying metrics with each other.

Example

composite_metric = SMAPE() + 0.4 * MAE()
Parameters:
  • metrics (List[LightningMetric], optional) – list of metrics to combine. Defaults to [].

  • weights (List[float], optional) – list of weights / multipliers for weights. Defaults to 1.0 for all metrics.

Methods

compute()

Get metric

forward(y_pred, y_actual, **kwargs)

Calculate composite metric

persistent([mode])

Change post-init if metric states should be saved to its state_dict.

reset()

Reset metric state variables to their default value.

to_prediction(y_pred, **kwargs)

Convert network prediction into a point prediction.

to_quantiles(y_pred, **kwargs)

Convert network prediction into a quantile prediction.

update(y_pred, y_actual, **kwargs)

Update composite metric

compute() Tensor[source]#

Get metric

Returns:

metric

Return type:

torch.Tensor

forward(y_pred: Tensor, y_actual: Tensor, **kwargs)[source]#

Calculate composite metric

Parameters:
  • y_pred – network output

  • y_actual – actual values

  • **kwargs – arguments to update function

Returns:

metric value on which backpropagation can be applied

Return type:

torch.Tensor

persistent(mode: bool = False) None[source]#

Change post-init if metric states should be saved to its state_dict.

reset() None[source]#

Reset metric state variables to their default value.

to_prediction(y_pred: Tensor, **kwargs) Tensor[source]#

Convert network prediction into a point prediction.

Will use first metric in metrics attribute to calculate result.

Parameters:
  • y_pred – prediction output of network

  • **kwargs – parameters to first metric to_prediction method

Returns:

point prediction

Return type:

torch.Tensor

to_quantiles(y_pred: Tensor, **kwargs) Tensor[source]#

Convert network prediction into a quantile prediction.

Will use first metric in metrics attribute to calculate result.

Parameters:
  • y_pred – prediction output of network

  • **kwargs – parameters to first metric’s to_quantiles() method

Returns:

prediction quantiles

Return type:

torch.Tensor

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

Update composite metric

Parameters:
  • y_pred – network output

  • y_actual – actual values

Returns:

metric value on which backpropagation can be applied

Return type:

torch.Tensor