Skip to content
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

Problem with sample_node and more_replacements #2496

Closed
twiecki opened this issue Aug 12, 2017 · 8 comments
Closed

Problem with sample_node and more_replacements #2496

twiecki opened this issue Aug 12, 2017 · 8 comments
Assignees

Comments

@twiecki
Copy link
Member

twiecki commented Aug 12, 2017

X = theano.shared(np.array([1., 2., 3.]))
with pm.Model() as model:
    x = pm.Normal('x', mu=0, sd=1)
    n = pm.Normal('n', mu=X*x, sd=1, observed=np.array([1, 2, 3]))
    
    mean_field = pm.fit(method='advi', n=50000, 
                        callbacks=[pm.callbacks.CheckParametersConvergence(diff='relative')])
    
    out_sample = mean_field.sample_node(n, size=100, more_replacements={X: np.random.randn(1000)})

I expect out_sample.eval().shape to provide shape (100, 1000), but it has shape (100, 3). It seems like the replacement is not correctly applied. This is on pymc3 master.

@junpenglao
Copy link
Member

junpenglao commented Aug 12, 2017

The same problem is discussed here: #2435 i think

@twiecki
Copy link
Member Author

twiecki commented Aug 12, 2017

In addition, it seems like sample_node() just returns the exact same value for every sample, thus, uncertainty is not taken into account.

@ferrine
Copy link
Member

ferrine commented Aug 12, 2017

@twiecki Your example is a bit wrong, you are sampling the observed

@twiecki
Copy link
Member Author

twiecki commented Aug 12, 2017

@ferrine Hm, I guess I need to sample the input to the observed?

@ferrine
Copy link
Member

ferrine commented Aug 12, 2017

And add sigma with white noise

@twiecki
Copy link
Member Author

twiecki commented Aug 12, 2017

That seems to work correctly. But it's a bit confusing as sample_ppc samples from the observed.

@twiecki twiecki closed this as completed Aug 12, 2017
@DeliciousHair
Copy link
Contributor

I know this is old, but I'm running into the same problem and the conversation above is not giving me any clues.

As an example, if I try to replicate the results in your blog post, I keep hitting this wall here:

with pm.Model() as logistic_regression:
    inputs = pm.Normal('inputs', mu=X, sd=X_unc, shape=X.shape)
    coeffs = pm.Normal('beta', mu=0, sd=1, shape=2)
    
    linear = pm.math.dot(inputs, coeffs)
    prob = pm.math.sigmoid(linear)
    obs = pm.Bernoulli('obs', p=prob, observed=Y)
    
    # Run ADVI which returns posterior means, standard deviations, and the evidence lower bound (ELBO)
    mean_field = pm.fit(method='advi', n=50000,
                        callbacks=[pm.callbacks.CheckParametersConvergence(diff='relative')])
    
    out_sample = (mean_field.sample_node(prob, size=100, more_replacements={inputs: grid_2d}) > .5)

due to the fact that out_sample.eval() has shape (100, 100). Not sure what to make of this...

@ferrine
Copy link
Member

ferrine commented Mar 5, 2018

Hi! I've replicated your issue in a test suit, going to fix soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants