def testProbStaticScalar(self): # Scalar batch_shape. df = np.asarray(3., dtype=np.float32) # Scalar batch_shape. loc = np.asarray([1], dtype=np.float32) scale_diag = np.asarray([2.], dtype=np.float32) scale_tril = np.diag(scale_diag) expected_mst = _FakeVectorStudentT(df=df, loc=loc, scale_tril=scale_tril) actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) x = 2. * self._rng.rand(4, 1).astype(np.float32) - 1. self.assertAllClose(expected_mst.log_prob(x), self.evaluate(actual_mst.log_prob(x)), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), self.evaluate(actual_mst.prob(x)), rtol=0., atol=1e-5)
def testProbScalarBaseDistributionNonScalarTransform(self): # Scalar batch_shape. df = np.asarray(2., dtype=np.float32) # Non-scalar batch_shape. loc = np.asarray([[0., 0, 0], [1, 2, 3], [1, 0, 1]], dtype=np.float32) scale_diag = np.asarray([[1., 2, 3], [2, 3, 4], [4, 5, 6]], dtype=np.float32) scale_tril = np.concatenate([[np.diag(scale_diag[i])] for i in range(len(scale_diag))]) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT(df=np.tile(df, reps=len(scale_diag)), loc=loc, scale_tril=scale_tril) actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose(expected_mst.log_prob(x), self.evaluate(actual_mst.log_prob(x)), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), self.evaluate(actual_mst.prob(x)), rtol=0., atol=1e-5)
def testProbNonScalarBaseDistributionScalarTransform(self): # Non-scalar batch_shape. df = np.asarray([1., 2., 3.], dtype=np.float32) # Scalar batch_shape. loc = np.asarray([1, 2, 3], dtype=np.float32) scale_diag = np.asarray([2, 3, 4], dtype=np.float32) scale_tril = np.diag(scale_diag) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT(df=df, loc=np.tile(loc[tf.newaxis, :], reps=[len(df), 1]), scale_tril=np.tile( scale_tril[tf.newaxis, :, :], reps=[len(df), 1, 1])) actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose(expected_mst.log_prob(x), self.evaluate(actual_mst.log_prob(x)), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), self.evaluate(actual_mst.prob(x)), rtol=0., atol=1e-5)
def testProbScalarBaseDistributionNonScalarTransformDynamic(self): # Scalar batch_shape. df = np.asarray(2., dtype=np.float32) # Non-scalar batch_shape. loc = np.asarray([[0., 0, 0], [1, 2, 3], [1, 0, 1]], dtype=np.float32) scale_diag = np.asarray([[1., 2, 3], [2, 3, 4], [4, 5, 6]], dtype=np.float32) scale_tril = np.concatenate([[np.diag(scale_diag[i])] for i in range(len(scale_diag))]) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT(df=np.tile(df, reps=len(scale_diag)), loc=loc, scale_tril=scale_tril) with self.test_session(): df_pl = tf.placeholder(tf.float32, name="df") loc_pl = tf.placeholder(tf.float32, name="loc") scale_diag_pl = tf.placeholder(tf.float32, name="scale_diag") feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag} actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose( expected_mst.log_prob(x), actual_mst.log_prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5)
def testProbNonScalarBaseDistributionScalarTransformDynamic(self): # Non-scalar batch_shape. df = np.asarray([1., 2., 3.], dtype=np.float32) # Scalar batch_shape. loc = np.asarray([1, 2, 3], dtype=np.float32) scale_diag = np.asarray([2, 3, 4], dtype=np.float32) scale_tril = np.diag(scale_diag) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT(df=df, loc=np.tile(loc[tf.newaxis, :], reps=[len(df), 1]), scale_tril=np.tile( scale_tril[tf.newaxis, :, :], reps=[len(df), 1, 1])) with self.test_session(): df_pl = tf.placeholder(tf.float32, name="df") loc_pl = tf.placeholder(tf.float32, name="loc") scale_diag_pl = tf.placeholder(tf.float32, name="scale_diag") feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag} actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose( expected_mst.log_prob(x), actual_mst.log_prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5)
def testProbNonScalarBaseDistributionScalarTransformDynamic(self): # Non-scalar batch_shape. df = np.asarray([1., 2., 3.], dtype=np.float32) # Scalar batch_shape. loc = np.asarray([1, 2, 3], dtype=np.float32) scale_diag = np.asarray([2, 3, 4], dtype=np.float32) scale_tril = np.diag(scale_diag) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT( df=df, loc=np.tile(loc[tf.newaxis, :], reps=[len(df), 1]), scale_tril=np.tile(scale_tril[tf.newaxis, :, :], reps=[len(df), 1, 1])) df_pl = tf.placeholder_with_default(input=df, shape=df.shape, name="df") loc_pl = tf.placeholder_with_default(input=loc, shape=loc.shape, name="loc") scale_diag_pl = tf.placeholder_with_default( input=scale_diag, shape=scale_diag.shape, name="scale_diag") actual_mst = _VectorStudentT( df=df_pl, loc=loc_pl, scale_diag=scale_diag_pl, validate_args=True) self.assertAllClose( expected_mst.log_prob(x), self.evaluate(actual_mst.log_prob(x)), rtol=0., atol=1e-5) self.assertAllClose( expected_mst.prob(x), self.evaluate(actual_mst.prob(x)), rtol=0., atol=1e-5)
def testProbStatic(self): # Non-scalar batch_shape. df = np.asarray([1., 2, 3], dtype=np.float32) # Non-scalar batch_shape. loc = np.asarray([[0., 0, 0], [1, 2, 3], [1, 0, 1]], dtype=np.float32) scale_diag = np.asarray([[1., 2, 3], [2, 3, 4], [4, 5, 6]], dtype=np.float32) scale_tril = np.concatenate([[np.diag(scale_diag[i])] for i in range(len(scale_diag))]) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT(df=df, loc=loc, scale_tril=scale_tril) with self.test_session(): actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose(expected_mst.log_prob(x), actual_mst.log_prob(x).eval(), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(), rtol=0., atol=1e-5)
def testProbDynamic(self): # Non-scalar batch_shape. df = np.asarray([1., 2, 3], dtype=np.float32) # Non-scalar batch_shape. loc = np.asarray([[0., 0, 0], [1, 2, 3], [1, 0, 1]], dtype=np.float32) scale_diag = np.asarray([[1., 2, 3], [2, 3, 4], [4, 5, 6]], dtype=np.float32) scale_tril = np.concatenate([[np.diag(scale_diag[i])] for i in range(len(scale_diag))]) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT( df=df, loc=loc, scale_tril=scale_tril) df_pl = tf.placeholder_with_default(input=df, shape=df.shape, name="df") loc_pl = tf.placeholder_with_default(input=loc, shape=loc.shape, name="loc") scale_diag_pl = tf.placeholder_with_default( input=scale_diag, shape=scale_diag.shape, name="scale_diag") actual_mst = _VectorStudentT( df=df_pl, loc=loc_pl, scale_diag=scale_diag_pl, validate_args=True) self.assertAllClose( expected_mst.log_prob(x), self.evaluate(actual_mst.log_prob(x)), rtol=0., atol=1e-5) self.assertAllClose( expected_mst.prob(x), self.evaluate(actual_mst.prob(x)), rtol=0., atol=1e-5)
def testProbStatic(self): # Non-scalar batch_shape. df = np.asarray([1., 2, 3], dtype=np.float32) # Non-scalar batch_shape. loc = np.asarray([[0., 0, 0], [1, 2, 3], [1, 0, 1]], dtype=np.float32) scale_diag = np.asarray([[1., 2, 3], [2, 3, 4], [4, 5, 6]], dtype=np.float32) scale_tril = np.concatenate([[np.diag(scale_diag[i])] for i in range(len(scale_diag))]) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT( df=df, loc=loc, scale_tril=scale_tril) with self.test_session(): actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose(expected_mst.log_prob(x), actual_mst.log_prob(x).eval(), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(), rtol=0., atol=1e-5)
def testProbNonScalarBaseDistributionScalarTransformDynamic(self): # Non-scalar batch_shape. df = np.asarray([1., 2., 3.], dtype=np.float32) # Scalar batch_shape. loc = np.asarray([1, 2, 3], dtype=np.float32) scale_diag = np.asarray([2, 3, 4], dtype=np.float32) scale_tril = np.diag(scale_diag) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT( df=df, loc=np.tile(loc[tf.newaxis, :], reps=[len(df), 1]), scale_tril=np.tile(scale_tril[tf.newaxis, :, :], reps=[len(df), 1, 1])) with self.test_session(): df_pl = tf.placeholder(tf.float32, name="df") loc_pl = tf.placeholder(tf.float32, name="loc") scale_diag_pl = tf.placeholder(tf.float32, name="scale_diag") feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag} actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose(expected_mst.log_prob(x), actual_mst.log_prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5)
def testProbScalarBaseDistributionNonScalarTransformDynamic(self): # Scalar batch_shape. df = np.asarray(2., dtype=np.float32) # Non-scalar batch_shape. loc = np.asarray([[0., 0, 0], [1, 2, 3], [1, 0, 1]], dtype=np.float32) scale_diag = np.asarray([[1., 2, 3], [2, 3, 4], [4, 5, 6]], dtype=np.float32) scale_tril = np.concatenate([[np.diag(scale_diag[i])] for i in range(len(scale_diag))]) x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1. expected_mst = _FakeVectorStudentT( df=np.tile(df, reps=len(scale_diag)), loc=loc, scale_tril=scale_tril) with self.test_session(): df_pl = tf.placeholder(tf.float32, name="df") loc_pl = tf.placeholder(tf.float32, name="loc") scale_diag_pl = tf.placeholder(tf.float32, name="scale_diag") feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag} actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) self.assertAllClose(expected_mst.log_prob(x), actual_mst.log_prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(feed_dict=feed_dict), rtol=0., atol=1e-5)
def testProbStaticScalar(self): with self.test_session(): # Scalar batch_shape. df = np.asarray(3., dtype=np.float32) # Scalar batch_shape. loc = np.asarray([1], dtype=np.float32) scale_diag = np.asarray([2.], dtype=np.float32) scale_tril = np.diag(scale_diag) expected_mst = _FakeVectorStudentT( df=df, loc=loc, scale_tril=scale_tril) actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag, validate_args=True) x = 2. * self._rng.rand(4, 1).astype(np.float32) - 1. self.assertAllClose(expected_mst.log_prob(x), actual_mst.log_prob(x).eval(), rtol=0., atol=1e-5) self.assertAllClose(expected_mst.prob(x), actual_mst.prob(x).eval(), rtol=0., atol=1e-5)