0
点赞
收藏
分享

微信扫一扫

【知识建设】交叉熵损失

yundejia 2022-02-22 阅读 101

这篇相当于看了知乎答主回答后的默写:

先默写下交叉熵损失的公式:

l o g P ( y ∣ x ) = − y l o g y ^ − ( 1 − y ) l o g ( 1 − y ^ ) logP(y|x)=-ylog{\hat{y}}-(1-y)log(1-\hat{y}) logP(yx)=ylogy^(1y)log(1y^)

其中 y y y是真实值, y ^ \hat{y} y^是预测值

如何推导

首先从sigmoid函数说起:

g ( s ) = 1 1 + e − s g(s)=\frac{1}{1+e^{-s}} g(s)=1+es1
在这里插入图片描述
这个函数通常被用于神经网络的最后一层,作为输出前的最后一层,其中 s s s为倒数第二层的输出。因此 g ( s ) = P ( y ∣ x ) g(s)=P(y|x) g(s)=P(yx),其中 x x x为输入, y y y为输出

sigmoid函数的性质是: s = 0 s=0 s=0, g ( s ) = 0.5 g(s)=0.5 g(s)=0.5. s > > 0 s>>0 s>>0, g ( s ) ≈ 1 g(s)\approx1 g(s)1. s < < 0 s<<0 s<<0, g ( s ) ≈ 0 g(s)\approx0 g(s)0.

y ^ = P ( y = 1 ∣ x ) \hat{y}=P(y=1|x) y^=P(y=1x). 1 − y ^ = P ( y = 0 ∣ x ) 1-\hat{y}=P(y=0|x) 1y^=P(y=0x).

利用最大似然估计将上述两个式子合并(并不太懂是怎么合并的,在我的认知中,最大似然估计,是在已知分布表达式的情况下,通过样本和真实值反推模型参数的方法):

P ( y ∣ x ) = y ^ y ( 1 − y ^ ) 1 − y P(y|x)=\hat{y}^y(1-\hat{y})^{1-y} P(yx)=y^y(1y^)1y

同取对数得到:

l o g P ( y ∣ x ) = y l o g y ^ + ( 1 − y ) l o g ( 1 − y ^ ) logP(y|x)=ylog\hat{y}+(1-y)log(1-\hat{y}) logP(yx)=ylogy^+(1y)log(1y^)

对其取负得到:

L o s s = − y l o g y ^ − ( 1 − y ) l o g ( 1 − y ^ ) Loss=-ylog\hat{y}-(1-y)log(1-\hat{y}) Loss=ylogy^(1y)log(1y^)

分析上述式子,当 y = 1 y=1 y=1时, L o s s = − l o g y ^ Loss=-log\hat{y} Loss=logy^,函数图像如下:
在这里插入图片描述
y ^ → 1 ( = = y ) \hat{y}\to1(==y) y^1(==y), L o s s → 0 Loss\to0 Loss0
y ^ → 0 ( ! = y ) \hat{y}\to0(!=y) y^0(!=y), L o s s → ∞ Loss\to\infty Loss
y ^ → 0 \hat{y}\to0 y^0的过程更加陡峭,代表惩罚更重

同样的,当 y = 0 y=0 y=0时, L o s s = − l o g ( 1 − y ^ ) Loss=-log(1-\hat{y}) Loss=log(1y^),函数图像如下:
在这里插入图片描述
y ^ → 0 ( = = y ) \hat{y}\to0(==y) y^0(==y), L o s s → 0 Loss\to0 Loss0
y ^ → 1 ( ! = y ) \hat{y}\to1(!=y) y^1(!=y), L o s s → ∞ Loss\to\infty Loss
y ^ → 1 \hat{y}\to1 y^1的过程更加陡峭,代表惩罚更重

搞定!

举报

相关推荐

0 条评论