def test_compose_transform_with_intermediates(transforms): transform = constraints.ComposeTransform(transforms) x = random.normal(random.PRNGKey(2), (7, 5)) y, intermediates = transform.call_with_intermediates(x) logdet = transform.log_abs_det_jacobian(x, y, intermediates) assert_allclose(y, transform(x)) assert_allclose(logdet, transform.log_abs_det_jacobian(x, y))
def test_iaf(): # test for substitute logic for exposed methods `sample_posterior` and `get_transforms` N, dim = 3000, 3 data = random.normal(random.PRNGKey(0), (N, dim)) true_coefs = np.arange(1., dim + 1.) logits = np.sum(true_coefs * data, axis=-1) labels = dist.Bernoulli(logits=logits).sample(random.PRNGKey(1)) def model(data, labels): coefs = numpyro.sample('coefs', dist.Normal(np.zeros(dim), np.ones(dim))) offset = numpyro.sample('offset', dist.Uniform(-1, 1)) logits = offset + np.sum(coefs * data, axis=-1) return numpyro.sample('obs', dist.Bernoulli(logits=logits), obs=labels) adam = optim.Adam(0.01) rng_init = random.PRNGKey(1) guide = AutoIAFNormal(model) svi = SVI(model, guide, elbo, adam) svi_state = svi.init(rng_init, model_args=(data, labels), guide_args=(data, labels)) params = svi.get_params(svi_state) x = random.normal(random.PRNGKey(0), (dim + 1, )) rng = random.PRNGKey(1) actual_sample = guide.sample_posterior(rng, params) actual_output = guide.get_transform(params)(x) flows = [] for i in range(guide.num_flows): if i > 0: flows.append(constraints.PermuteTransform( np.arange(dim + 1)[::-1])) arn_init, arn_apply = AutoregressiveNN( dim + 1, [dim + 1, dim + 1], permutation=np.arange(dim + 1), skip_connections=guide._skip_connections, nonlinearity=guide._nonlinearity) arn = partial(arn_apply, params['auto_arn__{}$params'.format(i)]) flows.append(InverseAutoregressiveTransform(arn)) transform = constraints.ComposeTransform(flows) rng_seed, rng_sample = random.split(rng) expected_sample = guide.unpack_latent( transform(dist.Normal(np.zeros(dim + 1), 1).sample(rng_sample))) expected_output = transform(x) assert_allclose(actual_sample['coefs'], expected_sample['coefs']) assert_allclose( actual_sample['offset'], constraints.biject_to(constraints.interval(-1, 1))( expected_sample['offset'])) assert_allclose(actual_output, expected_output)
assert len(intermediates) == 2 def _make_iaf(input_dim, hidden_dims, rng): arn_init, arn = AutoregressiveNN(input_dim, hidden_dims, param_dims=[1, 1]) _, init_params = arn_init(rng, (input_dim, )) return InverseAutoregressiveTransform(partial(arn, init_params)) @pytest.mark.parametrize( 'transforms', [[constraints.PowerTransform(0.7), constraints.AffineTransform(2., 3.)], [constraints.ExpTransform()], [ constraints.ComposeTransform( [constraints.AffineTransform(-2, 3), constraints.ExpTransform()]), constraints.PowerTransform(3.) ], [ _make_iaf(5, hidden_dims=[10], rng=random.PRNGKey(0)), constraints.PermuteTransform(np.arange(5)[::-1]), _make_iaf(5, hidden_dims=[10], rng=random.PRNGKey(1)) ]]) def test_compose_transform_with_intermediates(transforms): transform = constraints.ComposeTransform(transforms) x = random.normal(random.PRNGKey(2), (7, 5)) y, intermediates = transform.call_with_intermediates(x) logdet = transform.log_abs_det_jacobian(x, y, intermediates) assert_allclose(y, transform(x)) assert_allclose(logdet, transform.log_abs_det_jacobian(x, y))