예제 #1
0
p = StandardNormal((target.size, )).to(args.device)
model_id = get_model_id(args)

state_dict = torch.load(path_check)
pi.load_state_dict(state_dict)

##############
## Training ##
##############

print('Running MCMC...')
time_before = time.time()
samples, rate = metropolis_hastings(
    pi=pi,
    num_dims=target.size,
    num_chains=eval_args.num_chains,
    num_samples=eval_args.num_samples,
    steps_per_sample=eval_args.steps_per_sample,
    burnin_steps=eval_args.burnin_steps,
    proposal_scale=eval_args.proposal_scale)
runtime = time.time() - time_before

print('')
print('Projecting...')
num_chains, num_samples, dim = samples.shape
theta = torch.zeros(num_chains, num_samples, dim)
with torch.no_grad():
    for i in range(num_samples):
        print('{}/{}'.format(i + 1, num_samples), end='\r')
        for t in pi.transforms:
            samples[:, i], _ = t(samples[:, i])
            theta[:, i] = samples[:, i].detach().cpu()
예제 #2
0
def test_univariate_functions(f, x_0, expected_mean, expected_std):
    samples = metropolis_hastings(f, x_0)
    np.testing.assert_almost_equal(samples.mean(), expected_mean, decimal=1)
    np.testing.assert_almost_equal(samples.std(), expected_std, decimal=1)
예제 #3
0
def test_raise_exception_for_non_functions():
    with pytest.raises(TypeError):
        samples = metropolis_hastings(sum([]))
예제 #4
0
def test_raise_exception_for_wrong_n_samples(n_samples):
    with pytest.raises(ValueError):
        samples = metropolis_hastings(standard_norm_dist, 0, n_samples)
예제 #5
0
def test_correctness():
    samples = metropolis_hastings(standard_norm_dist, 0, 100)
    assert len(samples) == 100
    assert isinstance(samples, np.ndarray)
    assert np.issubdtype(samples.dtype, np.floating)