这篇相当于看了知乎答主回答后的默写:
先默写下交叉熵损失的公式:
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(y∣x)=−ylogy^−(1−y)log(1−y^)
其中 y y y是真实值, y ^ \hat{y} y^是预测值
如何推导
首先从sigmoid函数说起:
    
     
      
       
        g
       
       
        (
       
       
        s
       
       
        )
       
       
        =
       
       
        
         1
        
        
         
          1
         
         
          +
         
         
          
           e
          
          
           
            −
           
           
            s
           
          
         
        
       
      
      
       g(s)=\frac{1}{1+e^{-s}}
      
     
    g(s)=1+e−s1
 
 这个函数通常被用于神经网络的最后一层,作为输出前的最后一层,其中
    
     
      
       
        s
       
      
      
       s
      
     
    s为倒数第二层的输出。因此
    
     
      
       
        g
       
       
        (
       
       
        s
       
       
        )
       
       
        =
       
       
        P
       
       
        (
       
       
        y
       
       
        ∣
       
       
        x
       
       
        )
       
      
      
       g(s)=P(y|x)
      
     
    g(s)=P(y∣x),其中
    
     
      
       
        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=1∣x). 1 − y ^ = P ( y = 0 ∣ x ) 1-\hat{y}=P(y=0|x) 1−y^=P(y=0∣x).
利用最大似然估计将上述两个式子合并(并不太懂是怎么合并的,在我的认知中,最大似然估计,是在已知分布表达式的情况下,通过样本和真实值反推模型参数的方法):
P ( y ∣ x ) = y ^ y ( 1 − y ^ ) 1 − y P(y|x)=\hat{y}^y(1-\hat{y})^{1-y} P(y∣x)=y^y(1−y^)1−y
同取对数得到:
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(y∣x)=ylogy^+(1−y)log(1−y^)
对其取负得到:
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^−(1−y)log(1−y^)
分析上述式子,当
    
     
      
       
        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
      
     
    Loss→0
 当
    
     
      
       
        
         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(1−y^),函数图像如下:
 
 当
    
     
      
       
        
         y
        
        
         ^
        
       
       
        →
       
       
        0
       
       
        (
       
       
        =
       
       
        =
       
       
        y
       
       
        )
       
      
      
       \hat{y}\to0(==y)
      
     
    y^→0(==y), 
    
     
      
       
        L
       
       
        o
       
       
        s
       
       
        s
       
       
        →
       
       
        0
       
      
      
       Loss\to0
      
     
    Loss→0
 当
    
     
      
       
        
         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的过程更加陡峭,代表惩罚更重
搞定!










