Loss进化史

作为机器学习跟深度学习的三大要素之一,损失函数的定义极为讲究。不仅要连续可导,求导公式还要尽可能简洁,同时还要适应各种不同类型的问题,如回归问题一般用MSE,分类问题采用交叉熵。那我们就来看看loss的进化史,了解一下为什么要这么搭配。

1. 案例引入:政治倾向预测

希望根据一个人的年龄、性别、年收入等相互独立的特征,来预测一个人的政治倾向,有三种可预测结果:民主党、共和党、其他党。假设我们当前采用两个不同的模型分别预测,得到了三个样本属于每个类别的概率值:
模型1:

prediction label yes/or
0.3 0.3 0.4 0 0 1 (民主党) yes
0.3 0. 4 0.3 0 1 0 (共和党) yes
0.1 0.2 0.7 1 0 0 (其他党) no

模型1对于样本1和样本2以非常微弱的优势判断正确,对于样本3的判断则彻底错误。

模型2:

prediction label yes/or
0.1 0.2 0.7 0 0 1 (民主党) yes
0.1 0. 7 0.2 0 1 0 (共和党) yes
0.3 0.4 0.3 1 0 0 (其他党) no

模型2对于样本1和样本2判断更加准确,对于样本3判断错误,但是相对来说没有错得太离谱。

有了模型之后,如何定义损失函数来判断模型在样本上的表现呢?

2. 错误率(error rate)

就是识别错误的样本占总样本的比例,定义如下:

e r r o r   r a t e = n u m b e r   o f   e r r o r   s a m p l e s n u m b e r   o f   a l l   s a m p l e s error \ rate = \dfrac{number \ of \ error \ samples}{number \ of \ all \ samples} error rate=number of all samplesnumber of error samples

对于模型1: e r r o r   r a t e = 1 3 error \ rate = \dfrac{1}{3} error rate=31

对于模型2: e r r o r   r a t e = 1 3 error \ rate = \dfrac{1}{3} error rate=31

可以看到,得到的结果是一样的。虽然模型1和模型2都判别错了一个,但从概率值来看,模型2相比模型1更为贴近真实值,损失函数应该更小才对。但是错误率无法体现这个差异。

扫描二维码关注公众号,回复: 11870830 查看本文章

3. 均方差(MSE)

均方差用来衡量两个向量之间的差异,定义如下:

M S E = 1 n ∑ i n ( y i ′ − y i ) 2 MSE = \dfrac{1}{n} \sum_i ^n (y'_i-y_i)^2 MSE=n1in(yiyi)2

对于模型1,

sample 1: M S E 1 = ( 0.3 − 0 ) 2 + ( 0.3 − 0 ) 2 + ( 0.4 − 1 ) 2 = 0.54 MSE_1=(0.3-0)^2 + (0.3-0)^2 + (0.4-1)^2 = 0.54 MSE1=(0.30)2+(0.30)2+(0.41)2=0.54
sample 2: M S E 2 = ( 0.3 − 0 ) 2 + ( 0.4 − 1 ) 2 + ( 0.3 − 0 ) 2 = 0.54 MSE_2=(0.3-0)^2 + (0.4-1)^2 + (0.3-0)^2 = 0.54 MSE2=(0.30)2+(0.41)2+(0.30)2=0.54
sample 1: M S E 3 = ( 0.1 − 1 ) 2 + ( 0.2 − 0 ) 2 + ( 0.7 − 0 ) 2 = 1.32 MSE_3=(0.1-1)^2 + (0.2-0)^2 + (0.7-0)^2 = 1.32 MSE3=(0.11)2+(0.20)2+(0.70)2=1.32

对所有样本求平均:

M S E = 0.54 + 0.54 + 1.32 3 = 0.8 MSE=\dfrac{0.54 + 0.54+1.32}{3}=0.8 MSE=30.54+0.54+1.32=0.8

对于模型2,

sample 1: M S E 1 = ( 0.1 − 0 ) 2 + ( 0.2 − 0 ) 2 + ( 0.7 − 1 ) 2 = 0.138 MSE_1=(0.1-0)^2 + (0.2-0)^2 + (0.7-1)^2 = 0.138 MSE1=(0.10)2+(0.20)2+(0.71)2=0.138
sample 2: M S E 2 = ( 0.1 − 0 ) 2 + ( 0.7 − 1 ) 2 + ( 0.2 − 0 ) 2 = 0.138 MSE_2=(0.1-0)^2 + (0.7-1)^2 + (0.2-0)^2 = 0.138 MSE2=(0.10)2+(0.71)2+(0.20)2=0.138
sample 1: M S E 3 = ( 0.3 − 1 ) 2 + ( 0.4 − 0 ) 2 + ( 0.3 − 0 ) 2 = 0.72 MSE_3=(0.3-1)^2 + (0.4-0)^2 + (0.3-0)^2 = 0.72 MSE3=(0.31)2+(0.40)2+(0.30)2=0.72

对所有样本求平均:

M S E = 0.138 + 0.138 + 0.72 3 = 0.332 MSE=\dfrac{0.138 + 0.138+0.72}{3}=0.332 MSE=30.138+0.138+0.72=0.332

可以看到,模型2的MSE明显小于模型1。

4. 交叉熵(cross entropy)

用于衡量两个分布的相似度。(多分类时仅考虑正类的熵,二分类时正负类都有考虑)

(1) 对于二分类,模型最终的预测结果只有两种。对于每个样本,预测得到的概率值分别为 p 和 1-p。此时表达式为:

L = 1 N ∑ i L i = 1 N ∑ i − [ y i ⋅ l o g ( p i ) + ( 1 − y i ) ⋅ l o g ( 1 − p i ) ] L=\dfrac{1}{N} \sum_iL_i=\dfrac{1}{N} \sum_i-[y_i \cdot log(p_i) + (1-y_i) \cdot log(1-p_i)] L=N1iLi=N1i[yilog(pi)+(1yi)log(1pi)]

其中, y i y_i yi表示样本 i i i的label,属于正类为1,负类为0; p i p_i pi表示样本 i i i预测为正类的概率。

(2)对于多分类,其实际上为二分类的拓展:

L = 1 N ∑ i L i = 1 N ∑ i − ∑ c = 1 M y i c l o g ( p i c ) L=\dfrac{1}{N} \sum_iL_i=\dfrac{1}{N} \sum_i-\sum_{c=1}^My_{ic} log(p_{ic}) L=N1iLi=N1ic=1Myiclog(pic)

其中, M M M表示类别数, N N N表示样本数, y i c y_{ic} yic 表示第 i i i个样本是否属于第 c c c类。 i c _{ic} ic 表示第 i i i个样本属于第 c c c类的概率。

对于模型1,

sample 1: M S E 1 = − ( 0 ∗ l o g 0.3 + 0 ∗ l o g 0.3 + 1 ∗ l o g 0.4 ) = 0.91 MSE_1=-(0 * log0.3 + 0 * log0.3 + 1 * log0.4) = 0.91 MSE1=(0log0.3+0log0.3+1log0.4)=0.91
sample 2: M S E 2 = − ( 0 ∗ l o g 0.3 + 1 ∗ l o g 0.4 + 0 ∗ l o g 0.3 ) = 0.91 MSE_2=-(0 * log0.3 + 1 * log0.4 + 0 * log0.3) = 0.91 MSE2=(0log0.3+1log0.4+0log0.3)=0.91
sample 1: M S E 3 = − ( 1 ∗ l o g 0.1 + 0 ∗ l o g 0.2 + 0 ∗ l o g 0.7 ) = 2.30 MSE_3=-(1 * log0.1 + 0 * log0.2 + 0 * log0.7) = 2.30 MSE3=(1log0.1+0log0.2+0log0.7)=2.30

对所有样本求平均:

M S E = 0.91 + 0.91 + 2.30 3 = 1.37 MSE=\dfrac{0.91 + 0.91 + 2.30}{3}=1.37 MSE=30.91+0.91+2.30=1.37

对于模型2,

sample 1: M S E 1 = − ( 0 ∗ l o g 0.1 + 0 ∗ l o g 0.2 + 1 ∗ l o g 0.7 ) = 0.35 MSE_1=-(0 * log0.1 + 0 * log0.2 + 1 * log0.7) = 0.35 MSE1=(0log0.1+0log0.2+1log0.7)=0.35
sample 2: M S E 2 = − ( 0 ∗ l o g 0.1 + 1 ∗ l o g 0.7 + 0 ∗ l o g 0.2 ) = 0.35 MSE_2=-(0 * log0.1 + 1 * log0.7 + 0 * log0.2) = 0.35 MSE2=(0log0.1+1log0.7+0log0.2)=0.35
sample 1: M S E 3 = − ( 1 ∗ l o g 0.3 + 0 ∗ l o g 0.4 + 0 ∗ l o g 0.4 ) = 1.20 MSE_3=-(1 * log0.3 + 0 * log0.4 + 0 * log0.4) = 1.20 MSE3=(1log0.3+0log0.4+0log0.4)=1.20

对所有样本求平均:

M S E = 0.35 + 0.35 + 1.2 3 = 0.63 MSE=\dfrac{0.35 + 0.35+1.2}{3}=0.63 MSE=30.35+0.35+1.2=0.63

可以发现,交叉熵损失函数可以捕捉到模型1和模型2预测效果的差异。由于交叉熵涉及到计算每个类别的概率,所以交叉熵几乎每次都和sigmoid(或softmax)函数一起出现。
缺点:只关心对于正确标签预测概率的准确性,忽略了其他非正确标签的差异,导致学习到的特征比较散

猜你喜欢

转载自blog.csdn.net/SkullSky/article/details/108357983