从贝叶斯网络中采样,并证明有张量流概率

时间:2019-06-03 11:22:36

标签: bayesian-networks tensorflow-probability

是否有一种简便的方法可以从张量流概率的联合分布中“观察”证据和样本?例如,在PyMC3中,Distribution类在其构造函数中具有observed参数,因此人们可以轻松地根据证据进行条件处理(并运行MCMC来获取后验样本)。

有一些与Edward相关的文档,但对于简单的案例,我无法弄清楚,例如:

import tensorflow_probability.python.distributions as tfd
import tensorflow as tf

jdn = tfd.JointDistributionNamed(dict(
    dist_x=tfd.Categorical([0.2, 0.8], validate_args=True),
    dist_y=lambda dist_x: tfd.Categorical(probs=tf.gather([[0.1, 0.9],
                                                               [0.5, 0.5]], indices=dist_x))
))
# sample from joint
print(jdn.sample(100, seed=1234))

# now "observe" some variables
observed_variable = jdn.model.get('dist_x')
assert isinstance(observed_variable, tfd.Distribution)

observed_variable.?

这可能是最简单的具有两个二元变量X和Y的贝叶斯网络。目标是为X或Y设置证据,并从后验样本以估计概率。

(显然,可以使用拒绝采样,方法是先采样无条件的采样,然后丢弃与证据不符的采样,但是效率很低。)

1 个答案:

答案 0 :(得分:0)

通常,后采样很难:)

要获得用于MCMC方案的非标准化目标密度,可以执行类似的操作

import tensorflow_probability.python.distributions as tfd
import tensorflow as tf
import functools

jdn = tfd.JointDistributionNamed(dict(
    x=tfd.Categorical([0.2, 0.8], validate_args=True),
    y=lambda dist_x: tfd.Categorical(probs=tf.gather([[0.1, 0.9],
                                                      [0.5, 0.5]], indices=x))
))

log_unnormalized_posterior = functools.partial(jdn.log_prob, x=data)
# ^-- this is a function of one variable (y), which is the (log) unnormalized
# posterior conditioned on `x=data`.

要在此处获得实际的后验,您需要在所有可能的y值上评估此对数概率函数,然后对其进行归一化。然后,您可以将它们提供给新的“分类”,这将是实际的后验。在TFP中,我们没有固定的方式来执行此操作,主要是因为离散枚举通常非常昂贵。为了从连续变量的密度中采样,我们对汉密尔顿蒙特卡洛法提供了很好的支持,该方法基本上遵循与上述相同的方法,将观察到的变量“钳位”到某些数据以获得未归一化的目标密度,并将其用于MCMC。 / p>

相关问题