loss函数之L1Loss与MSELoss
作者:互联网
L1Loss
平均绝对误差(MAE),用于回归模型
对于包含 N N N个样本的batch数据 D ( x , y ) D(x, y) D(x,y), l o s s loss loss计算如下:
l o s s = 1 N ∑ n = 1 N l n loss=\frac{1}{N} \sum_{n=1}^{N} l_{n} loss=N1∑n=1Nln
其中, l n = ∣ x n − y n ∣ l_{n}=\left|x_{n}-y_{n}\right| ln=∣xn−yn∣
class L1Loss(_Loss):
__constants__ = ['reduction']
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(L1Loss, self).__init__(size_average, reduce, reduction)
def forward(self, input, target):
return F.l1_loss(input, target, reduction=self.reduction)
pytorch中通过torch.nn.L1Loss
类实现,也可以直接调用F.l1_loss
函数,代码中的size_average
与reduce
已经弃用。reduction有三种取值mean
, sum
, none
,对应不同的返回
ℓ
(
x
,
y
)
\ell(x, y)
ℓ(x,y). 默认为mean
。
L = { l 1 , … , l N } L=\left\{l_{1}, \ldots, l_{N}\right\} L={l1,…,lN}
ℓ ( x , y ) = { L , if reduction = ’none’ mean ( L ) , if reduction = ’mean’ sum ( L ) , if reduction = ’sum’ \ell(x, y)=\left\{\begin{array}{ll}\operatorname L, & \text { if reduction }=\text { 'none' } \\ \operatorname{mean}(L), & \text { if reduction }=\text { 'mean' } \\ \operatorname{sum}(L), & \text { if reduction }=\text { 'sum' }\end{array} \right. ℓ(x,y)=⎩⎨⎧L,mean(L),sum(L), if reduction = ’none’ if reduction = ’mean’ if reduction = ’sum’
其中,当reduction取值mean
时,对应于上述
l
o
s
s
loss
loss的计算
MSELoss
均方误差(MSE),用于回归模型
对于包含 N N N个样本的batch数据 D ( x , y ) D(x, y) D(x,y), l o s s loss loss计算如下:
l o s s = 1 N ∑ n = 1 N l n loss=\frac{1}{N} \sum_{n=1}^{N} l_{n} loss=N1∑n=1Nln
其中, l n = ( x n − y n ) 2 l_{n}=\left(x_{n}-y_{n}\right)^{2} ln=(xn−yn)2
class MSELoss(_Loss):
def __init__(self, size_average=None, reduce=None, reduction='mean'):
super(MSELoss, self).__init__(size_average, reduce, reduction)
def forward(self, input, target):
return F.mse_loss(input, target, reduction=self.reduction)
pytorch中通过torch.nn.MSELoss
类实现,也可以直接调用F.mse_loss
函数。代码中的size_average
与reduce
已经弃用。reduction有三种取值mean
, sum
, none
,对应不同的返回
ℓ
(
x
,
y
)
\ell(x, y)
ℓ(x,y). 默认为mean
。
L = { l 1 , … , l N } L=\left\{l_{1}, \ldots, l_{N}\right\} L={l1,…,lN}
ℓ ( x , y ) = { L , if reduction = ’none’ mean ( L ) , if reduction = ’mean’ sum ( L ) , if reduction = ’sum’ \ell(x, y)=\left\{\begin{array}{ll}\operatorname L, & \text { if reduction }=\text { 'none' } \\ \operatorname{mean}(L), & \text { if reduction }=\text { 'mean' } \\ \operatorname{sum}(L), & \text { if reduction }=\text { 'sum' }\end{array} \right. ℓ(x,y)=⎩⎨⎧L,mean(L),sum(L), if reduction = ’none’ if reduction = ’mean’ if reduction = ’sum’
其中,当reduction取值mean
时,对应于上述
l
o
s
s
loss
loss的计算
标签:__,loss,MSELoss,L1Loss,text,sum,reduction,mean 来源: https://blog.csdn.net/ltochange/article/details/117914416