1. 程式人生 > >各種 loss 的瞭解 (binary/categorical crossentropy)

各種 loss 的瞭解 (binary/categorical crossentropy)

損失函式是機器學習最重要的概念之一。通過計算損失函式的大小,是學習過程中的主要依據也是學習後判斷演算法優劣的重要判據。

1.binary_crossentropy交叉熵損失函式,一般用於二分類:

                                       

這個是針對概率之間的損失函式,你會發現只有yi和ŷ i是相等時,loss才為0,否則loss就是為一個正數。而且,概率相差越大,loss就越大。這個神奇的度量概率距離的方式稱為交叉熵。

 

2.categorical_crossentropy分類交叉熵函式:

=====================================================================================================

作者:Yohanna
連結:https://www.zhihu.com/question/36307214/answer/364963552
來源:知乎
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
 

交叉熵損失函式:

交叉熵可在神經網路(機器學習)中作為損失函式。 如下公式所示:y表示真實標記的分佈,a則為訓練後的模型的預測標記分佈,交叉熵損失函式可以衡量y與a的相似性。交叉熵作為損失函式還有一個好處是使用sigmoid函式在梯度下降時能避免均方誤差損失函式學習速率降低的問題,因為學習速率可以被輸出的誤差所控制。

  1. Binary Cross Entropy

常用於二分類問題,當然也可以用於多分類問題,通常需要在網路的最後一層新增sigmoid進行配合使用,其期望輸出值(target)需要進行one hot編碼,另外BCELoss還可以用於多分類問題Multi-label classification.

定義:
For brevity, let x = output, z = target. The binary cross entropy loss is
loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i]))

對應的程式碼為:

def binary_crossentropy(t,o):
    return -(t*tf.log(o+eps) + (1.0-t)*tf.log(1.0-o+eps))

2. Categorical cross-entropy

 

p are the predictions, t are the targets, i denotes the data point and j denotes the class.

適用於多分類問題,並使用softmax作為輸出層的啟用函式的情況。

This is the loss function of choice for multi-class classification problems and softmax output units. For hard targets, i.e., targets that assign all of the probability to a single class per data point, providing a vector of int for the targets is usually slightly more efficient than providing a matrix with a single 1.0 per row.

References:

[1] Neural Networks, Manifolds, and Topology