-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fixes in smc backend refactoring, added test #2286
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import pymc3 as pm | ||
import numpy as np | ||
from pymc3.step_methods import smc | ||
from pymc3.backends.smc_text import TextStage | ||
import pytest | ||
from tempfile import mkdtemp | ||
import shutil | ||
|
@@ -13,15 +14,13 @@ | |
@pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on float32") | ||
class TestSMC(SeededTest): | ||
|
||
def setup_method(self): | ||
super(TestSMC, self).setup_method() | ||
def setup_class(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just want to make sure this is intentional -- you'll now reuse the same test folder for every test in the class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah good point! will fix that! |
||
super(TestSMC, self).setup_class() | ||
self.test_folder = mkdtemp(prefix='ATMIP_TEST') | ||
|
||
@pytest.mark.parametrize('n_jobs', [1, 2]) | ||
def test_sample_n_core(self, n_jobs): | ||
n_chains = 300 | ||
n_steps = 100 | ||
tune_interval = 25 | ||
self.n_chains = 300 | ||
self.n_steps = 100 | ||
self.tune_interval = 25 | ||
|
||
n = 4 | ||
|
||
|
@@ -36,9 +35,6 @@ def test_sample_n_core(self, n_jobs): | |
w1 = stdev | ||
w2 = (1 - stdev) | ||
|
||
def last_sample(x): | ||
return x[(n_steps - 1)::n_steps] | ||
|
||
def two_gaussians(x): | ||
log_like1 = - 0.5 * n * tt.log(2 * np.pi) \ | ||
- 0.5 * tt.log(dsigma) \ | ||
|
@@ -48,7 +44,7 @@ def two_gaussians(x): | |
- 0.5 * (x - mu2).T.dot(isigma).dot(x - mu2) | ||
return tt.log(w1 * tt.exp(log_like1) + w2 * tt.exp(log_like2)) | ||
|
||
with pm.Model() as ATMIP_test: | ||
with pm.Model() as self.ATMIP_test: | ||
X = pm.Uniform('X', | ||
shape=n, | ||
lower=-2. * np.ones_like(mu1), | ||
|
@@ -58,25 +54,44 @@ def two_gaussians(x): | |
like = pm.Deterministic('like', two_gaussians(X)) | ||
llk = pm.Potential('like_potential', like) | ||
|
||
with ATMIP_test: | ||
step = smc.SMC( | ||
n_chains=n_chains, | ||
tune_interval=tune_interval, | ||
likelihood_name=ATMIP_test.deterministics[0].name) | ||
self.muref = mu1 | ||
|
||
@pytest.mark.parametrize('n_jobs', [1, 2]) | ||
def test_sample_n_core(self, n_jobs): | ||
|
||
def last_sample(x): | ||
return x[(self.n_steps - 1)::self.n_steps] | ||
|
||
step = smc.SMC( | ||
n_chains=self.n_chains, | ||
tune_interval=self.tune_interval, | ||
model=self.ATMIP_test, | ||
likelihood_name=self.ATMIP_test.deterministics[0].name) | ||
|
||
mtrace = smc.ATMIP_sample( | ||
n_steps=n_steps, | ||
n_steps=self.n_steps, | ||
step=step, | ||
n_jobs=n_jobs, | ||
progressbar=True, | ||
homepath=self.test_folder, | ||
model=ATMIP_test, | ||
model=self.ATMIP_test, | ||
rm_flag=True) | ||
|
||
d = mtrace.get_values('X', combine=True, squeeze=True) | ||
x = last_sample(d) | ||
mu1d = np.abs(x).mean(axis=0) | ||
np.testing.assert_allclose(mu1, mu1d, rtol=0., atol=0.03) | ||
np.testing.assert_allclose(self.muref, mu1d, rtol=0., atol=0.03) | ||
|
||
def test_stage_handler(self): | ||
stage_number = -1 | ||
stage_handler = TextStage(self.test_folder) | ||
|
||
step = stage_handler.load_atmip_params(stage_number, model=self.ATMIP_test) | ||
assert step.stage == stage_number | ||
|
||
corrupted_chains = stage_handler.recover_existing_results( | ||
stage_number, self.n_steps, step, n_jobs=1, model=self.ATMIP_test) | ||
assert len(corrupted_chains) == 0 | ||
|
||
def teardown_method(self): | ||
def teardown_class(self): | ||
shutil.rmtree(self.test_folder) |
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 like that this nesting is explicit, but just for interest's sake:
starting with python 2.7 you can use multiple context managers in one line
Uh oh!
There was an error while loading. Please reload this page.
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.
cool! didnt know that! but here I think it is indeed better to have them nested for clarity