PowerLoss#
Module for Loss class
- class PowerLoss(p=2, reduction='mean', relative=False)[source]#
Bases:
LossInterface
The PowerLoss loss implementation class. Creates a criterion that measures the error between each element in the input \(x\) and target \(y\) powered to a specific integer.
The unreduced (i.e. with
reduction
set tonone
) loss can be described as:\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = \frac{1}{D}\left[\sum_{i=1}^{D} \left| x_n^i - y_n^i \right|^p \right],\]If
'relative'
is set to true:\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = \frac{ \sum_{i=1}^{D} | x_n^i - y_n^i|^p }{\sum_{i=1}^{D}|y_n^i|^p},\]where \(N\) is the batch size. If
reduction
is notnone
(defaultmean
), then:\[\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}\]\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(n\) elements each.
The sum operation still operates over all the elements, and divides by \(n\).
The division by \(n\) can be avoided if one sets
reduction
tosum
.- Parameters:
p (int) – Degree of Lp norm. It specifies the type of norm to be calculated. See list of possible orders in torch linalg to see the possible degrees. Default 2 (euclidean norm).
reduction (str) – Specifies the reduction to apply to the output:
none
|mean
|sum
. Whennone
: no reduction will be applied,mean
: the sum of the output will be divided by the number of elements in the output,sum
: the output will be summed.relative (bool) – Specifies if relative error should be computed.
- forward(input, target)[source]#
Forward method for loss function.
- Parameters:
input (torch.Tensor) – Input tensor from real data.
target (torch.Tensor) – Model tensor output.
- Returns:
Loss evaluation.
- Return type: