Example in readme running
See original GitHub issueThe example the readme returns this error
SyntaxError: Expected a random variable of a constant to initialize distribution, got np.zeros(np.shape(x)[-1])
instead.
Maybe you are trying to initialize a distribution directly, or call a function inside the distribution initialization. While this would be a perfectly legitimate move, it is currently not supported in mcx. Use an intermediate variable instead:
Do not do `x <~ Normal(Normal(0, 1), 1)` or `x <~ Normal(my_function(10), 1)`, instead do `y <~ Normal(0, 1) & x <~ Normal(y, 1)` and `y = my_function(10) & x <~ Normal(y, 1)`
This is due to the linear_regression
function. The version of that function in the hmc_test.py
works fine though.
It would also be good to generate the data in the this example so that the example is self-contained.
So the following script has those 2 fixes and should work instead:
from jax import numpy as np
import jax
import numpy as onp
import mcx
from mcx.distributions import Exponential, Normal
rng_key = jax.random.PRNGKey(0)
x_data = onp.random.normal(0, 5, size=1000).reshape(-1, 1)
y_data = 3 * x_data + onp.random.normal(size=x_data.shape)
observations = {'x': x_data, 'predictions': y_data, 'lmbda': 3.}
@mcx.model
def linear_regression(x, lmbda=1.0):
sigma <~ Exponential(lmbda)
coeffs_init = np.ones(x.shape[-1])
coeffs <~ Normal(coeffs_init, sigma)
y = np.dot(x, coeffs)
predictions <~ Normal(y, sigma)
return predictions
kernel = mcx.HMC(100)
sampler = mcx.sampler(
rng_key,
linear_regression,
kernel,
**observations
)
posterior = sampler.run()
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Make a README
A README is a text file that introduces and explains a project. It contains information that is commonly required to understand what the...
Read more >How to Write a Good README File for Your GitHub Project
Go the extra mile and write tests for your application. Then provide code examples and how to run them. This will help show...
Read more >README.md template | Documenting your project - Drupal
Information about content and formatting of project README files.
Read more >project-guidelines/README.sample.md at master - GitHub
A quick introduction of the minimal setup you need to get a hello world up & running. commands here. Here you should say...
Read more >ReadMe File Examples
1 C++ Example. 2 Python Example. All code you submit must by accompanied by a Readme file, preferably in markdown format, and named...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok I’ll do this!
Reversed it to a self-contained example.