def test_sgvb(self): with self.get_session(): a = StochasticTensor(Normal(0., 1.), observed=np.asarray([[0., 1., 2.]])) b = StochasticTensor(Normal(1., 2.), observed=np.asarray([[1., 2., 3.]])) c = StochasticTensor(Normal(2., 3.), observed=np.asarray([[2., 3., 4.]])) lower_bound = sgvb([a, b], [c]) self.assertEqual(lower_bound.get_shape().as_list(), [1, 3]) np.testing.assert_almost_equal(lower_bound.eval(), (a.log_prob() + b.log_prob() - c.log_prob()).eval()) lower_bound = sgvb([a], [b, c], latent_axis=0) self.assertEqual(lower_bound.get_shape().as_list(), [3]) np.testing.assert_almost_equal(lower_bound.eval(), (a.log_prob() - b.log_prob() - c.log_prob()).eval().reshape([3])) lower_bound = sgvb([a], [b, c], latent_axis=[0, 1]) self.assertEqual(lower_bound.get_shape().as_list(), []) np.testing.assert_almost_equal( lower_bound.eval(), np.mean((a.log_prob() - b.log_prob() - c.log_prob()).eval()))
def test_log_prob_with_group_events_ndims(self): with self.get_session(): distrib = Normal(np.asarray(0., dtype=np.float32), np.asarray([1.0, 2.0, 3.0], dtype=np.float32), group_event_ndims=1) observed = np.asarray([[-1., 1., 2.], [0., 0., 0.]]) t = StochasticTensor(distrib, observed=observed) np.testing.assert_allclose( t.log_prob(group_event_ndims=0).eval(), distrib.log_prob(t, group_event_ndims=0).eval()) np.testing.assert_allclose( t.log_prob(group_event_ndims=1).eval(), distrib.log_prob(t, group_event_ndims=1).eval())
def test_set_attributes(self): t = StochasticTensor(self.distrib, tf.constant([1., 2., 3.])) self.assertTrue(hasattr(t, '_self_is_observed')) self.assertFalse(hasattr(t.__wrapped__, '_self_is_observed')) t._self_is_observed = 123 self.assertEqual(t._self_is_observed, 123) self.assertFalse(hasattr(t.__wrapped__, '_self_is_observed')) self.assertTrue(hasattr(t, 'log_prob')) self.assertFalse(hasattr(t.__wrapped__, 'log_prob')) t.log_prob = 456 self.assertEqual(t.log_prob, 456) self.assertTrue(hasattr(t, 'log_prob')) self.assertFalse(hasattr(t.__wrapped__, 'log_prob')) self.assertTrue(hasattr(t, 'get_shape')) self.assertTrue(hasattr(t.__wrapped__, 'get_shape')) t.get_shape = 789 self.assertEqual(t.get_shape, 789) self.assertEqual(t.__wrapped__.get_shape, 789) self.assertTrue(hasattr(t, 'get_shape')) self.assertTrue(hasattr(t.__wrapped__, 'get_shape')) t.abc = 1001 self.assertEqual(t.abc, 1001) self.assertEqual(t.__wrapped__.abc, 1001) self.assertTrue(hasattr(t, 'abc')) self.assertTrue(hasattr(t.__wrapped__, 'abc')) t.__wrapped__.xyz = 2002 self.assertEqual(t.xyz, 2002) self.assertEqual(t.__wrapped__.xyz, 2002) self.assertTrue(hasattr(t, 'xyz')) self.assertTrue(hasattr(t.__wrapped__, 'xyz'))
def test_prob_and_log_prob(self): with self.get_session(): distrib = Normal(np.asarray(0., dtype=np.float32), np.asarray([1.0, 2.0, 3.0], dtype=np.float32)) observed = np.arange(24, dtype=np.float32).reshape([4, 2, 3]) t = StochasticTensor(distrib, observed=observed) np.testing.assert_almost_equal(t.log_prob().eval(), distrib.log_prob(observed).eval()) np.testing.assert_almost_equal(t.prob().eval(), distrib.prob(observed).eval())
def test_del_attributes(self): t = StochasticTensor(self.distrib, tf.constant([1., 2., 3.])) del t._self_is_observed self.assertFalse(hasattr(t, '_self_is_observed')) self.assertFalse(hasattr(t.__wrapped__, '_self_is_observed')) t.abc = 1001 del t.abc self.assertFalse(hasattr(t, 'abc')) self.assertFalse(hasattr(t.__wrapped__, 'abc')) t.__wrapped__.xyz = 2002 del t.xyz self.assertFalse(hasattr(t, 'xyz')) self.assertFalse(hasattr(t.__wrapped__, 'xyz')) t.log_prob = 123 del t.log_prob self.assertFalse(hasattr(t.__wrapped__, 'log_prob')) self.assertNotEqual(t.log_prob, 123)