首页 > 解决方案 > TransformedTransitionKernel 无法在 sample_chain 中应用逆双射器

问题描述

我正在尝试为一些时间序列数据运行一个相当简单的 MCMC 示例。我相信我包含了所有必需的参数,但我仍然遇到错误。

库版本:

tensorflow==1.14.0
tensorflow-proability==0.7.0

我尝试回滚到 tfp 0.6.0 并出现matmul错误。我尝试每晚推进到 tf 并得到与以下相同的错误。

编码

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tf.enable_eager_execution()

rate_prior = tfd.Exponential(1./mean_users)
users_before = [...]  # bogus positive ints
users_after = [...]  # bogus positive ints

def unnormalized_log_prob(rate_before, rate_after):
    users_before_prior = tfd.Poisson(rate_before)
    users_after_prior = tfd.Poisson(rate_after)
    return (rate_prior.log_prob(rate_before)
        + rate_prior.log_prob(rate_after)
        + tf.reduce_sum(users_before_prior.log_prob(users_before))
        + tf.reduce_sum(users_after_prior.log_prob(users_after))
    )

bijectors = [tfp.bijectors.Exp, tfp.bijectors.Exp]
hmc = tfp.mcmc.TransformedTransitionKernel(
    tfp.mcmc.HamiltonianMonteCarlo(
        target_log_prob_fn=unnormalized_log_prob,
        step_size=10.,
        num_leapfrog_steps=3,
    ),
    bijectors
)

states = tfp.mcmc.sample_chain(
    num_results=10000,
    current_state=[tf.ones(2) * mean_users],
    kernel=hmc,
    trace_fn=None
)

这将返回一个错误。

.../tensorflow_probability/python/mcmc/transformed_kernel.py in <listcomp>(.0)
     71   def fn(state_parts):
     72     return [b.inverse(sp)
---> 73             for b, sp in zip(bijector, state_parts)]
     74   return fn
     75 
TypeError: inverse() missing 1 required positional argument: 'y'`

标签: tensorflow-probability

解决方案


尝试

bijectors = [tfp.bijectors.Exp(), tfp.bijectors.Exp()]]


推荐阅读