Perform A Randomwalk Step With Tensorflow Probability's Randomwalkmetropolis Function
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.RandomWalkMetropo
Solution 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)
Post a Comment for "Perform A Randomwalk Step With Tensorflow Probability's Randomwalkmetropolis Function"