我有一个测试概率分布矩阵:
qs = np.array([[0.1, 0.6],
[0.9, 0.4] ])
(每列最多1个)和“真实”分布:
p = np.array([0.5, 0.5])
我想计算TensorFlow 中从p
到qs
每一列的KL散度。我知道有一个函数tf.distributions.kl_divergence
,但是它只需要两个分布...
答案 0 :(得分:1)
您可以遍历各列:
sess = tf.InteractiveSession()
A = tf.distributions.Categorical(probs=[0.5, 0.5])
for i in range(2):
B = tf.distributions.Categorical(probs=list(qs[:,i]))
print(tf.distributions.kl_divergence(A, B).eval())
答案 1 :(得分:0)
方法如下:
sess = tf.InteractiveSession()
A = tf.distributions.Categorical(probs=[0.1, 0.6])
B = tf.distributions.Categorical(probs=[0.9, 0.4])
print(tf.distributions.kl_divergence(A, B).eval())
输出:0.7773577
答案 2 :(得分:0)
此解决方案适用于任意多列,并且仅使用TensorFlow:
qs = np.array([[0.9, 0.6, 0.4, 0.5],
[0.1, 0.4, 0.6, 0.5]])
p = np.array([0.5, 0.5])
# Format `p` to a column vector, for constistency reason
p_t = p.reshape([-1, 1])
# Convert to categorical distributions. Transpose part is important
dist_qs = tf.distributions.Categorical(probs=tf.transpose(qs))
dist_p = tf.distributions.Categorical(probs=tf.transpose(p))
# Calculate KL divergences for qs and broadcasted p
tf.distributions.kl_divergence(dist_p, dist_qs).eval()
这给出了:
array([0.51082562, 0.020411 , 0.020411 , 0.])
看起来是有希望的结果。