一、香農熵
1.1 香農熵原理公式
在將交叉熵的時,先講一下熵。
熵越大,代表不確定性越大,信息也就越大。
1.2 香農熵tf示例
比如:
對於一個三分類:真實標籤(one-hot之後) y_true = [0, 1, 0]
, 那麼其熵就是0 。
TIP:關於tf2.0的任意對數底,以log2爲例
y = tf.constant([2., 4., 8.])
# log2
tf.math.log(y)/tf.math.log([2.])
計算y_true的熵
y = tf.constant([1])
y_onehot = tf.one_hot(y, depth=3)
def H_p(y_):
"""
計算熵
"""
# 0- 計算熵
h_lst = -tf.math.log(y_) / tf.math.log(2.0)
# 1- 修正-np.inf
# 1-1- 獲取非-np.inf位置
mask = h_lst.numpy() == np.inf
indices = tf.where(mask)
# 1-2- 更新-np.inf
if sum(mask[0]):
update_zero = np.zeros(len(indices))
h_lst_update = tf.scatter_nd(indices, update_zero, h_lst.shape)
else:
h_lst_update = h_lst
return tf.reduce_sum(h_lst_update)
"""
>>> H_p(y_onehot)
<tf.Tensor: id=449, shape=(), dtype=float64, numpy=0.0>
>>>
"""
當預測值y_pred 返回爲[0.1, 0.7, 0.2]是後的熵:
H_p(tf.constant([0.1, 0.7, 0.2)) = 6.158429
二、交叉熵(Cross Entropy)
2.1 交叉熵原理公式
交叉熵公式如下:
可以分解成p的熵與p,q的KL散度。
KL散度是用於衡量兩個分部之間的距離的指標,當p=q時,距離爲0,定義如下:
簡單推導:
2.2 one-hot下交叉熵的變形
因爲交叉熵可以分解成p的熵與p,q的KL散度,當one_hot情況下H(y)=0
所以:
可以看到,只與真實類別i上的概率 有關,對應概率 越大,H(y, y_pred)
越小,當對應概率爲1時, 交叉熵H(y, y_pred)
取得最小值0,此時網絡輸出y_pred與真實標籤y完全一致,神經網絡取得最優狀態。
最小化交叉熵的過程也就是最大化政企類別的預測概率的過程。
2.3 tf2 實現
def crossentropy(y_, y_p):
mask = y_.numpy() == 1.0
indices = tf.where(mask)
# print(mask, indices)
ypi = tf.gather_nd(y_p, indices)
out = - tf.math.log(ypi) / tf.math.log(2.0)
return tf.constant([0.]) if sum(out.numpy() == np.inf) else out
crossentropy( y_onehot, tf.constant([[0.1, 0.9, 0.2]]) )