def testAssertCloseEpsilon(self):
    x = [0., 5, 10, 15, 20]
    # x != y
    y = [0.1, 5, 10, 15, 20]
    # x = z
    z = [1e-8, 5, 10, 15, 20]
    with self.test_session():
      with ops.control_dependencies([distribution_util.assert_close(x, z)]):
        array_ops.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with ops.control_dependencies([distribution_util.assert_close(x, y)]):
          array_ops.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with ops.control_dependencies([distribution_util.assert_close(y, z)]):
          array_ops.identity(y).eval()
 def _assert_valid_sample(self, x):
     if not self.validate_args: return x
     return control_flow_ops.with_dependencies([
         check_ops.assert_non_positive(x),
         distribution_util.assert_close(
             array_ops.zeros((), dtype=self.dtype),
             math_ops.reduce_logsumexp(x, reduction_indices=[-1])),
     ], x)
 def _assert_valid_sample(self, x):
   if not self.validate_args: return x
   return control_flow_ops.with_dependencies([
       check_ops.assert_positive(x),
       distribution_util.assert_close(
           array_ops.ones((), dtype=self.dtype),
           math_ops.reduce_sum(x, reduction_indices=[-1])),
   ], x)
  def testAssertCloseEpsilon(self):
    x = [0., 5, 10, 15, 20]
    # x != y
    y = [0.1, 5, 10, 15, 20]
    # x = z
    z = [1e-8, 5, 10, 15, 20]
    with self.test_session():
      with tf.control_dependencies([distribution_util.assert_close(x, z)]):
        tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(x, y)]):
          tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(y, z)]):
          tf.identity(y).eval()
  def testAssertCloseNonIntegerDtype(self):
    x = np.array([1., 5, 10, 15, 20], dtype=np.float32)
    y = x + 1e-8
    z = [2., 5, 10, 15, 20]
    with self.test_session():
      with tf.control_dependencies([distribution_util.assert_close(x, y)]):
        tf.identity(x).eval()

      with tf.control_dependencies([distribution_util.assert_close(y, x)]):
        tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(x, z)]):
          tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(y, z)]):
          tf.identity(y).eval()
  def testAssertCloseIntegerDtype(self):
    x = [1, 5, 10, 15, 20]
    y = x
    z = [2, 5, 10, 15, 20]
    with self.test_session():
      with tf.control_dependencies([distribution_util.assert_close(x, y)]):
        tf.identity(x).eval()

      with tf.control_dependencies([distribution_util.assert_close(y, x)]):
        tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(x, z)]):
          tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(y, z)]):
          tf.identity(y).eval()
 def _assert_valid_sample(self, x):
   if not self.validate_args:
     return x
   return control_flow_ops.with_dependencies([
       check_ops.assert_non_positive(x),
       distribution_util.assert_close(
           array_ops.zeros([], dtype=self.dtype),
           math_ops.reduce_logsumexp(x, axis=[-1])),
   ], x)
  def testAssertCloseNonIntegerDtype(self):
    x = np.array([1., 5, 10, 15, 20], dtype=np.float32)
    y = x + 1e-8
    z = [2., 5, 10, 15, 20]
    with self.test_session():
      with tf.control_dependencies([distribution_util.assert_close(x, y)]):
        tf.identity(x).eval()

      with tf.control_dependencies([distribution_util.assert_close(y, x)]):
        tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(x, z)]):
          tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(y, z)]):
          tf.identity(y).eval()
  def testAssertCloseIntegerDtype(self):
    x = [1, 5, 10, 15, 20]
    y = x
    z = [2, 5, 10, 15, 20]
    with self.test_session():
      with tf.control_dependencies([distribution_util.assert_close(x, y)]):
        tf.identity(x).eval()

      with tf.control_dependencies([distribution_util.assert_close(y, x)]):
        tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(x, z)]):
          tf.identity(x).eval()

      with self.assertRaisesOpError("Condition x ~= y"):
        with tf.control_dependencies([distribution_util.assert_close(y, z)]):
          tf.identity(y).eval()
Beispiel #10
0
 def _check_x(self, x):
   """Check x for proper shape, values, then return tensor version."""
   x = ops.convert_to_tensor(x, name="x_before_deps")
   candidate_one = math_ops.reduce_sum(x, reduction_indices=[-1])
   one = constant_op.constant(1., self.dtype)
   dependencies = [check_ops.assert_positive(x), check_ops.assert_less(
       x, one, message="x has components greater than or equal to 1"),
                   distribution_util.assert_close(one, candidate_one)
                  ] if self.validate_args else []
   return control_flow_ops.with_dependencies(dependencies, x)
Beispiel #11
0
 def _check_x(self, x):
   """Check x for proper shape, values, then return tensor version."""
   x = ops.convert_to_tensor(x, name="x_before_deps")
   candidate_one = math_ops.reduce_sum(x, reduction_indices=[-1])
   one = constant_op.constant(1., self.dtype)
   dependencies = [check_ops.assert_positive(x), check_ops.assert_less(
       x, one, message="x has components greater than or equal to 1"),
                   distribution_util.assert_close(one, candidate_one)
                  ] if self.validate_args else []
   return control_flow_ops.with_dependencies(dependencies, x)
  def testAssertCloseNonIntegerDtype(self):
    x = array_ops.placeholder(dtypes.float32)
    y = x + 1e-8
    z = array_ops.placeholder(dtypes.float32)
    feed_dict = {x: [1., 5, 10, 15, 20], z: [2., 5, 10, 15, 20]}
    with self.test_session():
      with ops.control_dependencies([distribution_util.assert_close(x, y)]):
        array_ops.identity(x).eval(feed_dict=feed_dict)

      with ops.control_dependencies([distribution_util.assert_close(y, x)]):
        array_ops.identity(x).eval(feed_dict=feed_dict)

      with self.assertRaisesOpError("Condition x ~= y"):
        with ops.control_dependencies([distribution_util.assert_close(x, z)]):
          array_ops.identity(x).eval(feed_dict=feed_dict)

      with self.assertRaisesOpError("Condition x ~= y"):
        with ops.control_dependencies([distribution_util.assert_close(y, z)]):
          array_ops.identity(y).eval(feed_dict=feed_dict)
Beispiel #13
0
 def _maybe_assert_valid_sample(self, x):
     """Checks the validity of a sample."""
     if not self.validate_args:
         return x
     return control_flow_ops.with_dependencies([
         check_ops.assert_positive(x, message="samples must be positive"),
         distribution_util.assert_close(
             array_ops.ones([], dtype=self.dtype),
             math_ops.reduce_sum(x, -1),
             message="sample last-dimension must sum to `1`"),
     ], x)
Beispiel #14
0
 def _maybe_assert_valid_sample(self, x):
   """Checks the validity of a sample."""
   if not self.validate_args:
     return x
   return control_flow_ops.with_dependencies([
       check_ops.assert_positive(
           x,
           message="samples must be positive"),
       distribution_util.assert_close(
           array_ops.ones((), dtype=self.dtype),
           math_ops.reduce_sum(x, -1),
           message="sample last-dimension must sum to `1`"),
   ], x)
    def testAssertCloseNonIntegerDtype(self):
        x = array_ops.placeholder(dtypes.float32)
        y = x + 1e-8
        z = array_ops.placeholder(dtypes.float32)
        feed_dict = {x: [1., 5, 10, 15, 20], z: [2., 5, 10, 15, 20]}
        with self.test_session():
            with ops.control_dependencies(
                [distribution_util.assert_close(x, y)]):
                array_ops.identity(x).eval(feed_dict=feed_dict)

            with ops.control_dependencies(
                [distribution_util.assert_close(y, x)]):
                array_ops.identity(x).eval(feed_dict=feed_dict)

            with self.assertRaisesOpError("Condition x ~= y"):
                with ops.control_dependencies(
                    [distribution_util.assert_close(x, z)]):
                    array_ops.identity(x).eval(feed_dict=feed_dict)

            with self.assertRaisesOpError("Condition x ~= y"):
                with ops.control_dependencies(
                    [distribution_util.assert_close(y, z)]):
                    array_ops.identity(y).eval(feed_dict=feed_dict)