def test_aevb_empirical(self): _, model, _ = models.exponential_beta(n=2) x = model.x mu = theano.shared(x.init_value) rho = theano.shared(np.zeros_like(x.init_value)) with model: inference = ADVI(local_rv={x: (mu, rho)}) approx = inference.approx trace0 = approx.sample(10000) approx = Empirical(trace0, local_rv={x: (mu, rho)}) trace1 = approx.sample(10000) approx.random(no_rand=True) approx.random_fn(no_rand=True) np.testing.assert_allclose(trace0['y'].mean(0), trace1['y'].mean(0), atol=0.02) np.testing.assert_allclose(trace0['y'].var(0), trace1['y'].var(0), atol=0.02) np.testing.assert_allclose(trace0['x'].mean(0), trace1['x'].mean(0), atol=0.02) np.testing.assert_allclose(trace0['x'].var(0), trace1['x'].var(0), atol=0.02)
def test_approximate(self): with models.multidimensional_model()[1]: meth = ADVI() fit(10, method=meth) with pytest.raises(KeyError): fit(10, method='undefined') with pytest.raises(TypeError): fit(10, method=1)
def test_from_advi(another_simple_model): with another_simple_model: advi = ADVI() full_rank = FullRankADVI.from_advi(advi) full_rank.fit(20)
def test_from_mean_field(another_simple_model): with another_simple_model: advi = ADVI() full_rank = FullRankADVI.from_mean_field(advi.approx) full_rank.fit(20)
def test_from_advi(self): with models.multidimensional_model()[1]: advi = ADVI() full_rank = FullRankADVI.from_advi(advi) full_rank.fit(20)
def test_from_mean_field(self): with models.multidimensional_model()[1]: advi = ADVI() full_rank = FullRankADVI.from_mean_field(advi.approx) full_rank.fit(20)
approx.randidx(None).eval() approx.randidx(1).eval() approx.random_fn(no_rand=True) approx.random_fn(no_rand=False) approx.histogram_logp.eval() def test_init_from_noize(self): with models.multidimensional_model()[1]: approx = Empirical.from_noise(100) assert approx.histogram.eval().shape == (100, 6) _model = models.simple_model()[1] with _model: pm.Potential('pot', tt.ones((10, 10))) _advi = ADVI() _fullrank_advi = FullRankADVI() _svgd = SVGD() @pytest.mark.parametrize( ['method', 'kwargs', 'error'], [('undefined', dict(), KeyError), (1, dict(), TypeError), (_advi, dict(start={}), None), (_fullrank_advi, dict(), None), (_svgd, dict(), None), ('advi', dict(), None), ('advi->fullrank_advi', dict(frac=.1), None), ('advi->fullrank_advi', dict(frac=1), ValueError), ('fullrank_advi', dict(), None), ('svgd', dict(), None), ('svgd', dict(start={}), None), ('svgd', dict(local_rv={_model.free_RVs[0]: (0, 1)}), ValueError)]) def test_fit(method, kwargs, error):
def test_approximate(self): with models.multidimensional_model()[1]: meth = ADVI() fit(10, method=meth) self.assertRaises(KeyError, fit, 10, method='undefined') self.assertRaises(TypeError, fit, 10, method=1)