Perform a RandomWalk step with Tensorflow Probability's RandomWalkMetropolis function

醉酒当歌 提交于 2019-12-13 04:39:11

问题


I am new to Tensorflow Probability and would like to do a RandomWalk Montecarlo simulation. Let's say I have tensor r that represents a state. I want the tfp.mcmc.RandomWalkMetropolis function to return a proposal for a new state r'.

tfp.mcmc.RandomWalkMetropolis(r)
>>> <tensorflow_probability.python.mcmc.random_walk_metropolis.RandomWalkMetropolis object at 0x14abed2185c0>

Instead of the same state, or a slightly perturbed state only this RandomWalkMetropolis object is returned. The RandomWalkMetropolis class also contains the function one_step, but it requires 'previous_kernel_results', which I don't have, because I want this to be my first step. Also, how do I specify the Metropolis accept/reject step further?


回答1:


RWM is a python object, which is used via the bootstrap_results and one_step methods. For example:

# TF/TFP Imports
!pip install --quiet tfp-nightly tf-nightly
import tensorflow.compat.v2 as tf
tf.enable_v2_behavior()
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors
import matplotlib.pyplot as plt


def log_prob(x):
  return tfd.Normal(0, 1).log_prob(x)

kernel = tfp.mcmc.RandomWalkMetropolis(log_prob)
state = tfd.Normal(0, 1).sample()
extra = kernel.bootstrap_results(state)
samples = []
for _ in range(1000):
  state, extra = kernel.one_step(state, extra)
  samples.append(state)

plt.hist(samples, bins=20)


来源:https://stackoverflow.com/questions/57199905/perform-a-randomwalk-step-with-tensorflow-probabilitys-randomwalkmetropolis-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!