diff options
author | 2018-10-02 08:30:36 -0700 | |
---|---|---|
committer | 2018-10-02 08:34:53 -0700 | |
commit | 28757ad658243526d84fd16d53b9eefbf809c6ff (patch) | |
tree | 4ac420c5bc9effbf9858293356f32d86026b6c9c /tensorflow/python/kernel_tests | |
parent | 97d515273a1e86a861cdfb338671a42b3b1126a7 (diff) |
Use xlogy in a few places in TFP to avoid NaN's for certain special cases.
PiperOrigin-RevId: 215392621
Diffstat (limited to 'tensorflow/python/kernel_tests')
4 files changed, 37 insertions, 0 deletions
diff --git a/tensorflow/python/kernel_tests/distributions/beta_test.py b/tensorflow/python/kernel_tests/distributions/beta_test.py index d580a415dd..42e81bd658 100644 --- a/tensorflow/python/kernel_tests/distributions/beta_test.py +++ b/tensorflow/python/kernel_tests/distributions/beta_test.py @@ -167,6 +167,11 @@ class BetaTest(test.TestCase): self.assertAllClose([[1., 3. / 2], [3. / 2, 15. / 8]], self.evaluate(pdf)) self.assertEqual((2, 2), pdf.get_shape()) + def testLogPdfOnBoundaryIsFiniteWhenAlphaIsOne(self): + b = [[0.01, 0.1, 1., 2], [5., 10., 2., 3]] + pdf = self.evaluate(beta_lib.Beta(1., b).prob(0.)) + self.assertAllEqual(np.ones_like(pdf, dtype=np.bool), np.isfinite(pdf)) + def testBetaMean(self): a = [1., 2, 3] b = [2., 4, 1.2] diff --git a/tensorflow/python/kernel_tests/distributions/dirichlet_test.py b/tensorflow/python/kernel_tests/distributions/dirichlet_test.py index cace5b3ba2..0f96382453 100644 --- a/tensorflow/python/kernel_tests/distributions/dirichlet_test.py +++ b/tensorflow/python/kernel_tests/distributions/dirichlet_test.py @@ -83,6 +83,23 @@ class DirichletTest(test.TestCase): with self.assertRaisesOpError("sample last-dimension must sum to `1`"): self.evaluate(dist.prob([.1, .2, .8])) + def testLogPdfOnBoundaryIsFiniteWhenAlphaIsOne(self): + # Test concentration = 1. for each dimension. + concentration = 3 * np.ones((10, 10)).astype(np.float32) + concentration[range(10), range(10)] = 1. + x = 1 / 9. * np.ones((10, 10)).astype(np.float32) + x[range(10), range(10)] = 0. + dist = dirichlet_lib.Dirichlet(concentration) + log_prob = self.evaluate(dist.log_prob(x)) + self.assertAllEqual( + np.ones_like(log_prob, dtype=np.bool), np.isfinite(log_prob)) + + # Test when concentration[k] = 1., and x is zero at various dimensions. + dist = dirichlet_lib.Dirichlet(10 * [1.]) + log_prob = self.evaluate(dist.log_prob(x)) + self.assertAllEqual( + np.ones_like(log_prob, dtype=np.bool), np.isfinite(log_prob)) + def testPdfZeroBatches(self): alpha = [1., 2] x = [.5, .5] diff --git a/tensorflow/python/kernel_tests/distributions/exponential_test.py b/tensorflow/python/kernel_tests/distributions/exponential_test.py index 367f8bb0f1..1600387585 100644 --- a/tensorflow/python/kernel_tests/distributions/exponential_test.py +++ b/tensorflow/python/kernel_tests/distributions/exponential_test.py @@ -65,6 +65,13 @@ class ExponentialTest(test.TestCase): self.assertAllClose(self.evaluate(log_pdf), expected_log_pdf) self.assertAllClose(self.evaluate(pdf), np.exp(expected_log_pdf)) + def testExponentialLogPDFBoundary(self): + # Check that Log PDF is finite at 0. + rate = np.array([0.1, 0.5, 1., 2., 5., 10.], dtype=np.float32) + exponential = exponential_lib.Exponential(rate=rate) + log_pdf = exponential.log_prob(0.) + self.assertAllClose(np.log(rate), self.evaluate(log_pdf)) + def testExponentialCDF(self): batch_size = 6 lam = constant_op.constant([2.0] * batch_size) diff --git a/tensorflow/python/kernel_tests/distributions/gamma_test.py b/tensorflow/python/kernel_tests/distributions/gamma_test.py index 4eff40b029..4c5b9c3ea3 100644 --- a/tensorflow/python/kernel_tests/distributions/gamma_test.py +++ b/tensorflow/python/kernel_tests/distributions/gamma_test.py @@ -77,6 +77,14 @@ class GammaTest(test.TestCase): self.assertAllClose(self.evaluate(log_pdf), expected_log_pdf) self.assertAllClose(self.evaluate(pdf), np.exp(expected_log_pdf)) + def testGammaLogPDFBoundary(self): + # When concentration = 1, we have an exponential distribution. Check that at + # 0 we have finite log prob. + rate = np.array([0.1, 0.5, 1., 2., 5., 10.], dtype=np.float32) + gamma = gamma_lib.Gamma(concentration=1., rate=rate) + log_pdf = gamma.log_prob(0.) + self.assertAllClose(np.log(rate), self.evaluate(log_pdf)) + def testGammaLogPDFMultidimensional(self): batch_size = 6 alpha = constant_op.constant([[2.0, 4.0]] * batch_size) |