【问题标题】:tfp.mcmc.HamiltonianMonteCarlo Not working in Tensorflow Probabilitytfp.mcmc.HamiltonianMonteCarlo 不在 TensorFlow Probability 中工作
【发布时间】:2020-05-19 03:43:37
【问题描述】:

我有以下代码,它基本上尝试使用张量流概率拟合一个简单的回归模型。代码运行没有错误,但 MCMC 采样器似乎没有做任何事情,因为它返回了初始状态的跟踪。

import tensorflow.compat.v2 as tf
import tensorflow_probability as tfp
from tensorflow_probability import distributions as tfdimport warnings

tf.enable_v2_behavior()

plt.style.use("ggplot")
warnings.filterwarnings('ignore')

ru=4
N=102
N = 102 # number of data points
t = np.linspace(0, 4*np.pi, N)
data = 3+np.sin(t+0.001) + 0.5 + np.random.randn(N)
media_1 = ((data-min(data))/(max(data)-min(data)) ) #+ np.random.normal(0,.05, N)
y = np.repeat(ru, N) + np.random.normal(.3,.01,N) * media_1 + np.random.normal(0, .005, N)

# model
model = tfd.JointDistributionNamed(dict(
  beta1 =  tfd.Normal(0,1) ,   
  intercept = tfd.Normal(0,5 ) ,
  var = tfd.Normal(0.05, 0.0005) , 

  y = lambda intercept,beta1,var: 
        tfd.Independent(tfd.Normal(loc=intercept + beta1 * media_1, scale=var), 
reinterpreted_batch_ndims=1
    )
))

def target_log_prob_fn(intercept, beta1,  var):
    return model.log_prob({'intercept':intercept, 'beta1':beta1, 'var':var, 'y': y }) 

s = model.sample()

init_states = [ tf.fill([1], s['intercept'].numpy(), name='init_intercept'),
            tf.fill([1], s['beta1'].numpy(), name='init_beta1'),
            tf.fill([1], s['var'].numpy(), name='init_var'),] 


num_results = 5000
num_burnin_steps = 3000

# Improve performance by tracing the sampler using `tf.function`
# and compiling it using XLA.
@tf.function(autograph=False, experimental_compile=True)
def do_sampling():
return tfp.mcmc.sample_chain(
  num_results=num_results,
  num_burnin_steps=num_burnin_steps,
  current_state=init_states,

  kernel=tfp.mcmc.HamiltonianMonteCarlo(
      target_log_prob_fn=target_log_prob_fn,
      step_size=0.1,
      num_leapfrog_steps=3)

)

states, kernel_results = do_sampling()

状态中返回的跟踪与 initial_states 中的初始值完全相同...有什么想法吗?

【问题讨论】:

    标签: tensorflow tensorflow2.0 tensorflow-probability


    【解决方案1】:

    我可以通过使用我找到的here 的 sn-p 打印接受率来确认这个 MCMC 采样器没有混合

    print("Acceptance rate:", kernel_results.is_accepted.numpy().mean())
    

    同一页面提供了一些关于如何使您的 HMC 内核自适应的提示,这意味着如果提议的步骤过多被拒绝,它将自动减小步长(如果接受的步骤过多,也会增加步长):

    # Apply a simple step size adaptation during burnin
    @tf.function
    def do_sampling():
      adaptive_kernel = tfp.mcmc.SimpleStepSizeAdaptation(
          tfp.mcmc.HamiltonianMonteCarlo(
            target_log_prob_fn=target_log_prob_fn,
            step_size=0.1,
            num_leapfrog_steps=3),
          num_adaptation_steps=int(.8 * num_burnin_steps),
          target_accept_prob=np.float64(.65))
    
      return tfp.mcmc.sample_chain(
        num_results=num_results,
        num_burnin_steps=num_burnin_steps,
        current_state=init_states,
        kernel=adaptive_kernel,
        trace_fn=lambda cs, kr: kr)
    
    samples, kernel_results = do_sampling()
    print("Acceptance rate:", kernel_results.inner_results.is_accepted.numpy().mean())
    

    这对我来说产生了非零的接受率。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多