Power Loss#
Module for the Power Loss class.
- class PowerLoss(p=2, reduction='mean', relative=False)[source]#
Bases:
BaseDualLossImplementation 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
reductionis 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
PowerLossclass.- 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 isFalse.
- Raises:
ValueError – If
relativeis not a boolean.ValueError – If
pis not a positive integer.
- forward(input, target)[source]#
Forward method of the loss function.
- Parameters:
input (torch.Tensor) – The input tensor.
target (torch.Tensor) – The target tensor.
- Returns:
The computed loss.
- Return type: