神经网络loss函数意义_lossfunction

神经网络loss函数意义_lossfunctionL1Loss平均绝对误差(MAE),用于回归模型对于包含NNN个样本的batch数据D(x,y)D(x,y)D(x,y),losslossloss计算如下:loss=1N∑n=1Nlnloss=\frac{1}{N}\sum_{n=1}^{N}l_{n}loss=N1​∑n=1N​ln​其中,ln=∣xn−yn∣l_{n}=\left|x_{n}-y_{n}\right|ln​=∣xn​−yn​∣classL1Loss(_Loss):__constants__=[‘redu

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

L1Loss

平均绝对误差(MAE),用于回归模型

对于包含 N N N个样本的batch数据 D ( x , y ) D(x, y) D(x,y) x x x为神经网络的输出, y y y是真实的得分, x x x y y y同维度。

n n n个样本的损失值 l n l_{n} ln计算如下:

l n = ∣ x n − y n ∣ l_{n}=\left|x_{n}-y_{n}\right| ln=xnyn

其中, y n y_{n} yn代表第 n n n样本的真实得分,可能对应一个值,也可能多个值,代表样本不同方面的得分,所以 l n l_{n} ln可能是一个值,也可能是一个向量。

class L1Loss(_Loss):
    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_averagereduce已经弃用。reduction有三种取值mean, sum, none,对应不同的返回 ℓ ( x , y ) \ell(x, y) (x,y)。 默认为mean,对 L L L中所有元素求平均,对应于一般情况下的 l o s s loss loss的计算。

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’ 

MSELoss

均方误差(MSE),用于回归模型

对于包含 N N N个样本的batch数据 D ( x , y ) D(x, y) D(x,y) x x x为神经网络的输出, y y y是真实的得分。

n n n个样本的损失值 l n l_{n} ln计算如下:

l n = ( x n − y n ) 2 l_{n}=\left(x_{n}-y_{n}\right)^{2} ln=(xnyn)2

其中, y n y_{n} yn代表第 n n n样本的真实得分,可能对应一个值,也可能多个值,代表样本不同方面的得分,所以 l n l_{n} ln可能是一个值,也可能是一个向量。

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_averagereduce已经弃用。reduction有三种取值mean, sum, none,对应不同的返回 ℓ ( x , y ) \ell(x, y) (x,y)。默认为mean,对 L L L中所有元素求平均,对应于一般情况下的 l o s s loss loss的计算。

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’ 

SmoothL1Loss

分段使用均方误差和平均绝对误差,用于回归模型

对于包含 N N N个样本的batch数据 D ( x , y ) D(x, y) D(x,y) x x x为神经网络的输出, y y y是真实的得分。

n n n个样本的损失值 l n l_{n} ln计算如下:

l n = { 0.5 ( x n − y n ) 2 /  beta  ,  if  ∣ x n − y n ∣ <  beta  ∣ x n − y n ∣ − 0.5 ∗  beta  ,  otherwise  l_{n}=\left\{\begin{array}{ll}0.5\left(x_{n}-y_{n}\right)^{2} / \text { beta }, & \text { if }\left|x_{n}-y_{n}\right|<\text { beta } \\ \left|x_{n}-y_{n}\right|-0.5 * \text { beta }, & \text { otherwise }\end{array}\right. ln={
0.5(xnyn)2/ beta ,xnyn0.5 beta , if xnyn< beta  otherwise 

其中, y n y_{n} yn代表第 n n n样本的真实得分,可能对应一个值,也可能多个值,代表样本不同方面的得分,所以 l n l_{n} ln可能是一个值,也可能是一个向量。

相比平均绝对误差,SmoothL1Loss平滑了 ∣ x n − y n ∣ \left|x_{n}-y_{n}\right| xnyn趋近于0时的误差。相比均方误差函数,SmoothL1Loss对离群点更不敏感。在一定程度上可以防止梯度爆炸问题。Fast R-CNN论文有详细论述。

class SmoothL1Loss(_Loss):
    def __init__(self, size_average=None, reduce=None, reduction: str = 'mean', beta: float = 1.0) -> None:
        super(SmoothL1Loss, self).__init__(size_average, reduce, reduction)
        self.beta = beta
    def forward(self, input: Tensor, target: Tensor) -> Tensor:
        return F.smooth_l1_loss(input, target, reduction=self.reduction, beta=self.beta)

pytorch中通过torch.nn.SmoothL1Loss类实现,也可以直接调用F.smooth_l1_loss 函数。代码中的size_averagereduce已经弃用。reduction有三种取值mean, sum, none,对应不同的返回 ℓ ( x , y ) \ell(x, y) (x,y)。默认为mean,对 L L L中所有元素求平均,对应于一般情况下的 l o s s loss loss的计算。

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’ 

参数 b e t a > = 0 beta>=0 beta>=0,默认为1

HuberLoss

分段使用均方误差和平均绝对误差,用于回归模型

对于包含 N N N个样本的batch数据 D ( x , y ) D(x, y) D(x,y) x x x为神经网络的输出, y y y是真实的得分。

n n n个样本的损失值 l n l_{n} ln计算如下:

l n = { 0.5 ( x n − y n ) 2 ,  if  ∣ x n − y n ∣ <  beta   beta  ∗ ( ∣ x n − y n ∣ − 0.5 ∗  beta  ) ,  otherwise  l_{n}=\left\{\begin{array}{ll}0.5\left(x_{n}-y_{n}\right)^{2}, & \text { if }\left|x_{n}-y_{n}\right|<\text { beta } \\ \text { beta }*(\left|x_{n}-y_{n}\right|-0.5 * \text { beta }), & \text { otherwise }\end{array}\right. ln={
0.5(xnyn)2, beta (xnyn0.5 beta ), if xnyn< beta  otherwise 

对比SmoothL1LossHuberLoss公式可知, HuberLoss = beta ∗ SmoothL1Loss \text {HuberLoss}= \text {beta}* \text {SmoothL1Loss} HuberLoss=betaSmoothL1Loss,两者有如下区别:

  • b e t a beta beta趋于0时,SmoothL1Loss收敛于L1Loss, HuberLoss收敛于常数0
  • b e t a beta beta趋于无穷时,SmoothL1Loss收敛于常数0,HuberLoss收敛于MSELoss
  • 随着 b e t a beta beta的变化,SmoothL1Loss中平均绝对误差段的斜率恒定为1;而HuberLos中平均绝对误差段的斜率是 b e t a beta beta

SmoothL1Loss 例子:

import torch
import torch.nn as nn
import math


def validate_loss(output, target, beta):
    val = 0
    for li_x, li_y in zip(output, target):
        for i, xy in enumerate(zip(li_x, li_y)):
            x, y = xy
            if math.fabs(x - y) < beta:
                loss_val = 0.5 * math.pow(x - y, 2) / beta
            else:
                loss_val = math.fabs(x - y) - 0.5 * beta
            val += loss_val
    return val / output.nelement()


beta = 1
loss_fct = nn.SmoothL1Loss(reduction="mean", beta=beta)
input_src = torch.Tensor([[0.8, 0.8], [0.9, 0.9], [0.3, 0.3]])
target = torch.Tensor([[0.6, 0.6], [0.7, 0.8], [0.4, 0.5]])
print(input_src.size())
print(target.size())
loss = loss_fct(input_src, target)
print(loss.item())

validate = validate_loss(input_src, target, beta)
print(validate)

loss_fct = nn.SmoothL1Loss(reduction="none", beta=beta)
loss = loss_fct(input_src, target)
print(loss)

输出结果:

torch.Size([3, 2])
torch.Size([3, 2])
0.01499999687075615
0.014999997715155441
tensor([[0.0200, 0.0200],
        [0.0200, 0.0050],
        [0.0050, 0.0200]])
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192294.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 保存和检索字符串

    保存和检索字符串

    2022年1月11日
    39
  • 南京字节跳动公司招聘_字节跳动入职期权有多少

    南京字节跳动公司招聘_字节跳动入职期权有多少1.前言相信大家对ZooKeeper应该不算陌生。但是你真的了解ZooKeeper到底有啥用不?如果别人/面试官让你给他讲讲对于ZooKeeper的认识,你能回答到什么地步呢?拿我自己来说吧!我本人曾经使用Dubbo来做分布式项目的时候,使用了ZooKeeper作为注册中心。为了保证分布式系统能够同步访问某个资源,我还使用ZooKeeper做过分布式锁。另外,我在学习Kafka的时候,知道Kafka很多功能的实现依赖了ZooKeeper。前几天,总结项目经验的时候,

    2022年10月3日
    1
  • 找jaeger_CQB初探

    找jaeger_CQB初探导读:有一天我们接到这样一条客诉“你们的收银软件最近特别慢,严重影响我们的收银效率,再不解决我们就不用了”,我相信大家应该都遇到过这种问题,即使现在没遇到,将来一定会遇到的,那遇到了怎么办呢?就这个话

    2022年8月1日
    3
  • VMM基础_MTM方法

    VMM基础_MTM方法复杂度3/5机密度3/5最后更新2021/04/20VMMVirtualMemoryManagement是所有操作系统都要解决的问题,也是非常硬件相关的问题,必须从硬件CPU的地址管理开始谈起。

    2022年9月22日
    0
  • C# 字节数组截取

    C# 字节数组截取C#字节数组截取如:byte[]bt=newbyte[]{0,1,2,3,4,5,6,7,8,9};方法一截取位数规则1)截取2位长度的字节数组用BitConverter.ToInt16例如,从第2位开始截取2个字节则BitConverter.ToInt16(bt,2);2)截取4位长度的字节数组用BitConverter.ToInt32例如…

    2022年4月30日
    461
  • Fielddata is disabled on text fields by default. Set fielddata=true on [Tag] in order to load field

    Fielddata is disabled on text fields by default. Set fielddata=true on [Tag] in order to load field

    2021年3月12日
    145

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号