问题
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