1

我知道使用 python 和scikit learn,如何计算高斯混合的KL散度,因为它的权重、均值和协方差等参数为np.array,如下所示。

GaussianMixture 使用组件参数初始化 - sklearn

两个 GMM 的 KL-Divergence

但我想知道,使用Tensorflow,有没有办法计算两个高斯混合之间的KL散度,因为它的参数为 Tensor

1)我在Tensorflow中尝试了上面的scikit,但它没有工作,因为Tensorflow在执行会话之前不会给它一个实际值。

2) 有一些 TF 包,但不完全是高斯混合的 KL。 https://www.tensorflow.org/api_docs/python/tf/contrib/distributions/Mixture

https://www.tensorflow.org/api_docs/python/tf/distributions/kl_divergence

任何帮助是极大的赞赏。

后来,我尝试了一个最新的 TF 库,如下所示。

import tensorflow as tf
print('tensorflow ',tf.__version__)  # for Python 3
import numpy as np
import matplotlib.pyplot as plt

ds = tf.contrib.distributions
kl_divergence=tf.contrib.distributions.kl_divergence

# Gaussian Mixure1
mix = 0.3# weight
bimix_gauss1 = ds.Mixture(
cat=ds.Categorical(probs=[mix, 1.-mix]),#weight
components=[
   ds.Normal(loc=-1., scale=0.1),
   ds.Normal(loc=+1., scale=0.5),
])

# Gaussian Mixture2
mix = 0.4# weight
bimix_gauss2 = ds.Mixture(
    cat=ds.Categorical(probs=[mix, 1.-mix]),#weight
    components=[
    ds.Normal(loc=-0.4, scale=0.2),
    ds.Normal(loc=+1.2, scale=0.6),
])

# KL between GM1 and GM2
kl_value=kl_divergence(
    distribution_a=bimix_gauss1,
    distribution_b=bimix_gauss2,
    allow_nan_stats=True,
    name=None
)

sess = tf.Session() # 
with sess.as_default():

    x = tf.linspace(-2., 3., int(1e4)).eval()
    plt.plot(x, bimix_gauss1.prob(x).eval(),'r-')
    plt.plot(x, bimix_gauss2.prob(x).eval(),'b-')
    plt.show()

    print('kl_value=',kl_value.eval())

然后我得到了这个错误...... NotImplementedError: No KL(distribution_a || distribution_b) 为distribution_a type Mixture和distribution_b type Mixture注册

我现在很伤心。:(

4

0 回答 0