Add test
authorJunpeng Lao <junpeng.lao@unifr.ch>
Wed, 18 Apr 2018 08:45:49 +0000 (10:45 +0200)
committerJunpeng Lao <junpeng.lao@unifr.ch>
Wed, 18 Apr 2018 08:45:49 +0000 (10:45 +0200)
tensorflow/contrib/distributions/python/kernel_tests/bijectors/ordered_test.py
tensorflow/contrib/distributions/python/ops/bijectors/ordered.py

index 1bcbfed..2d49b42 100644 (file)
@@ -38,12 +38,12 @@ class OrderedBijectorTest(test.TestCase):
     with self.test_session():
       ordered = Ordered()
       self.assertEqual("ordered", ordered.name)
-      x = np.log([[2., 3, 4], [4., 8, 12]])
-      y = [[0.2, 0.3, 0.4, 0.1], [0.16, 0.32, 0.48, 0.04]]
+      x = np.asarray([[2., 3, 4], [4., 8, 13]])
+      y = [[2., 0, 0], [4., np.log(4.), np.log(5.)]]
       self.assertAllClose(y, ordered.forward(x).eval())
       self.assertAllClose(x, ordered.inverse(y).eval())
       self.assertAllClose(
-          -np.sum(np.log(y), axis=1),
+          -np.sum(y[..., 1:], axis=-1),
           ordered.inverse_log_det_jacobian(y, event_ndims=1).eval(),
           atol=0.,
           rtol=1e-7)
@@ -58,15 +58,15 @@ class OrderedBijectorTest(test.TestCase):
       ordered = Ordered()
       self.assertEqual("ordered", ordered.name)
       x = array_ops.placeholder(shape=[2, None], dtype=dtypes.float32)
-      real_x = np.log([[2., 3, 4], [4., 8, 12]])
+      real_x = np.asarray([[2., 3, 4], [4., 8, 13]])
       y = array_ops.placeholder(shape=[2, None], dtype=dtypes.float32)
-      real_y = [[0.2, 0.3, 0.4, 0.1], [0.16, 0.32, 0.48, 0.04]]
+      real_y = [[2., 0, 0], [4., np.log(4.), np.log(5.)]]
       self.assertAllClose(real_y, ordered.forward(x).eval(
           feed_dict={x: real_x}))
       self.assertAllClose(real_x, ordered.inverse(y).eval(
           feed_dict={y: real_y}))
       self.assertAllClose(
-          -np.sum(np.log(real_y), axis=1),
+          -np.sum(y[..., 1:], axis=-1),
           ordered.inverse_log_det_jacobian(y, event_ndims=1).eval(
               feed_dict={y: real_y}),
           atol=0.,
@@ -82,7 +82,7 @@ class OrderedBijectorTest(test.TestCase):
   def testShapeGetters(self):
     with self.test_session():
       x = tensor_shape.TensorShape([4])
-      y = tensor_shape.TensorShape([5])
+      y = tensor_shape.TensorShape([4])
       bijector = Ordered(validate_args=True)
       self.assertAllEqual(y, bijector.forward_event_shape(x))
       self.assertAllEqual(y.as_list(),
index ec8f660..64cf2e6 100644 (file)
@@ -37,6 +37,9 @@ class Ordered(bijector.Bijector):
   """Bijector which maps a tensor x_k that has increasing elements in the last
   dimension to an unconstrained tensor y_k.
 
+  The inverse of the bijector applied to a normal random vector `X ~ N(0, 1)`
+  gives back a sorted random vector with the same distribution `Y ~ N(0, 1)`
+
   On the last dimension of the tensor, Ordered bijector performs:
   `y[0] = x[0]`
   `y[1:] = math_ops.log(x[1:] - x[:-1])`
@@ -79,7 +82,6 @@ class Ordered(bijector.Bijector):
 
   def _inverse_event_shape_tensor(self, output_shape):
     if self.validate_args:
-      # It is not possible for a negative shape so we need only check <= 1.
       is_greater_one = check_ops.assert_greater(
           output_shape[-1], 1, message="Need last dimension greater than 1.")
       output_shape = control_flow_ops.with_dependencies(
@@ -108,7 +110,7 @@ class Ordered(bijector.Bijector):
   def _maybe_assert_valid_x(self, x):
     if not self.validate_args:
       return x
-    is_valid = check_ops.is_strictly_increasing(
-        x,
+    is_valid = check_ops.assert_positive(
+        x[..., 1:] - x[..., :-1],
         message="Forward transformation input must be strictly increasing.")
     return control_flow_ops.with_dependencies([is_valid], x)
\ No newline at end of file