def test_prior_energy_gradient(self): isa = ISA(4) samples = isa.sample_prior(100) grad = isa.prior_energy_gradient(samples) # simple sanity checks self.assertEqual(grad.shape[0], samples.shape[0]) self.assertEqual(grad.shape[1], samples.shape[1]) f = lambda x: isa.prior_energy(x.reshape(-1, 1)).flatten() df = lambda x: isa.prior_energy_gradient(x.reshape(-1, 1)).flatten() for i in range(samples.shape[1]): relative_error = check_grad(f, df, samples[:, i]) / sqrt(sum(square(df(samples[:, i])))) # comparison with numerical gradient self.assertLess(relative_error, 0.001)
def test_prior_energy_gradient(self): isa = ISA(4) samples = isa.sample_prior(100) grad = isa.prior_energy_gradient(samples) # simple sanity checks self.assertEqual(grad.shape[0], samples.shape[0]) self.assertEqual(grad.shape[1], samples.shape[1]) f = lambda x: isa.prior_energy(x.reshape(-1, 1)).flatten() df = lambda x: isa.prior_energy_gradient(x.reshape(-1, 1)).flatten() for i in range(samples.shape[1]): relative_error = check_grad(f, df, samples[:, i]) / sqrt( sum(square(df(samples[:, i])))) # comparison with numerical gradient self.assertLess(relative_error, 0.001)
def test_loglikelihood(self): isa = ISA(7, ssize=3) samples = isa.sample(100) energy = isa.prior_energy(dot(inv(isa.A), samples)) loglik = isa.loglikelihood(samples) # difference between loglik and -energy should be const self.assertTrue(var(loglik + energy) < 1e-10) isa = ISA(2, 3) samples = isa.sample(20) params = isa.default_parameters() params['ais']['num_samples'] = 5 params['ais']['num_iter'] = 10 loglik = isa.loglikelihood(samples, params, return_all=True) # simple sanity checks self.assertTrue(loglik.shape[0], params['ais']['num_samples']) self.assertTrue(loglik.shape[1], samples.shape[1])