张量流:高斯混合波的KL散度

2024-10-02 10:18:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道在python和scikit的学习中,如何计算高斯混合体的散度,假设它的权重、平均值和协方差等参数为np.数组,如下所示。在

GaussianMixture initialization using component parameters - sklearn

KL-Divergence of two GMMs

但我想知道,对于张量流,有没有办法计算两个高斯混合体的KL散度,因为它的参数是张量

1)我在Tensorflow中尝试了上面的scikit,但它没有起作用,因为在session执行之前,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())

然后我得到了这个错误。。。 未实施错误:没有为分配类型混合和分配类型混合注册KL(分配|a |分配_b)

我现在很难过。:(


Tags: tftensorflowasdspltlocdistributionsnormal

热门问题