Ejemplo n.º 1
0
  def testApproximateEqual(self):
    for dtype in [np.float32, np.double]:
      x = dtype(1)
      y = dtype(1.00009)
      z = False
      with test_util.device(use_gpu=True):
        # Default tolerance is 0.00001
        z_tf = self.evaluate(math_ops.approximate_equal(x, y))
        self.assertAllEqual(z, z_tf)

    for dtype in [np.float32, np.double]:
      x = dtype(1)
      y = dtype(1.000009)
      z = True
      with test_util.device(use_gpu=True):
        # Default tolerance is 0.00001
        z_tf = self.evaluate(math_ops.approximate_equal(x, y))
        self.assertAllEqual(z, z_tf)

    for dtype in [np.float32, np.double]:
      x = np.array([[[[-1, 2.00009999], [-3, 4.01]]]], dtype=dtype)
      y = np.array([[[[-1.001, 2], [-3.00009, 4]]]], dtype=dtype)
      z = np.array([[[[False, True], [True, False]]]], dtype=np.bool)
      with test_util.device(use_gpu=True):
        z_tf = self.evaluate(math_ops.approximate_equal(x, y, tolerance=0.0001))
        self.assertAllEqual(z, z_tf)
Ejemplo n.º 2
0
    def testApproximateEqual(self):
        for dtype in [np.float32, np.double]:
            x = dtype(1)
            y = dtype(1.00009)
            z = False
            with test_util.device(use_gpu=True):
                # Default tolerance is 0.00001
                z_tf = self.evaluate(math_ops.approximate_equal(x, y))
                self.assertAllEqual(z, z_tf)

        for dtype in [np.float32, np.double]:
            x = dtype(1)
            y = dtype(1.000009)
            z = True
            with test_util.device(use_gpu=True):
                # Default tolerance is 0.00001
                z_tf = self.evaluate(math_ops.approximate_equal(x, y))
                self.assertAllEqual(z, z_tf)

        for dtype in [np.float32, np.double]:
            x = np.array([[[[-1, 2.00009999], [-3, 4.01]]]], dtype=dtype)
            y = np.array([[[[-1.001, 2], [-3.00009, 4]]]], dtype=dtype)
            z = np.array([[[[False, True], [True, False]]]], dtype=np.bool)
            with test_util.device(use_gpu=True):
                z_tf = self.evaluate(
                    math_ops.approximate_equal(x, y, tolerance=0.0001))
                self.assertAllEqual(z, z_tf)
Ejemplo n.º 3
0
 def testApproximateEqualShape(self):
   for dtype in [np.float32, np.double]:
     x = np.array([1, 2], dtype=dtype)
     y = np.array([[1, 2]], dtype=dtype)
     # The inputs 'x' and 'y' must have the same shape.
     with self.assertRaisesRegexp(
         ValueError, "Shapes must be equal rank, but are 1 and 2"):
       math_ops.approximate_equal(x, y)
Ejemplo n.º 4
0
 def testApproximateEqualShape(self):
     for dtype in [np.float32, np.double]:
         x = np.array([1, 2], dtype=dtype)
         y = np.array([[1, 2]], dtype=dtype)
         # The inputs 'x' and 'y' must have the same shape.
         with self.assertRaisesRegexp(
                 ValueError, "Shapes must be equal rank, but are 1 and 2"):
             math_ops.approximate_equal(x, y)
Ejemplo n.º 5
0
 def testApproximateEqualShape(self):
     for dtype in [np.float32, np.double]:
         x = np.array([1, 2], dtype=dtype)
         y = np.array([[1, 2]], dtype=dtype)
         # The inputs 'x' and 'y' must have the same shape.
         with self.assertRaisesRegexp(
             (ValueError, errors.InvalidArgumentError),
                 "Shapes must be equal rank|must be of the same shape"):
             math_ops.approximate_equal(x, y)
Ejemplo n.º 6
0
 def testExecuteFloatAttr(self):
     three = tensor.Tensor(3.0)
     almost_three = tensor.Tensor(2.8)
     almost_equal = math_ops.approximate_equal(three,
                                               almost_three,
                                               tolerance=0.3)
     self.assertTrue(almost_equal.numpy())
Ejemplo n.º 7
0
 def testExecuteFloatAttr(self):
     three = constant_op.constant(3.0)
     almost_three = constant_op.constant(2.8)
     almost_equal = math_ops.approximate_equal(three,
                                               almost_three,
                                               tolerance=0.3)
     self.assertTrue(almost_equal)
Ejemplo n.º 8
0
  def testComplexOps(self):
    for dtype in self.complex_types:
      ctypes = {np.complex64: np.float32}
      self._testBinary(
          math_ops.complex,
          np.array([[[[-1, 2], [2, 0]]]], dtype=ctypes[dtype]),
          np.array([[[[2, -3], [0, 4]]]], dtype=ctypes[dtype]),
          expected=np.array([[[[-1 + 2j, 2 - 3j], [2, 4j]]]], dtype=dtype))

      self._testBinary(
          lambda x, y: math_ops.approximate_equal(x, y, tolerance=0.0001),
          np.array(
              [[[[-1 + 2j, 2.00009999 - 3j], [2 - 3j, 3 + 4.01j]]]],
              dtype=dtype),
          np.array(
              [[[[-1.001 + 2j, 2 - 3j], [2 - 3.00009j, 3 + 4j]]]], dtype=dtype),
          expected=np.array([[[[False, True], [True, False]]]], dtype=dtype))

      self._testBinary(
          gen_math_ops._real_div,
          np.array([3, 3j, -1.5j, -8, 2 + 3j, 2 + 4j, 44 + 3j], dtype=dtype),
          np.array([2, -2, 7j, -4j, 4 - 6j, 1 + 2j, 0], dtype=dtype),
          expected=np.array(
              [
                  1.5, -1.5j, -0.2142857, -2j, (2 + 3j) / (4 - 6j), 2,
                  float("inf")
              ],
              dtype=dtype))

      # TODO(b/65408531): support+test pow for cplx

      lhs = np.array([4 + 2j, -3 - 1j, 2j, 1], dtype=dtype)
      rhs = np.array([5, -6j, 7 - 3j, -8j], dtype=dtype)
      self._testBinary(
          gen_math_ops._reciprocal_grad, lhs, rhs, expected=-rhs * lhs * lhs)

      self._testBinary(
          gen_math_ops._sigmoid_grad, lhs, rhs, expected=rhs * lhs * (1 - lhs))

      # TODO(b/65408531): support+test _rsqrt_grad for cplx (needs pow)

      self._testBinary(
          gen_math_ops._sqrt_grad, lhs, rhs, expected=rhs / (2 * lhs))

      self._testBinary(
          gen_math_ops._tanh_grad, lhs, rhs, expected=rhs * (1 - lhs * lhs))
Ejemplo n.º 9
0
 def testExecuteFloatAttr(self):
   three = tensor.Tensor(3.0)
   almost_three = tensor.Tensor(2.8)
   almost_equal = math_ops.approximate_equal(
       three, almost_three, tolerance=0.3)
   self.assertTrue(almost_equal.numpy())
  def testComplexOps(self):
    for dtype in self.complex_types:
      ctypes = {np.complex64: np.float32}
      self._testBinary(
          math_ops.complex,
          np.array([[[[-1, 2], [2, 0]]]], dtype=ctypes[dtype]),
          np.array([[[[2, -3], [0, 4]]]], dtype=ctypes[dtype]),
          expected=np.array([[[[-1 + 2j, 2 - 3j], [2, 4j]]]], dtype=dtype))

      self._testBinary(
          lambda x, y: math_ops.approximate_equal(x, y, tolerance=0.0001),
          np.array(
              [[[[-1 + 2j, 2.00009999 - 3j], [2 - 3j, 3 + 4.01j]]]],
              dtype=dtype),
          np.array(
              [[[[-1.001 + 2j, 2 - 3j], [2 - 3.00009j, 3 + 4j]]]], dtype=dtype),
          expected=np.array([[[[False, True], [True, False]]]], dtype=dtype))

      self._testBinary(
          gen_math_ops._real_div,
          np.array([3, 3j, -1.5j, -8, 2 + 3j, 2 + 4j], dtype=dtype),
          np.array([2, -2, 7j, -4j, 4 - 6j, 1 + 2j], dtype=dtype),
          expected=np.array(
              [1.5, -1.5j, -0.2142857, -2j, (2 + 3j) / (4 - 6j), 2],
              dtype=dtype))

      # Test inf/nan scenarios.
      self._testBinary(
          gen_math_ops._real_div,
          np.array([4 + 3j, 4, 3j, -4, -4j, 2 - 3j], dtype=dtype),
          np.array([0, 0, 0, 0, 0, 0], dtype=dtype),
          expected=np.array(
              [
                  dtype(1 + 1j) / 0,
                  dtype(1) / 0,
                  dtype(1j) / 0,
                  dtype(-1) / 0,
                  dtype(-1j) / 0,
                  dtype(1 - 1j) / 0
              ],
              dtype=dtype))

      atan2_supported = self.device == "XLA_GPU"
      if atan2_supported:
        self._testBinary(
            math_ops.pow,
            dtype(3 + 2j),
            dtype(4 - 5j),
            expected=np.power(dtype(3 + 2j), dtype(4 - 5j)))
        self._testBinary(  # empty rhs
            math_ops.pow,
            np.array([1 + 2j, 2 - 3j], dtype=dtype),
            np.zeros(shape=[0, 2], dtype=dtype),
            expected=np.zeros(shape=[0, 2], dtype=dtype))
        self._testBinary(  # to zero power
            math_ops.pow,
            np.array([1 + 2j, 2 - 3j], dtype=dtype),
            np.zeros(shape=[1, 2], dtype=dtype),
            expected=np.ones(shape=[1, 2], dtype=dtype))
        lhs = np.array([1 - 2j, 4 + 3j, 2 - 3j, 3, 2j, 1, 4], dtype=dtype)
        rhs = np.array([2, 3j, 3 + 4j, 2 + 3j, 3 - 2j, 2, 3 + 3j], dtype=dtype)
        scalar = dtype(2 + 2j)
        self._testBinary(math_ops.pow, lhs, rhs, expected=np.power(lhs, rhs))
        self._testBinary(
            math_ops.pow, scalar, rhs, expected=np.power(scalar, rhs))
        self._testBinary(math_ops.pow, lhs, scalar, np.power(lhs, scalar))

      lhs = np.array([4 + 2j, -3 - 1j, 2j, 1], dtype=dtype)
      rhs = np.array([5, -6j, 7 - 3j, -8j], dtype=dtype)
      self._testBinary(
          gen_math_ops._reciprocal_grad, lhs, rhs, expected=-rhs * lhs * lhs)

      self._testBinary(
          gen_math_ops._sigmoid_grad, lhs, rhs, expected=rhs * lhs * (1 - lhs))

      if atan2_supported:
        self._testBinary(
            gen_math_ops._rsqrt_grad, lhs, rhs, expected=lhs**3 * rhs / -2)

      self._testBinary(
          gen_math_ops._sqrt_grad, lhs, rhs, expected=rhs / (2 * lhs))

      self._testBinary(
          gen_math_ops._tanh_grad, lhs, rhs, expected=rhs * (1 - lhs * lhs))
Ejemplo n.º 11
0
  def testComplexOps(self):
    for dtype in self.complex_types:
      ctypes = {np.complex64: np.float32}
      self._testBinary(
          math_ops.complex,
          np.array([[[[-1, 2], [2, 0]]]], dtype=ctypes[dtype]),
          np.array([[[[2, -3], [0, 4]]]], dtype=ctypes[dtype]),
          expected=np.array([[[[-1 + 2j, 2 - 3j], [2, 4j]]]], dtype=dtype))

      self._testBinary(
          lambda x, y: math_ops.approximate_equal(x, y, tolerance=0.0001),
          np.array(
              [[[[-1 + 2j, 2.00009999 - 3j], [2 - 3j, 3 + 4.01j]]]],
              dtype=dtype),
          np.array(
              [[[[-1.001 + 2j, 2 - 3j], [2 - 3.00009j, 3 + 4j]]]], dtype=dtype),
          expected=np.array([[[[False, True], [True, False]]]], dtype=dtype))

      self._testBinary(
          gen_math_ops._real_div,
          np.array([3, 3j, -1.5j, -8, 2 + 3j, 2 + 4j], dtype=dtype),
          np.array([2, -2, 7j, -4j, 4 - 6j, 1 + 2j], dtype=dtype),
          expected=np.array(
              [1.5, -1.5j, -0.2142857, -2j, (2 + 3j) / (4 - 6j), 2],
              dtype=dtype))

      # Test inf/nan scenarios.
      self._testBinary(
          gen_math_ops._real_div,
          np.array([4 + 3j, 4, 3j, -4, -4j, 2 - 3j], dtype=dtype),
          np.array([0, 0, 0, 0, 0, 0], dtype=dtype),
          expected=np.array(
              [
                  dtype(1 + 1j) / 0,
                  dtype(1) / 0,
                  dtype(1j) / 0,
                  dtype(-1) / 0,
                  dtype(-1j) / 0,
                  dtype(1 - 1j) / 0
              ],
              dtype=dtype))

      self._testBinary(
          math_ops.pow,
          dtype(3 + 2j),
          dtype(4 - 5j),
          expected=np.power(dtype(3 + 2j), dtype(4 - 5j)))
      self._testBinary(  # empty rhs
          math_ops.pow,
          np.array([1 + 2j, 2 - 3j], dtype=dtype),
          np.zeros(shape=[0, 2], dtype=dtype),
          expected=np.zeros(shape=[0, 2], dtype=dtype))
      self._testBinary(  # to zero power
          math_ops.pow,
          np.array([1 + 2j, 2 - 3j], dtype=dtype),
          np.zeros(shape=[1, 2], dtype=dtype),
          expected=np.ones(shape=[1, 2], dtype=dtype))
      lhs = np.array([1 - 2j, 4 + 3j, 2 - 3j, 3, 2j, 1, 4], dtype=dtype)
      rhs = np.array([2, 3j, 3 + 4j, 2 + 3j, 3 - 2j, 2, 3 + 3j], dtype=dtype)
      scalar = dtype(2 + 2j)
      self._testBinary(math_ops.pow, lhs, rhs, expected=np.power(lhs, rhs))
      self._testBinary(
          math_ops.pow, scalar, rhs, expected=np.power(scalar, rhs))
      self._testBinary(math_ops.pow, lhs, scalar, np.power(lhs, scalar))

      lhs = np.array([4 + 2j, -3 - 1j, 2j, 1], dtype=dtype)
      rhs = np.array([5, -6j, 7 - 3j, -8j], dtype=dtype)
      self._testBinary(
          gen_math_ops._reciprocal_grad, lhs, rhs, expected=-rhs * lhs * lhs)

      self._testBinary(
          gen_math_ops._sigmoid_grad, lhs, rhs, expected=rhs * lhs * (1 - lhs))

      self._testBinary(
          gen_math_ops._rsqrt_grad, lhs, rhs, expected=lhs**3 * rhs / -2)

      self._testBinary(
          gen_math_ops._sqrt_grad, lhs, rhs, expected=rhs / (2 * lhs))

      self._testBinary(
          gen_math_ops._tanh_grad, lhs, rhs, expected=rhs * (1 - lhs * lhs))
Ejemplo n.º 12
0
 def approximate_equal(x, y):
   return math_ops.approximate_equal(x, y)
Ejemplo n.º 13
0
    def testFloatOps(self):
        for dtype in self.float_types:
            self._testBinary(lambda x, y: math_ops.approximate_equal(
                x, y, tolerance=0.0001),
                             np.array([[[[-1, 2.00009999], [-3, 4.01]]]],
                                      dtype=dtype),
                             np.array([[[[-1.001, 2], [-3.00009, 4]]]],
                                      dtype=dtype),
                             expected=np.array(
                                 [[[[False, True], [True, False]]]],
                                 dtype=dtype))

            self._testBinary(gen_math_ops._real_div,
                             np.array([3, 3, -1.5, -8, 44], dtype=dtype),
                             np.array([2, -2, 7, -4, 0], dtype=dtype),
                             expected=np.array(
                                 [1.5, -1.5, -0.2142857, 2,
                                  float("inf")],
                                 dtype=dtype))

            self._testBinary(math_ops.pow,
                             dtype(3),
                             dtype(4),
                             expected=dtype(81))

            self._testBinary(math_ops.pow,
                             np.array([1, 2], dtype=dtype),
                             np.zeros(shape=[0, 2], dtype=dtype),
                             expected=np.zeros(shape=[0, 2], dtype=dtype))
            self._testBinary(math_ops.pow,
                             np.array([10, 4], dtype=dtype),
                             np.array([2, 3], dtype=dtype),
                             expected=np.array([100, 64], dtype=dtype))
            self._testBinary(math_ops.pow,
                             dtype(2),
                             np.array([3, 4], dtype=dtype),
                             expected=np.array([8, 16], dtype=dtype))
            self._testBinary(math_ops.pow,
                             np.array([[2], [3]], dtype=dtype),
                             dtype(4),
                             expected=np.array([[16], [81]], dtype=dtype))

            self._testBinary(gen_math_ops._reciprocal_grad,
                             np.array([4, -3, -2, 1], dtype=dtype),
                             np.array([5, -6, 7, -8], dtype=dtype),
                             expected=np.array([-80, 54, -28, 8], dtype=dtype))

            self._testBinary(gen_math_ops._sigmoid_grad,
                             np.array([4, 3, 2, 1], dtype=dtype),
                             np.array([5, 6, 7, 8], dtype=dtype),
                             expected=np.array([-60, -36, -14, 0],
                                               dtype=dtype))

            self._testBinary(gen_math_ops._rsqrt_grad,
                             np.array([4, 3, 2, 1], dtype=dtype),
                             np.array([5, 6, 7, 8], dtype=dtype),
                             expected=np.array([-160, -81, -28, -4],
                                               dtype=dtype))

            self._testBinary(gen_math_ops._sqrt_grad,
                             np.array([4, 3, 2, 1], dtype=dtype),
                             np.array([5, 6, 7, 8], dtype=dtype),
                             expected=np.array([0.625, 1, 1.75, 4],
                                               dtype=dtype))

            self._testBinary(
                gen_nn_ops._softplus_grad,
                np.array([4, 3, 2, 1], dtype=dtype),
                np.array([5, 6, 7, 8], dtype=dtype),
                expected=np.array(
                    [3.97322869, 2.99258232, 1.99817801, 0.99966466],
                    dtype=dtype))

            self._testBinary(gen_nn_ops._softsign_grad,
                             np.array([4, 3, 2, 1], dtype=dtype),
                             np.array([5, 6, 7, 8], dtype=dtype),
                             expected=np.array(
                                 [0.11111111, 0.06122449, 0.03125, 0.01234568],
                                 dtype=dtype))

            self._testBinary(gen_math_ops._tanh_grad,
                             np.array([4, 3, 2, 1], dtype=dtype),
                             np.array([5, 6, 7, 8], dtype=dtype),
                             expected=np.array([-75, -48, -21, 0],
                                               dtype=dtype))

            self._testBinary(gen_nn_ops._elu_grad,
                             np.array([1, 2, 3, 4, 5, 6], dtype=dtype),
                             np.array([-.6, -.4, -.2, 0, .2, .4], dtype=dtype),
                             expected=np.array([0.4, 1.2, 2.4, 4, 5, 6],
                                               dtype=dtype))

            self._testBinary(
                gen_nn_ops._selu_grad,
                np.array([1, 2, 3, 4, 5, 6], dtype=dtype),
                np.array([-.6, -.4, -.2, .2, .4, .6], dtype=dtype),
                expected=np.array([
                    1.158099340847, 2.7161986816948, 4.67429802254,
                    4.202803949422, 5.2535049367774, 6.30420592413
                ],
                                  dtype=dtype))

            self._testBinary(gen_nn_ops._relu_grad,
                             np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                      dtype=dtype),
                             np.array([0, 0, 0, 0, 0, 0.1, 0.3, 0.5, 0.7, 0.9],
                                      dtype=dtype),
                             expected=np.array([0, 0, 0, 0, 0, 6, 7, 8, 9, 10],
                                               dtype=dtype))

            self._testBinary(
                gen_nn_ops._relu6_grad,
                np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=dtype),
                np.array([0, 0, 0, 0, 0, 0.1, 0.3, 0.5, 0.7, 0.9, 6.1, 10.0],
                         dtype=dtype),
                expected=np.array([0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 0, 0],
                                  dtype=dtype))

            self._testBinary(
                gen_nn_ops._softmax_cross_entropy_with_logits,
                np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=dtype),
                np.array([[0.1, 0.2, 0.3, 0.4], [0.4, 0.3, 0.2, 0.1]],
                         dtype=dtype),
                expected=[
                    np.array([1.44019, 2.44019], dtype=dtype),
                    np.array([[-0.067941, -0.112856, -0.063117, 0.243914],
                              [-0.367941, -0.212856, 0.036883, 0.543914]],
                             dtype=dtype),
                ],
                equality_test=self.ListsAreClose)

            self._testBinary(
                gen_nn_ops._sparse_softmax_cross_entropy_with_logits,
                np.array([[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8],
                          [0.9, 1.0, 1.1, 1.2]],
                         dtype=dtype),
                np.array([2, 1, 7], dtype=np.int32),
                expected=[
                    np.array([1.342536, 1.442536, np.nan], dtype=dtype),
                    np.array([[0.213838, 0.236328, -0.738817, 0.288651],
                              [0.213838, -0.763672, 0.261183, 0.288651],
                              [np.nan, np.nan, np.nan, np.nan]],
                             dtype=dtype),
                ],
                equality_test=self.ListsAreClose)
Ejemplo n.º 14
0
 def loop_fn(i):
   x1 = array_ops.gather(x, i)
   y1 = array_ops.gather(y, i)
   return math_ops.approximate_equal(x1, y1)
Ejemplo n.º 15
0
 def testExecuteFloatAttr(self):
   three = constant_op.constant(3.0)
   almost_three = constant_op.constant(2.8)
   almost_equal = math_ops.approximate_equal(
       three, almost_three, tolerance=0.3)
   self.assertTrue(almost_equal)
Ejemplo n.º 16
0
 def loop_fn(i):
     x1 = array_ops.gather(x, i)
     y1 = array_ops.gather(y, i)
     return math_ops.approximate_equal(x1, y1)
Ejemplo n.º 17
0
  def testFloatOps(self):
    for dtype in self.float_types:
      if dtype == dtypes.bfloat16.as_numpy_dtype:
        a = -1.01
        b = 4.1
      else:
        a = -1.001
        b = 4.01
      self._testBinary(
          lambda x, y: math_ops.approximate_equal(x, y, tolerance=0.0001),
          np.array([[[[-1, 2.00009999], [-3, b]]]], dtype=dtype),
          np.array([[[[a, 2], [-3.00009, 4]]]], dtype=dtype),
          expected=np.array([[[[False, True], [True, False]]]], dtype=dtype))

      self._testBinary(
          gen_math_ops._real_div,
          np.array([3, 3, -1.5, -8, 44], dtype=dtype),
          np.array([2, -2, 7, -4, 0], dtype=dtype),
          expected=np.array(
              [1.5, -1.5, -0.2142857, 2, float("inf")], dtype=dtype))

      self._testBinary(math_ops.pow, dtype(3), dtype(4), expected=dtype(81))

      self._testBinary(
          math_ops.pow,
          np.array([1, 2], dtype=dtype),
          np.zeros(shape=[0, 2], dtype=dtype),
          expected=np.zeros(shape=[0, 2], dtype=dtype))
      self._testBinary(
          math_ops.pow,
          np.array([10, 4], dtype=dtype),
          np.array([2, 3], dtype=dtype),
          expected=np.array([100, 64], dtype=dtype))
      self._testBinary(
          math_ops.pow,
          dtype(2),
          np.array([3, 4], dtype=dtype),
          expected=np.array([8, 16], dtype=dtype))
      self._testBinary(
          math_ops.pow,
          np.array([[2], [3]], dtype=dtype),
          dtype(4),
          expected=np.array([[16], [81]], dtype=dtype))

      self._testBinary(
          math_ops.atan2,
          np.array([0, np.sqrt(2), 1, np.sqrt(2), 0], dtype),
          np.array([1, np.sqrt(2), 0, -np.sqrt(2), -1], dtype),
          expected=np.array(
              [0, np.pi / 4, np.pi / 2, np.pi * 3 / 4, np.pi], dtype=dtype))

      self._testBinary(
          gen_math_ops._reciprocal_grad,
          np.array([4, -3, -2, 1], dtype=dtype),
          np.array([5, -6, 7, -8], dtype=dtype),
          expected=np.array([-80, 54, -28, 8], dtype=dtype))

      self._testBinary(
          gen_math_ops._sigmoid_grad,
          np.array([4, 3, 2, 1], dtype=dtype),
          np.array([5, 6, 7, 8], dtype=dtype),
          expected=np.array([-60, -36, -14, 0], dtype=dtype))

      self._testBinary(
          gen_math_ops._rsqrt_grad,
          np.array([4, 3, 2, 1], dtype=dtype),
          np.array([5, 6, 7, 8], dtype=dtype),
          expected=np.array([-160, -81, -28, -4], dtype=dtype))

      self._testBinary(
          gen_math_ops._sqrt_grad,
          np.array([4, 3, 2, 1], dtype=dtype),
          np.array([5, 6, 7, 8], dtype=dtype),
          expected=np.array([0.625, 1, 1.75, 4], dtype=dtype))

      self._testBinary(
          gen_nn_ops._softplus_grad,
          np.array([4, 3, 2, 1], dtype=dtype),
          np.array([5, 6, 7, 8], dtype=dtype),
          expected=np.array(
              [3.97322869, 2.99258232, 1.99817801, 0.99966466], dtype=dtype))

      self._testBinary(
          gen_nn_ops._softsign_grad,
          np.array([4, 3, 2, 1], dtype=dtype),
          np.array([5, 6, 7, 8], dtype=dtype),
          expected=np.array(
              [0.11111111, 0.06122449, 0.03125, 0.01234568], dtype=dtype))

      self._testBinary(
          gen_math_ops._tanh_grad,
          np.array([4, 3, 2, 1], dtype=dtype),
          np.array([5, 6, 7, 8], dtype=dtype),
          expected=np.array([-75, -48, -21, 0], dtype=dtype))

      self._testBinary(
          gen_nn_ops._elu_grad,
          np.array([1, 2, 3, 4, 5, 6], dtype=dtype),
          np.array([-.6, -.4, -.2, 0, .2, .4], dtype=dtype),
          expected=np.array([0.4, 1.2, 2.4, 4, 5, 6], dtype=dtype))

      self._testBinary(
          gen_nn_ops._selu_grad,
          np.array([1, 2, 3, 4, 5, 6], dtype=dtype),
          np.array([-.6, -.4, -.2, .2, .4, .6], dtype=dtype),
          expected=np.array(
              [1.158099340847, 2.7161986816948, 4.67429802254,
               4.202803949422, 5.2535049367774, 6.30420592413], dtype=dtype))

      self._testBinary(
          gen_nn_ops._relu_grad,
          np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=dtype),
          np.array([0, 0, 0, 0, 0, 0.1, 0.3, 0.5, 0.7, 0.9], dtype=dtype),
          expected=np.array([0, 0, 0, 0, 0, 6, 7, 8, 9, 10], dtype=dtype))

      self._testBinary(
          gen_nn_ops._relu6_grad,
          np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=dtype),
          np.array(
              [0, 0, 0, 0, 0, 0.1, 0.3, 0.5, 0.7, 0.9, 6.1, 10.0], dtype=dtype),
          expected=np.array([0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 0, 0], dtype=dtype))

      self._testBinary(
          gen_nn_ops._softmax_cross_entropy_with_logits,
          np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=dtype),
          np.array([[0.1, 0.2, 0.3, 0.4], [0.4, 0.3, 0.2, 0.1]], dtype=dtype),
          expected=[
              np.array([1.44019, 2.44019], dtype=dtype),
              np.array([[-0.067941, -0.112856, -0.063117, 0.243914],
                        [-0.367941, -0.212856, 0.036883, 0.543914]],
                       dtype=dtype),
          ],
          equality_test=self.ListsAreClose)

      self._testBinary(
          gen_nn_ops._sparse_softmax_cross_entropy_with_logits,
          np.array([[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8],
                    [0.9, 1.0, 1.1, 1.2]], dtype=dtype),
          np.array([2, 1, 7], dtype=np.int32),
          expected=[
              np.array([1.342536, 1.442536, np.nan], dtype=dtype),
              np.array([[0.213838, 0.236328, -0.738817, 0.288651],
                        [0.213838, -0.763672, 0.261183, 0.288651],
                        [np.nan, np.nan, np.nan, np.nan]],
                       dtype=dtype),
          ],
          equality_test=self.ListsAreClose)