交叉熵(Cross Entropy)是Loss函數的一種(也稱為損失函數或代價函數),用於描述模型預測值與真實值的差距大小,常見的Loss函數就是均方平方差(Mean Squared Error)
交叉熵的原理
交叉熵刻畫的是實際輸出(概率)與期望輸出(概率)的距離,也就是交叉熵的值越小,兩個概率分佈就越接近。假設概率分佈p為期望輸出,概率分佈q為實際輸出,H(p,q)為交叉熵,則:
這個公式如何表徵距離呢,舉個例子:假設N=3,期望輸出為p=(1,0,0),實際輸出q1=(0.5,0.2,0.3),q2=(0.8,0.1,0.1),那麼:
TensorFlow的交叉熵函數
TensorFlow針對分類問題,實現了四個交叉熵函數,分別是
1.tf.nn.sigmoid_cross_entropy_with_logits
計算 給定 logits 的sigmoid函數 交叉熵。
2.tf.nn.softmax_cross_entropy_with_logits
計算 logits 和 labels 之間的 softmax 交叉熵
3.tf.nn.sparse_softmax_cross_entropy_with_logits
計算 logits 和 labels 之間的 稀疏softmax 交叉熵
4.tf.nn.weighted_cross_entropy_with_logits
計算加權交叉熵
softmax_cross_entropy_with_logits和sparse_softmax_cross_entropy_with_logits區別
英文解釋:
Having two different functions is a convenience, as they produce the same result.
The difference is simple:
- For sparse_softmax_cross_entropy_with_logits, labels must have the shape [batch_size] and the dtype int32 or int64. Each label is an int in range [0, num_classes-1].
- For softmax_cross_entropy_with_logits, labels must have the shape [batch_size, num_classes] and dtype float32 or float64.
Labels used in softmax_cross_entropy_with_logits are the one hot version of labels used in sparse_softmax_cross_entropy_with_logits.
Another tiny difference is that with sparse_softmax_cross_entropy_with_logits, you can give -1 as a label to have loss 0 on this label.
獨熱編碼one_hot
v = tuple(range(0, 5))
v_onehot = tf.one_hot(v, len(v))
v2 = tf.keras.utils.to_categorical(v,num_classes=len(v) )
print("v_onehot:\\n",v_onehot)
print("v2:\\n",v2, type(v2))
<code>v_onehot:
tf.Tensor(
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]], shape=(5, 5), dtype=float32)
v2:
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]] <class>/<code>
閱讀更多 公子小悅 的文章