def testInvalidTolRaises(self): loc = rng.rand(2, 3, 4).astype(np.float32) deterministic = deterministic_lib.VectorDeterministic( loc, atol=-1, validate_args=True) with self.cached_session(): with self.assertRaisesOpError("Condition x >= 0"): deterministic.prob(loc).eval()
def testInvalidTolRaises(self): loc = rng.rand(2, 3, 4).astype(np.float32) with self.assertRaisesRegexp(errors.InvalidArgumentError, "Condition x >= 0"): _ = deterministic_lib.VectorDeterministic(loc, atol=-1, validate_args=True)
def testProbVectorDeterministicWithNoBatchDimsOnRZeroRaisesIfXNotInSameRk( self): # 0 batch of deterministics on R^0. deterministic = deterministic_lib.VectorDeterministic( [], validate_args=True) with self.cached_session(): with self.assertRaisesOpError("not defined in the same space"): deterministic.prob([1.]).eval()
def testProbVectorDeterministicWithNoBatchDims(self): # 0 batch of deterministics on R^1. deterministic = deterministic_lib.VectorDeterministic([0.]) with self.cached_session(): self.assertAllClose(1., deterministic.prob([0.]).eval()) self.assertAllClose(0., deterministic.prob([2.]).eval()) self.assertAllClose([1., 0.], deterministic.prob([[0.], [2.]]).eval())
def testInvalidXRaises(self): loc = rng.rand(2, 3, 4).astype(np.float32) deterministic = deterministic_lib.VectorDeterministic( loc, atol=-1, validate_args=True) with self.cached_session(): with self.assertRaisesRegexp(ValueError, "must have rank at least 1"): deterministic.prob(0.).eval()
def testSampleWithBatchDims(self): deterministic = deterministic_lib.VectorDeterministic([[0.], [0.]]) for sample_shape in [(), (4, )]: with self.cached_session(): sample = deterministic.sample(sample_shape) self.assertAllEqual(sample_shape + (2, 1), sample.get_shape()) self.assertAllClose( np.zeros(sample_shape + (2, 1)).astype(np.float32), sample.eval())
def testProbWithNonzeroRTol(self): # 3 batch of deterministics on R^2. loc = [[0., 1.], [1., 1.], [100., 100.]] x = [[0., 1.], [0.9, 1.], [99.9, 100.1]] deterministic = deterministic_lib.VectorDeterministic(loc, rtol=0.01) expected_prob = [1., 0., 1.] with self.cached_session(): prob = deterministic.prob(x) self.assertAllEqual((3, ), prob.get_shape()) self.assertAllEqual(expected_prob, prob.eval())
def testProbWithNonzeroATol(self): # 3 batch of deterministics on R^2. loc = [[0., 1.], [2., 3.], [4., 5.]] x = [[0., 1.], [1.9, 3.], [3.99, 5.]] deterministic = deterministic_lib.VectorDeterministic(loc, atol=0.05) expected_prob = [1., 0., 1.] with self.cached_session(): prob = deterministic.prob(x) self.assertAllEqual((3, ), prob.get_shape()) self.assertAllEqual(expected_prob, prob.eval())
def testShape(self): with self.cached_session(): loc = rng.rand(2, 3, 4) deterministic = deterministic_lib.VectorDeterministic(loc) self.assertAllEqual(deterministic.batch_shape_tensor().eval(), (2, 3)) self.assertAllEqual(deterministic.batch_shape, (2, 3)) self.assertAllEqual(deterministic.event_shape_tensor().eval(), [4]) self.assertEqual(deterministic.event_shape, tensor_shape.TensorShape([4]))
def testSampleDynamicWithBatchDims(self): loc = array_ops.placeholder(np.float32) sample_shape = array_ops.placeholder(np.int32) deterministic = deterministic_lib.VectorDeterministic(loc) for sample_shape_ in [(), (4, )]: with self.cached_session(): sample_ = deterministic.sample(sample_shape).eval( feed_dict={ loc: [[0.], [0.]], sample_shape: sample_shape_ }) self.assertAllClose( np.zeros(sample_shape_ + (2, 1)).astype(np.float32), sample_)
def testEntropy(self): loc = np.array([[8.3, 1.2, 3.3], [-0.1, -3.2, 7.]]) deterministic = deterministic_lib.VectorDeterministic(loc=loc) with self.cached_session() as sess: entropy_ = sess.run(deterministic.entropy()) self.assertAllEqual(np.zeros(2), entropy_)
def testProbVectorDeterministicWithNoBatchDimsOnRZero(self): # 0 batch of deterministics on R^0. deterministic = deterministic_lib.VectorDeterministic( [], validate_args=True) with self.cached_session(): self.assertAllClose(1., deterministic.prob([]).eval())