question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example in readme running

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremiecoulloncommented, Nov 26, 2020

Ok I’ll do this!

0reactions
rloufcommented, Nov 28, 2020

Reversed it to a self-contained example.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found