-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix sample_ppc #2309
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
Merged
Merged
Fix sample_ppc #2309
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import pymc3 as pm | ||
| import numpy as np | ||
| import numpy.testing as npt | ||
| import pytest | ||
| import theano.tensor as tt | ||
| import theano | ||
|
|
||
| from pymc3.distributions.distribution import _draw_value, draw_values | ||
|
|
||
|
|
||
| def test_draw_value(): | ||
| npt.assert_equal(_draw_value(np.array([5, 6])), [5, 6]) | ||
| npt.assert_equal(_draw_value(np.array(5.)), 5) | ||
|
|
||
| npt.assert_equal(_draw_value(tt.constant([5., 6.])), [5, 6]) | ||
| assert _draw_value(tt.constant(5)) == 5 | ||
| npt.assert_equal(_draw_value(2 * tt.constant([5., 6.])), [10, 12]) | ||
|
|
||
| val = theano.shared(np.array([5., 6.])) | ||
| npt.assert_equal(_draw_value(val), [5, 6]) | ||
| npt.assert_equal(_draw_value(2 * val), [10, 12]) | ||
|
|
||
| a = tt.scalar('a') | ||
| a.tag.test_value = 6 | ||
| npt.assert_equal(_draw_value(2 * a, givens=[(a, 1)]), 2) | ||
|
|
||
| assert _draw_value(5) == 5 | ||
| assert _draw_value(5.) == 5 | ||
| assert isinstance(_draw_value(5.), type(5.)) | ||
| assert isinstance(_draw_value(5), type(5)) | ||
|
|
||
| with pm.Model(): | ||
| mu = 2 * tt.constant(np.array([5., 6.])) + theano.shared(np.array(5)) | ||
| a = pm.Normal('a', mu=mu, sd=5, shape=2) | ||
|
|
||
| val1 = _draw_value(a) | ||
| val2 = _draw_value(a) | ||
| assert np.all(val1 != val2) | ||
|
|
||
| with pytest.raises(ValueError) as err: | ||
| _draw_value([]) | ||
| err.match('Unexpected type') | ||
|
|
||
|
|
||
| class TestDrawValues(object): | ||
| def test_empty(self): | ||
| assert draw_values([]) == [] | ||
|
|
||
| def test_vals(self): | ||
| npt.assert_equal(draw_values([np.array([5, 6])])[0], [5, 6]) | ||
| npt.assert_equal(draw_values([np.array(5.)])[0], 5) | ||
|
|
||
| npt.assert_equal(draw_values([tt.constant([5., 6.])])[0], [5, 6]) | ||
| assert draw_values([tt.constant(5)])[0] == 5 | ||
| npt.assert_equal(draw_values([2 * tt.constant([5., 6.])])[0], [10, 12]) | ||
|
|
||
| val = theano.shared(np.array([5., 6.])) | ||
| npt.assert_equal(draw_values([val])[0], [5, 6]) | ||
| npt.assert_equal(draw_values([2 * val])[0], [10, 12]) | ||
|
|
||
| def test_simple_model(self): | ||
| with pm.Model(): | ||
| mu = 2 * tt.constant(np.array([5., 6.])) + theano.shared(np.array(5)) | ||
| a = pm.Normal('a', mu=mu, sd=5, shape=2) | ||
|
|
||
| val1 = draw_values([a]) | ||
| val2 = draw_values([a]) | ||
| assert np.all(val1[0] != val2[0]) | ||
|
|
||
| point = {'a': np.array([3., 4.])} | ||
| npt.assert_equal(draw_values([a], point=point), [point['a']]) | ||
|
|
||
| def test_dep_vars(self): | ||
| with pm.Model(): | ||
| mu = 2 * tt.constant(np.array([5., 6.])) + theano.shared(np.array(5)) | ||
| sd = pm.HalfNormal('sd', shape=2) | ||
| tau = 1 / sd ** 2 | ||
| a = pm.Normal('a', mu=mu, tau=tau, shape=2) | ||
|
|
||
| point = {'a': np.array([1., 2.])} | ||
| npt.assert_equal(draw_values([a], point=point), [point['a']]) | ||
|
|
||
| with pytest.raises(theano.gof.MissingInputError): | ||
| draw_values([a]) | ||
|
|
||
| # We need the untransformed vars | ||
| with pytest.raises(theano.gof.MissingInputError): | ||
| draw_values([a], point={'sd': np.array([2., 3.])}) | ||
|
|
||
| val1 = draw_values([a], point={'sd_log__': np.array([2., 3.])})[0] | ||
| val2 = draw_values([a], point={'sd_log__': np.array([2., 3.])})[0] | ||
| assert np.all(val1 != val2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is that required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
draw_valuesused to return a list if it was passed more than one value and a single value otherwise. I didn't like that and change it to return always a list. I am playing around withsample_ppca bit, and there it could be that the list of variable to draw is dynamic, which would make the previous behaviour somewhat strange. But I can change it back if you disagree.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I don't disagree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it effect the speed? Since you commented in #2296 that
draw_valuesmight also cause slowdown inrandommethod.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so