-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use custom Hamiltonian? #121
Comments
Hi, for an introductory example of using HMC, you can refer to the toy example here, where HMC is used to sample a Gaussian-distributed variable: https://github.com/thu-ml/zhusuan/blob/master/examples/toy_examples/gaussian.py. You can also look at other examples listed in our README with For your question, it is best to implement your probabilistic model using A more direct approach for your question is to implement a # Suppose the latent variable you want to sample is p.
def log_prior(p):
# Calculate the log prior density...
return log_prior
def log_likelihood(p):
# Calculate the log-likelihood...
return log_likelihood
def log_joint(observed):
return log_prior(observed['p']) + log_likelihood(observed['p'])
hmc = zs.HMC(step_size=1e-3)
# A tf.Variable should be created to store the current samples.
p = tf.Variable(...)
sample_op, _ = hmc.sample(log_joint, observed={}, latent={'p': p})
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(sample_op)
# Now the value of the tf.Variable p will be updated; p stores the current samples. For this kind of usage (passing Feel free to ask if you have more questions :) |
just the log joint probability |
What will happen if the log-joint probability is |
The |
Very glad to hear that! |
Hi.
The parameters of my model have no constraint.
I implemented the following two functions and want to use HMC algorithm of zhusuan.
How to do that? If I can't use the function directly, how to implement custom
StochasticTensor
?Thank you.
The text was updated successfully, but these errors were encountered: