常见的loss函数及pytorch代码

常见loss函数总结及pytorch代码

  • 回归损失函数
    • MAE、MSE、Smooth l1loss
    • 其他常见的损失函数

回归损失函数

MAE、MSE、Smooth l1loss

首先,是这三个函数的函数图:
常见的loss函数及pytorch代码_第1张图片
这边函数的理解部分参考博客:https://blog.csdn.net/qq_37002417/article/details/106198053

  1. MAE(mean absolute error)l1loss:
    f ( x ) = a b s ( x ) f(x)=abs(x) f(x)=abs(x)
    1.1 函数特点:(1)0处不可微,求解效率低;(2)梯度稳定,但是在loss较低时,函数的梯度保持不变,因此不利于模型收敛;(3)相比于MSE,对异常值鲁邦,从导数可以看出。(4)使用不多,多用于结构风险最小化。
    1.2 pytorch代码:

常见的loss函数及pytorch代码_第2张图片
2 MSE(Mean square loss):
f ( x ) = x 2 f(x)=x^{2} f(x)=x2
2.1 函数特点:(1)处处可微,利于求解;(2)loss较小时,梯度也会较小,因此利于模型收敛;(3)训练初期,梯度较大,容易梯度爆炸;(4)异常值对loss的影响较大,因此从而降低正常值的预测效果??这句话感觉不理解。
2.2 pytorch:与l1几乎相同。
3 Smoothl1 loss
s m o o t h L 1 ( x ) = { 0.5 x 2 , ∣ x ∣ ≤ 1 ∣ x ∣ − 0.5 , o t h e r w i s e smooth_{L_{1}}(x)=\begin{cases}0.5x^{2}, &\left |x \right |\leq 1 \cr \left |x \right|-0.5, &otherwise\end{cases} smoothL1(x)={0.5x2,x0.5,x1otherwise
3.1 函数特点:从梯度上比较完美,(1)异常值过大,不至于梯度爆炸;(2)loss较小时,梯度较小,利于模型收敛。(3)后来在目标检测中,one-stage方法存在正负样本极端不平衡问题,因此后面出现了一些针对某种问题的loss函数。

其他常见的损失函数

  1. sofrtmax-cross entropy loss:
    softmax:
    q ( x ) = e x i ∑ e x i q(x)=\frac{e^{x_{i}}}{\sum e^{x_{i}}} q(x)=exiexi
    cross entropy loss:
    l o s s = − ∑ p ( x ) l o g ( q ( x ) ) loss=-\sum p(x)log(q(x)) loss=p(x)log(q(x))
    q表示预测值,p表示真实值。
    函数特点:用预测分布去逼近真实概率分布,增加softmax的意义是防止出现数值的上下溢。
  2. 针对正负样本不平衡问题的loss函数
    2.1 focal loss函数:
    F o c a l l o s s = { − ( 1 − q ( x ) ) γ ∗ l o g ( q ( x ) ) , y i = 1 − ( q ( x ) ) γ ∗ l o g ( 1 − q ( x ) ) , o t h e r w i s e Focalloss=\begin{cases} -(1-q(x))^{\gamma}*log(q(x)),&y_{i}=1 \cr -(q(x))^{\gamma}*log(1-q(x)),&otherwise\end{cases} Focalloss={(1q(x))γlog(q(x)),(q(x))γlog(1q(x)),yi=1otherwise
    在没有加样本平衡参数时,个人觉得类似于smoothl1的作用,不同的是该函数该函数学得是概率分布,而smoothl1学的是(忘记名词。。)。
    加入了样本平衡参数的focalloss:
    F o c a l l o s s = { − a ( 1 − q ( x ) ) γ ∗ l o g ( q ( x ) ) , y i = 1 − ( 1 − a ) ( q ( x ) ) γ ∗ l o g ( 1 − q ( x ) ) , o t h e r w i s e Focalloss=\begin{cases} -a(1-q(x))^{\gamma}*log(q(x)),&y_{i}=1 \cr -(1-a)(q(x))^{\gamma}*log(1-q(x)),&otherwise\end{cases} Focalloss={a(1q(x))γlog(q(x)),(1a)(q(x))γlog(1q(x)),yi=1otherwise
    这个权重影响的是梯度,这对于正样本较少的时候,可以使loss函数以较快的速率下降,学习速度更快。
    常见的loss函数及pytorch代码_第3张图片
  3. 针对类别的loss函数:
  4. 针对boxscale pred提出的IoU loss:

你可能感兴趣的:(论文阅读,深度学习,机器学习,算法)