Lp Loss#
Module for the Lp Loss class.
- class LpLoss(p=2, reduction='mean', relative=False)[source]#
Bases:
BaseDualLossImplementation of the \(L^p\) loss measuring the pointwise \(L^p\) distance 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 = \left( \sum_{i=1}^{D} \left| x_n^i - y_n^i \right|^p \right)^{1/p}\]If
relative=True, each term is normalized by the \(L^p\) norm of the input tensor \(x\):\[l_n = \frac{\left( \sum_{i=1}^{D} |x_n^i - y_n^i|^p \right)^{1/p}} {\left( \sum_{i=1}^{D} |x_n^i|^p \right)^{1/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
LpLossclass.- Parameters:
p (int | float | str) – The order of the norm. It can be a numeric value for standard p-norms or one of the following strings:
"inf"for maximum absolute value,"-inf"for minimum absolute value. The values"inf"and"-inf"are internally converted to their floating counterparts. Default is2.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 valid norm order.
- 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: