基础的信息熵理论请参考博文
目录标题
一维随机变量
我们先以最简单的一位随机变量为例,随机定义两组向量表示真实分类标签和预测输出
import tensorflow as tf
y_true=tf.random.normal([100,])
y_true=tf.nn.softmax(y_true)
y_pred=tf.random.normal([100,])
y_pred=tf.nn.softmax(y_pred)
熵
公式
H ( X ) = − ∑ p ( x ) log 2 p ( x ) H(X)=-\sum p(x)\log_{2} p(x) H(X)=−∑p(x)log2p(x)
H_true = -tf.reduce_sum(y_true * tf.math.log(y_true))
H_pred = -tf.reduce_sum(y_pred * tf.math.log(y_pred))
<tf.Tensor: shape=(), dtype=float32, numpy=4.162502>
<tf.Tensor: shape=(), dtype=float32, numpy=4.2000785>
相对熵(relative entropy)或(Kullback-Leibler)KL散度
公式
D ( p ∣ ∣ q ) = ∑ x ∈ χ p ( x ) l o g p (