Power Loss#

Module for the Power Loss class.

class PowerLoss(p=2, reduction='mean', relative=False)[source]#

Bases: BaseDualLoss

Implementation of the Power loss, measuring the pointwise averaged \(p\)-power error between an input tensor \(x\) and a target tensor \(y\).

Given a batch of size \(N\) and feature dimension \(D\), the unreduced loss (reduction="none") is defined as:

\[L = \{l_1, \dots, l_N\}^\top, \quad l_n = \frac{1}{D} \sum_{i=1}^{D} \left| x_n^i - y_n^i \right|^p\]

If relative=True, each term is normalized by the averaged \(p\)-power magnitude of the input tensor \(x\):

\[l_n = \frac{\frac{1}{D} \sum_{i=1}^{D} |x_n^i - y_n^i|^p} {\frac{1}{D} \sum_{i=1}^{D} |x_n^i|^p}\]

If reduction is set to "mean" or "sum", the vector \(L\) is aggregated accordingly:

\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{``mean''} \\ \operatorname{sum}(L), & \text{if reduction} = \text{``sum''} \end{cases}\end{split}\]

where \(N\) is the batch size.

Initialization of the PowerLoss class.

Parameters:
  • p (int) – The order of the p-norm. Default is 2.

  • reduction (str) – The reduction method to aggregate pointwise loss values. Available options include: "none" for unreduced loss, "mean" for the average of the loss values, and "sum" for their total sum. Default is "mean".

  • relative (bool) – If True, computes the relative error. Default is False.

Raises:
forward(input, target)[source]#

Forward method of the loss function.

Parameters:
Returns:

The computed loss.

Return type:

torch.Tensor