Pytorch 教程 3-损失函数

三、损失函数Loss


3.1 L1Loss

  • torch.nn.L1Loss(size_average=True, reduce=True)
    • 计算output和target之差的绝对值,可选返回同维度的tensor或者是一个标量

    • 计算公式

      loss ⁡ ( x , y ) = 1 N ∑ i = 1 N ∣ x − y ∣ \operatorname{loss}(\mathbf{x},\mathbf{y})=\dfrac{1}{N}\sum_{i=1}^{N}|\mathbf{x}-\mathbf{y}| loss(x,y)=N1i=1Nxy

    • 参数

      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.2 MSELoss

  • torch.nn.MSELoss(size_average=None, reduce=None)
    • 计算output和target之差的平方,可选返回同维度的tensor或者是一个标量

    • 计算公式

      loss ⁡ ( x , y ) = 1 N ∑ i = 1 N ∣ x − y ∣ 2 \operatorname{loss}(\mathbf x,\mathbf y)=\dfrac1N\sum_{i=1}^N\left|\mathbf x-\mathbf y\right|^2 loss(x,y)=N1i=1Nxy2

    • 参数

      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.3 CrossEntropyLoss

  • torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=None, reduce=None, reduction='elementwise_mean')
    • 将输入经过softmax激活函数之后,再计算其与target的交叉熵损失。即该方法将nn.LogSoftmax()nn.NLLLoss()进行了结合。严格意义上的交叉熵损失函数应该是nn.NLLLoss()

    • 计算公式

      loss ⁡ ( x , c l a s s ) = − log ⁡ ( exp ⁡ ( x [ c l a s s ] ) ∑ j exp ⁡ ( x [ j ] ) ) = − x [ c l a s s ] + log ⁡ ( ∑ j exp ⁡ ( x [ j ] ) ) \operatorname{loss}(x,class)=-\log\left(\dfrac{\exp(x[class])}{\sum_{j}\exp(x[j])}\right)=-x[class]+\log\left(\sum_{j}\exp(x[j])\right) loss(x,class)=log(jexp(x[j])exp(x[class]))=x[class]+log(jexp(x[j]))

    • 参数

      • weight(Tensor):为每个类别的loss设置权值,常用于类别不均衡问题,weight必须是float类型的tensor
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True
      • ignore_index(int):忽略某一类别,不计算其loss,其loss为0,并且,在采用size_average时,不会计算那一类的 loss,除的时候的分母也不会统计那一类的样本。

3.4 NLLLoss

  • torch.nn.NLLLoss(weight=None, size_average=None, ignore_index=None, reduce=None, reduction='elementwise_mean')
    • 计算公式

      loss ⁡ ( x , l a b e l ) = − x l a b e l \operatorname{loss}(\mathbf{x},\mathrm{label})=-\mathbf{x}_{\mathrm{label}} loss(x,label)=xlabel

    • 参数

      • weight(Tensor):为每个类别的loss设置权值,常用于类别不均衡问题,weight必须是float类型的tensor
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True
      • ignore_index(int):忽略某一类别,不计算其loss,其loss为0,并且,在采用size_average时,不会计算那一类的 loss,除的时候的分母也不会统计那一类的样本。

3.5 PoissonNLLLoss

  • torch.nn.PoissonNLLLoss(log_input=True, full=False, size_average=None, eps=1e-8, reduce=None, reduction='elementwise_mean')
    • 用于target服从泊松公布的分类任务

    • 计算公式

      t a r g e t ∼ P o s s i o n ( i n p u t ) l o s s ( i n p u t , t a r g e t ) = i n p u t − t a r g e t ∗ l n ( i n p u t ) + l n ( t a r g e t ! ) target\sim Possion(input)\\ loss(input,target) = input - target*ln(input)+ln(target!) targetPossion(input)loss(input,target)=inputtargetln(input)+ln(target!)

    • 参数

      • log_input(bool):为True时候,计算公式为: l o s s ( i n p u t , t a r g e t ) = e x p ( i n p u t ) − t a r g e t ∗ i n p u t loss(input, target)=exp(input)-target*input loss(input,target)=exp(input)targetinput,为False时, l o s s ( i n p u t , t a r g e t ) = i n p u t − t a r g e t ∗ l o g ( i n p u t + e p s ) loss(input,target)=input-target*log(input+eps) loss(input,target)=inputtargetlog(input+eps)
      • full(bool):是否计算全部的los
      • eps(float):当log_input=False时,用来防止计算log(0),而增加的一个修正项
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.6 KLDivLose

  • torch.nn.KLDivLoss(size_average=None, reduce=None, reduction='elementwise_mean')
    • 计算input和target之间的KL散度
    • 参数
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值,平均值为element-wise的,而不是针对样本的平均;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.7 BCELoss

  • torch.nn.BCELoss(weight=None, size_average=None, reduce=None, reduce=None, reduction='elementwise_mean')
    • 二分类任务时的交叉熵计算函数。在自编码器中常用

    • 计算公式

      ℓ ( x , y ) = L = { l 1 , … , l N } ⊤ , l n = − w n [ y n ⋅ log ⁡ x n + ( 1 − y n ) ⋅ log ⁡ ( 1 − x n ) ] \ell(x,y)=L=\{l_1,\dots,l_N\}^\top,l_n=-w_n\left[y_n\cdot\log x_n+(1-y_n)\cdot\log(1-x_n)\right] (x,y)=L={l1,,lN},ln=wn[ynlogxn+(1yn)log(1xn)]

    • 参数

      • weight(Tensor):为每个类别的loss设置权值,常用于类别不均衡问题,weight必须是float类型的tensor
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.8 BCEWithLogitsLoss

  • torch.nn.BCEWithLogitsLoss(weight=None, size_average=None, reduce=None, reduction='elementwise_mean', pos_weight=None)
    • 将Sigmoid和BCELoss结合,input经过Sigmoid激活函数后,将变成概率分布的形式

    • 计算公式

      ℓ ( x , y ) = L = { l 1 , … , l N } T , l n = − w n [ t n ⋅ log ⁡ σ ( x n ) + ( 1 − t n ) ⋅ log ⁡ ( 1 − σ ( x n ) ) ] σ ( ) 表示 S i g m o i d 函数 \ell(x,y)=L=\{l_1,\ldots,l_N\}^{\text{T}},\quad l_n=-w_n\left[t_n\cdot\log\sigma(x_n)+(1-t_n)\cdot\log(1-\sigma(x_n))\right]\\ \sigma()表示Sigmoid函数 (x,y)=L={l1,,lN}T,ln=wn[tnlogσ(xn)+(1tn)log(1σ(xn))]σ()表示Sigmoid函数

    • 参数

      • weight(Tensor):为batch中单个样本设置权值
      • pos_weight:正样本的权重,当 p > 1 p>1 p>1,提高召回率,当 p < 1 p<1 p<1,提高精确度,可达到权衡召回率和精确度的作用,必须为一个与类别数等长的向量
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.9 MarginRankingLoss

  • torch.nn.MarginRankingLoss(margin=0, size_average=None, reduce=None, reduction='elementwise_mean')
    • 计算两个向量之间的相似度,当两个向量之间的距离大于margin,则loss为正,小于margin,loss为0

    • 计算公式

      l o s s ( x , y ) = m a x ( 0 , − y ∗ ( x 1 − x 2 ) + m a r g i n ) loss(x,y)=max(0,-y*(x_1-x_2)+margin) loss(x,y)=max(0,y(x1x2)+margin)

    • 参数

      • maegin(float) x 1 x_1 x1 x 2 x_2 x2之间的差异
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.10 HingeEmbeddingLoss

  • torch.nn.HingeEmbeddingLoss(margin=1.0, size_average=None, reduce=None, reduction='elementwise_mean')
    • 为折页损失的拓展,主要用于衡量两个输入是否相似

    • 计算公式

      l n = { x n , if y n = 1 , max ⁡ { 0 , Δ − x n } , if y n = − 1 , l_n=\left\{\begin{array}{ll}x_n,&\textrm{if}y_n=1,\\ \max\{0,\Delta-x_n\},&\textrm{if}{y_n=-1},\end{array}\right. ln={xn,max{0,Δxn},ifyn=1,ifyn=1,

    • 参数

      • maegin(float):默认为1,容忍的差距
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.11 MultiLabelMarginLoss

  • torch.nn.MultiLabelMarginLoss(size_average=None, reduce=None, reduction='elementwise_mean')
    • 用于一个样本属于多个类别时的分类任务

3.12 SmoothL1Loss

  • torch.nn.SmoothL1Loss(size_average=None, reduce=None, reduction='elementwise_mean')
    • 计算平滑L1损失

    • 计算公式

      l o s s ( x , y ) = 1 n ∑ i z i where  z i  is given by: z i = { 0.5 ( x i − y i ) 2 , if  ∣ x i − y i ∣ < 1 ∣ x i − y j ∣ − 0.5 , otherwise \quad loss(x,y)=\dfrac{1}{n}\sum_i z_i\\\text{where }z_i\text{ is given}\text{ by:}\\ \quad z_i=\left\{\begin{array}{ll}0.5(x_i-y_i)^2,&\text{if }|x_i-y_i|<1\\ |x_i-y_j|-0.5,&\text{otherwise}\end{array}\right. loss(x,y)=n1iziwhere zi is given by:zi={0.5(xiyi)2,xiyj0.5,if xiyi<1otherwise

    • 参数

      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.13 SoftMarginLoss

  • torch.nn.SoftMarginLoss(size_average=None, reduce=None, reduction='elementwise_mean')
    • 参数
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.14 MultiLabelSoftMarginLoss

  • torch.nn.MultiLabelSoftMarginLoss(weight=None, size_average=None, reduce=None, reduction='elementwise_mean')
    • SoftMarginLoss的多标签版本
    • 参数
      • weight(Tensor):为每个类别的loss设置权值,常用于类别不均衡问题,weight必须是float类型的tensor
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.15 CosineEmbeddingLoss

  • torch.nn.CosineEmbeddingLoss(margin=0, size_average=None, reduce=None, reduction='elementwise_mean')
    • 用cosine函数来衡量两个输入是否相似

    • 计算公式

      loss ⁡ ( x , y ) = { 1 − cos ⁡ ( x 1 , x 2 ) , if  y = = 1 max ⁡ ( 0 , cos ⁡ ( x 1 , x 2 ) − margin ⁡ ) , if  y = = − 1 \operatorname{loss}(x,y)=\left\{\begin{array}{ll}1-\cos(x_1,x_2),&\text{if }y==1\\ \operatorname*{max}(0,\cos(x_{1},x_2)-\operatorname{margin}),&\textrm{if }y==-1\end{array}\right. loss(x,y)={1cos(x1,x2),max(0,cos(x1,x2)margin),if y==1if y==1

    • 参数

      • margin(float):取值范围 [ − 1 , 1 ] [-1,1] [1,1],推荐设置范围 [ 0 , 0.5 ] [0,0.5] [0,0.5]
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

3.16 MultiMarginLoss

  • torch.nn.MultiMarginLoss(p=1, margin=1, weight=None, size_average=None, reduce=None, reduction='elementwise_mean')
    • 计算多分类的折页损失

3.17 TripletMarginLoss

  • torch.nn.TripletMarginLoss(margin=1.0, p=2, eps=1e06, swap=False, size_average=None, reduce=None, eduction='elementwise_mean')
    • 计算三元组损失,人脸验证中常用

    • 计算公式

      L ( a , p , n ) = max ⁡ { d ( a i , p i ) − d ( a i , n i ) + m a r g i n , 0 } where  d ( x i , y i ) = ∥ x i − y i ∥ D . L(a,p,n)=\max\{d(a_i,p_i)-d(a_i,n_i)+margin,0\}\\ \text{where }d(x_i,y_i)=\|\mathbf{x}_i-\mathbf{y}_i\|_D. L(a,p,n)=max{d(ai,pi)d(ai,ni)+margin,0}where d(xi,yi)=xiyiD.

    • 参数

      • margin(float):默认值为 1
      • p(int):The norm degree ,默认值为 2
      • size_average(bool):当reduce=True有效时,为True,返回的loss为平均值;为False,返回的各样本的loss之和
      • reduce(bool):返回值是否为标量,默认为True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值