GithubHelp home page GithubHelp logo

How to use custom Hamiltonian? about zhusuan HOT 5 CLOSED

rzu512 avatar rzu512 commented on June 12, 2024
How to use custom Hamiltonian?

from zhusuan.

Comments (5)

csy530216 avatar csy530216 commented on June 12, 2024

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 HMC or SGMCMC as the inference method, which are more practical and complicated than the toy gaussian example.

For your question, it is best to implement your probabilistic model using BayesianNet in ZhuSuan, then your model will automatically have a log_joint property corresponding to the posterior distribution, then you can pass the MetaBayesianNet instance to HMC.

A more direct approach for your question is to implement a log_joint function by yourself. A minimal description is as follows:

# 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 log_joint callable function instead of a MetaBayesianNet instance to hmc.sample), please refer to some older versions of our examples, such as https://github.com/thu-ml/zhusuan/blob/fe8b7cba1e0ec31b37eeb6652d0e87d28fad18fd/examples/topic_models/lntm_mcem.py.

Feel free to ask if you have more questions :)

from zhusuan.

thjashin avatar thjashin commented on June 12, 2024

just the log joint probability

from zhusuan.

rzu512 avatar rzu512 commented on June 12, 2024

What will happen if the log-joint probability is -inf or nan? Would the HMC automatically reject the proposal in these two cases?

from zhusuan.

rzu512 avatar rzu512 commented on June 12, 2024

The HMC works well! Thanks a lot. The mass and step-size adaptations really help to fit my model.

from zhusuan.

csy530216 avatar csy530216 commented on June 12, 2024

Very glad to hear that!
Regarding the issue of nan, HMC should deal with such cases correctly (see #78).

from zhusuan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.