【问题标题】:Mixture of multivariate gaussian distribution tensorflow probability混合多元高斯分布张量流概率
【发布时间】:2020-04-06 10:24:03
【问题描述】:

正如标题中所说,我正在尝试使用 tensorflow 概率包创建多元正态分布的混合。

在我的原始项目中,我输入了神经网络输出的分类权重、位置和方差。但是在创建图表时,我收到以下错误:

components[0] 批次形状必须与 cat 形状和其他组件批次形状兼容

我使用占位符重现了同样的问题:

import tensorflow as tf
import tensorflow_probability as tfp # dist= tfp.distributions 

tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.InteractiveSession()

l1 = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, 2], name='observations_1')
l2 = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, 2], name='observations_2')

log_std = tf.compat.v1.get_variable('log_std', [1, 2], dtype=tf.float32,
                                          initializer=tf.constant_initializer(1.0),
                                          trainable=True)

mix = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None,1], name='weights')

cat = tfp.distributions.Categorical(probs=[mix, 1.-mix])
components = [
    tfp.distributions.MultivariateNormalDiag(loc=l1, scale_diag=tf.exp(log_std)),
    tfp.distributions.MultivariateNormalDiag(loc=l2, scale_diag=tf.exp(log_std)),
]

bimix_gauss = tfp.distributions.Mixture(
  cat=cat,
  components=components)

所以,我的问题是,我做错了什么?我查看了错误,似乎tensorshape_util.is_compatible_with 是引发错误的原因,但我不明白为什么。

谢谢!

【问题讨论】:

    标签: python tensorflow2.0 mixture-model tensorflow-probability


    【解决方案1】:

    当组件是相同类型时,MixtureSameFamily 应该更高效。

    您只传递一个分类实例(使用 .batch_shape [b1,b2,...,bn])和一个 MVNDiag 实例(使用 .batch_shape [b1,b2,...,bn,numcats]) .

    只有两个班,不知道伯努利会不会工作?

    【讨论】:

      【解决方案2】:

      您似乎向tfp.distributions.Categorical 提供了形状错误的输入。它的probs 参数应该是[batch_size, cat_size] 的形状,而您提供的参数是[cat_size, batch_size, 1]。所以也许尝试用tf.concat([mix, 1-mix], 1)参数化probs

      yourlog_std 也可能存在问题,它与l1l2 的形状不同。如果MultivariateNormalDiag 没有正确广播它,请尝试将其形状指定为(None, 2) 或平铺,使其第一个维度对应于您的位置参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 2020-12-29
        • 1970-01-01
        相关资源
        最近更新 更多