Comments (5)
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.
just the log joint probability
from zhusuan.
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.
The HMC
works well! Thanks a lot. The mass and step-size adaptations really help to fit my model.
from zhusuan.
Very glad to hear that!
Regarding the issue of nan
, HMC should deal with such cases correctly (see #78).
from zhusuan.
Related Issues (20)
- questions about dlgm_nf.py HOT 1
- Can't compute prior (local_log_prob) of a StochasticTensor inside tf.scan (in LSTM cell) HOT 11
- Clarifying the * N in log_joint? HOT 4
- Dirichlet + Categorical or Dirichlet + Multinomial toy example ? HOT 5
- Collaboration with TensorLayer HOT 5
- save and restore models? HOT 4
- I have some trouble translating a model from PyMC3 HOT 4
- 请问哪里能找到zhusuan的中文文档? HOT 4
- AttributeError: module 'progressbar' has no attribute 'DataSize' HOT 1
- Why the std of y_mean is so small? HOT 7
- Memory leaks caused by VariationalObjective HOT 2
- Eager executation HOT 2
- Get logp from SGMCMC HOT 2
- module 'tensorflow' has no attribute 'make_template' HOT 1
- The examples of ‘semi_supervised_vae’ cannot run successfully HOT 1
- cant install ZhuSuan HOT 4
- AttributeError: module 'tensorflow' has no attribute 'log'
- Examples code is out dated and doesn't work with Tensorflow 2.x HOT 2
- Can't run on MacOS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zhusuan.