Beispiel #1
0
def _MaxPoolGradWithArgmax(op, grad, unused_argmax_grad):
  return gen_nn_ops._max_pool_grad_with_argmax(op.inputs[0],
                                               grad,
                                               op.outputs[1],
                                               op.get_attr("ksize"),
                                               op.get_attr("strides"),
                                               padding=op.get_attr("padding"))
Beispiel #2
0
def _MaxPoolGradWithArgmax(op, grad, unused_argmax_grad):
    return gen_nn_ops._max_pool_grad_with_argmax(op.inputs[0],
                                                 grad,
                                                 op.outputs[1],
                                                 op.get_attr("ksize"),
                                                 op.get_attr("strides"),
                                                 padding=op.get_attr("padding"))
 def _CompareMaxPoolingBk(self, input_shape, output_shape, ksize, strides,
                          padding):
   for dtype in np.float32, np.float16:
     # Generate numbers in a narrow range, so that there are many duplicates
     # in the input.
     tensor_input = np.random.random_integers(0, 3, input_shape).astype(dtype)
     tensor_output = np.random.rand(*output_shape).astype(dtype)
     with self.test_session(use_gpu=True):
       t = constant_op.constant(tensor_input, shape=input_shape)
       _, argmax_op = nn_ops.max_pool_with_argmax(t, ksize, strides, padding)
       argmax = argmax_op.eval()
       grad_in = constant_op.constant(tensor_output, shape=output_shape)
       out_op = gen_nn_ops._max_pool_grad_with_argmax(t, grad_in, argmax,
                                                      ksize, strides, padding)
       gpu_val = out_op.eval()
       self.assertShapeEqual(gpu_val, out_op)
     with self.test_session(use_gpu=False):
       t = constant_op.constant(tensor_input, shape=input_shape)
       out_op = nn_ops.max_pool(t, ksize, strides, padding)
       orig_out = out_op.eval()
       grad_in = constant_op.constant(tensor_output, shape=output_shape)
       out_op = gen_nn_ops._max_pool_grad(t, orig_out, grad_in, ksize, strides,
                                          padding)
       cpu_val = out_op.eval()
       self.assertShapeEqual(cpu_val, out_op)
     if dtype == np.float16:
       # The CPU version accumulates its gradient on fp16, so it's less
       # accurate than the GPU version that does the accumulation on fp32
       self.assertAllClose(cpu_val, gpu_val, rtol=0.01, atol=0.01)
     else:
       self.assertAllClose(cpu_val, gpu_val)
 def _CompareMaxPoolingBk(self, input_shape, output_shape, ksize, strides,
                          padding):
   # Generate numbers in a narrow range, so that there are many duplicates
   # in the input.
   tensor_input = np.random.random_integers(0, 3,
                                            input_shape).astype(np.float32)
   tensor_output = np.random.rand(*output_shape).astype(np.float32)
   with self.test_session(use_gpu=True):
     t = tf.constant(tensor_input, shape=input_shape)
     _, argmax_op = tf.nn.max_pool_with_argmax(t, ksize, strides, padding)
     argmax = argmax_op.eval()
     grad_in = tf.constant(tensor_output, shape=output_shape)
     out_op = gen_nn_ops._max_pool_grad_with_argmax(t, grad_in, argmax,
                                                    ksize, strides, padding)
     gpu_val = out_op.eval()
     self.assertShapeEqual(gpu_val, out_op)
   with self.test_session(use_gpu=False):
     t = tf.constant(tensor_input, shape=input_shape)
     out_op = tf.nn.max_pool(t, ksize, strides, padding)
     orig_out = out_op.eval()
     grad_in = tf.constant(tensor_output, shape=output_shape)
     out_op = gen_nn_ops._max_pool_grad(t, orig_out, grad_in, ksize,
                                        strides, padding)
     cpu_val = out_op.eval()
     self.assertShapeEqual(cpu_val, out_op)
   self.assertAllClose(cpu_val, gpu_val, rtol=1e-5, atol=1e-5)
 def _CompareMaxPoolingBk(self, input_shape, output_shape, ksize, strides,
                          padding):
   # Generate numbers in a narrow range, so that there are many duplicates
   # in the input.
   tensor_input = np.random.random_integers(0, 3,
                                            input_shape).astype(np.float32)
   tensor_output = np.random.rand(*output_shape).astype(np.float32)
   with self.test_session(use_gpu=True):
     t = tf.constant(tensor_input, shape=input_shape)
     _, argmax_op = tf.nn.max_pool_with_argmax(t, ksize, strides, padding)
     argmax = argmax_op.eval()
     grad_in = tf.constant(tensor_output, shape=output_shape)
     out_op = gen_nn_ops._max_pool_grad_with_argmax(t, grad_in, argmax,
                                                    ksize, strides, padding)
     gpu_val = out_op.eval()
     self.assertShapeEqual(gpu_val, out_op)
   with self.test_session(use_gpu=False):
     t = tf.constant(tensor_input, shape=input_shape)
     out_op = tf.nn.max_pool(t, ksize, strides, padding)
     orig_out = out_op.eval()
     grad_in = tf.constant(tensor_output, shape=output_shape)
     out_op = gen_nn_ops._max_pool_grad(t, orig_out, grad_in, ksize,
                                        strides, padding)
     cpu_val = out_op.eval()
     self.assertShapeEqual(cpu_val, out_op)
   self.assertAllClose(cpu_val, gpu_val, rtol=1e-5, atol=1e-5)
 def _CompareMaxPoolingBk(self, input_shape, output_shape, ksize, strides,
                          padding):
   for dtype in np.float32, np.float16:
     # Generate numbers in a narrow range, so that there are many duplicates
     # in the input.
     tensor_input = np.random.random_integers(0, 3, input_shape).astype(dtype)
     tensor_output = np.random.rand(*output_shape).astype(dtype)
     with self.test_session(use_gpu=True):
       t = tf.constant(tensor_input, shape=input_shape)
       _, argmax_op = tf.nn.max_pool_with_argmax(t, ksize, strides, padding)
       argmax = argmax_op.eval()
       grad_in = tf.constant(tensor_output, shape=output_shape)
       out_op = gen_nn_ops._max_pool_grad_with_argmax(t, grad_in, argmax,
                                                      ksize, strides, padding)
       gpu_val = out_op.eval()
       self.assertShapeEqual(gpu_val, out_op)
     with self.test_session(use_gpu=False):
       t = tf.constant(tensor_input, shape=input_shape)
       out_op = tf.nn.max_pool(t, ksize, strides, padding)
       orig_out = out_op.eval()
       grad_in = tf.constant(tensor_output, shape=output_shape)
       out_op = gen_nn_ops._max_pool_grad(t, orig_out, grad_in, ksize, strides,
                                          padding)
       cpu_val = out_op.eval()
       self.assertShapeEqual(cpu_val, out_op)
     if dtype == np.float16:
       # The CPU version accumulates its gradient on fp16, so it's less
       # accurate than the GPU version that does the accumulation on fp32
       self.assertAllClose(cpu_val, gpu_val, rtol=0.01, atol=0.01)
     else:
       self.assertAllClose(cpu_val, gpu_val)
 def testMaxPoolingGradWithArgmax(self):
     # MaxPoolWithArgMax is implemented only on GPU.
     if not tf.test.IsBuiltWithCuda():
         return
     orig_input = [1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0]
     tensor_input = [11.0, 12.0, 13.0, 14.0]
     tensor_argmax = list(np.array([0, 1, 3, 5], dtype=np.int64))
     with self.test_session(use_gpu=True) as sess:
         orig_in = tf.constant(orig_input, shape=[1, 3, 3, 1])
         t = tf.constant(tensor_input, shape=[1, 2, 2, 1])
         argmax = tf.constant(tensor_argmax, shape=[1, 2, 2, 1], dtype=tf.int64)
         out_op = gen_nn_ops._max_pool_grad_with_argmax(
             orig_in, t, argmax, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding="VALID"
         )
         out = out_op.eval().flatten()
         self.assertAllClose(out, [11.0, 12.0, 0.0, 13.0, 0.0, 14.0, 0.0, 0.0, 0.0])
 def testMaxPoolingGradWithArgmax(self):
   # MaxPoolWithArgMax is implemented only on GPU.
   if not tf.test.IsBuiltWithCuda():
     return
   orig_input = [1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0]
   tensor_input = [11.0, 12.0, 13.0, 14.0]
   tensor_argmax = list(np.array([0, 1, 3, 5], dtype=np.int64))
   with self.test_session(use_gpu=True) as sess:
     orig_in = tf.constant(orig_input, shape=[1, 3, 3, 1])
     t = tf.constant(tensor_input, shape=[1, 2, 2, 1])
     argmax = tf.constant(tensor_argmax, shape=[1, 2, 2, 1],
                                   dtype=tf.int64)
     out_op = gen_nn_ops._max_pool_grad_with_argmax(orig_in, t, argmax,
                                                    ksize=[1, 2, 2, 1],
                                                    strides=[1, 1, 1, 1],
                                                    padding="VALID")
     out = out_op.eval().flatten()
     self.assertAllClose(out, [11.0, 12.0, 0.0, 13.0, 0.0,
                               14.0, 0.0, 0.0, 0.0])
Beispiel #9
0
def _MaxPoolGradWithArgmax(op, grad, unused_argmax_grad):
  """The gradients for `MaxPoolWithArgmax`.
  Args:
    op: The `MaxPoolWithArgmax` `Operation` that we are differentiating, which
      we can use to find the inputs and outputs of the original op.
    grad: Gradient with respect to the output of the `MaxPoolWithArgmax` op.
    op.inputs[0]: x
    op.outputs[0]: y
    op.outputs[1]: argmax_in_x
  Returns:
    Gradients with respect to the input of `MaxPoolWithArgmax`.
  """

  return gen_nn_ops._max_pool_grad_with_argmax(
      op.inputs[0],
      grad,
      op.outputs[1],
      op.get_attr("ksize"),
      op.get_attr("strides"),
      padding=op.get_attr("padding"))