pytorch 学习之 损失函数

损失函数

损失函数(loss): L o s s = f ( y ^ , y ) Loss=f(\hat{y}, y) Loss=f(y^,y) ,衡量模型输出与真实标签的差异,针对一个样本。

代价函数(cost): C o s s = 1 N ∑ i N f ( y ^ i , y i ) Coss=\frac{1}{N}\sum_i^Nf(\hat{y}_i, y_i) Coss=N1iNf(y^i,yi) ,计算整个样本集loss的平均值。

目标函数(objective): O b j = C o s t + R e g u l a r i z a t i o n Obj=Cost+Regularization Obj=Cost+Regularization ,最后模型拟合的函数。

18种损失函数
  1. nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction=‘mean’)

    nn.LogSoftnax()与nn.NLLLoss()的结合,进行交叉熵计算。

    weight:各类别的loss设置权值。

    ignore_index:忽略某个类别。

    reduction:计算模式,可为none/sum/mean

    • none:逐个元素计算
    • sum:所有元素求和,返回标量
    • mean:加权平均,返回标量

    熵:描述一个事件的不确定性,熵越小,不确定性越大。

    交叉熵衡量两个概率分布之间的差异,值越低,分布越近。

  2. nn.NLLLoss(weight=None, ignore_index=-100, reduction=‘mean’)

    实现负对数似然函数中的负号功能

    weight:各类别的loss设置权值。

    ignore_index:忽略某个类别。

    reduction:计算模式,可为none/sum/mean

    • none:逐个元素计算
    • sum:所有元素求和,返回标量
    • mean:加权平均,返回标量
  3. nn.BCELoss(weight=None, ignore_index=-100, reduction=‘mean’)

    二分类交叉熵(输入取值在[0, 1])

    weight:各类别的loss设置权值。

    ignore_index:忽略某个类别。

    reduction:计算模式,可为none/sum/mean

    • none:逐个元素计算
    • sum:所有元素求和,返回标量
    • mean:加权平均,返回标量
  4. nn.BCEWithLogitsLoss(weight=None, ignore_index=-100, reduction=‘mean’, pos_weight=None)

    结合Sigmoid与二分类交叉熵(网络最后不加sigmoid函数)

    pos_weight:正样本的权值

    weight:各类别的loss设置权值

    ignore_index:忽略某个类别

    reduction:计算模式,可为none/sum/mean

    • none:逐个元素计算
    • sum:所有元素求和,返回标量
    • mean:加权平均,返回标量
  5. nn.L1Loss(reduction=‘mean’)

    计算inputs与target之差的绝对值

  6. nn.MSELoss(reduction=‘mean’)

    计算inputs与target之差的平方

  7. nn.SmoothL1Loss(reduction=‘mean’)

    平滑的L1Loss

  8. nn.PoissonNLLLoss(log_input=True, full=False, eps=1e-08, reduction=‘mean’)

    泊松分布的负对数似然损失函数

    log_input:输入是否为对数形式,决定计算公式

    log_input=True

    • loss(input, target) = exp(input) - target*input

    log_input=False

    • loss(input, target) = input - target*log(input+eps)

    full:计算所有loss

    eps:修正项,避免log(input)为nan

  9. nn.KLDivLoss(reduction=‘mean’)

    计算KLD(divergence),KL散度,相对熵

    (需要提前将输入计算log-probabilities,如通过nn.logsoftmax())

    reduction:计算模式,可为none/sum/mean/batchmean

    • none:逐个元素计算
    • sum:所有元素求和,返回标量
    • mean:加权平均,返回标量
    • batchmean:batchsize维度求平均
  10. nn.MarginRankingLoss(margin=0.0, reduction=‘mean’)

    计算两个向量之间的相似度,通常用于排序任务

    (该方法计算两组数据之间的差异,返回一个n*n的loss矩阵)

    margin:边界值,x1与x2之间的差异值。

    reduction:计算模式,可为none/sum/mean

    loss(x, y) = max(0, -y*(x1-x2)+margin)

    • y=1时,希望x1比x2大,当x1>x2时,不产生loss
    • y=-1时,希望x1比x2小,当x1<x2时,不产生loss
  11. nn.MultiLabelMarginLoss(reduction=‘mean’)

    多标签边界损失函数

    多标签:一张图片中,有好多类别

    reduction:计算模式,可为none/sum/mean

  12. nn.SoftMarginLoss(reduction=‘mean’)

    计算二分类的logistic损失

    reduction:计算模式,可为none/sum/mean

  13. nn.MultiLabelSoftMarginLoss(weight=None, reduction=‘mean’)

    SoftMarginLoss()多标签版

    weight:各类别的loss设置权值

    reduction:计算模式,可为none/sum/mean

  14. nn.MultiMarginLoss(p = 1, margin=1.0, weight=None, reduction=‘mean’)

    计算多分类的折页损失

    p:可选1或2

    weight:各类别的loss设置权值

    margin:边界值

    reduction:计算模式,可为none/sum/mean

  15. nn.TripletMarginLoss(p = 2.0, margin=1.0, eps=1e-06, swap=False, reduction=‘mean’)

    计算三元组损失,人脸验证中常用

    p:范数的阶,默认为2

    margin:边界值

    reduction:计算模式,可为none/sum/mean

  16. nn.HingeEmbeddingLoss(margin=1.0, reduction=‘mean’)

    计算两个输入的相似性,常用于非线性embedding和半监督学习(输入x应为两个输入之差的绝对值)

    margin:边界值

    reduction:计算模式,可为none/sum/mean

  17. nn.CosineEmbeddingLoss(margin=0.0, reduction=‘mean’)

    采用余弦相似度计算两个输入的相似性

    margin:可取[-1, 1],推荐[0, 0.5]

    reduction:计算模式,可为none/sum/mean

  18. nn.CTCLoss(blank=0, zero_infinity = False, reduction=‘mean’)

    计算CTC损失,解决时序类数据的分类

    blank:blank label

    zero_infinity:无穷大的值或梯度置0

    reduction:计算模式,可为none/sum/mean

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万俟淋曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值