message: Add extra test for sparse_segment_sum/mean/sqrtn_grad
  This explicit gradient test makes it a lot easier to debug the kernel
  implementation than the existing tests that rely on gradient_checker.
diff: @@ -519,6 +519,22 @@ class SparseSegmentReductionHelper(SegmentReductionHelper):
  return self._segmentReduce(
  segment_indices, x[indices], op1, op2, num_segments=num_segments)
  + def _sparseSegmentReduceGrad(self, ygrad, indices, segment_ids, output_dim0,
  + mode):
  + assert(mode in ('sum', 'mean', 'sqrtn'))
  + if mode != 'sum':
  + weights = np.zeros(ygrad.shape[0], ygrad.dtype)
  + for segment in segment_ids:
  + weights[segment] += 1
  + weights = 1. / weights if mode == 'mean' else 1. / np.sqrt(weights)
  + xgrad = np.zeros([output_dim0, ygrad.shape[1]], ygrad.dtype)
  + for segment, index in zip(segment_ids, indices):
  + if mode == 'sum':
  + xgrad[index] += ygrad[segment]
  + else:
  + xgrad[index] += ygrad[segment] * weights[segment]
  + return xgrad
  +
  class SparseSegmentReductionOpTest(SparseSegmentReductionHelper):
  @@ -865,6 +881,26 @@ class SparseSegmentReductionOpTest(SparseSegmentReductionHelper):
  delta=1)
  self.assertAllClose(jacob_t, jacob_n)
  + def testGradientExplicit(self):
  + # Note that the GPU implem has different paths for different inner sizes.
  + for inner_size in (1, 2, 3, 32):
  + with self.session():
  + tf_ygrad, np_ygrad = self._input(
  + [3, inner_size], dtype=dtypes_lib.float32)
  + segment_ids = [0, 1, 2, 2, 2]
  + indices = [8, 3, 0, 9, 3]
  + output_dim0 = 10
  + ops_list = [
  + (math_ops.sparse_segment_sum_grad, 'sum'),
  + (math_ops.sparse_segment_mean_grad, 'mean'),
  + (math_ops.sparse_segment_sqrt_n_grad, 'sqrtn'),
  + ]
  + for tf_op, mode in ops_list:
  + np_xgrad = self._sparseSegmentReduceGrad(
  + np_ygrad, indices, segment_ids, output_dim0, mode)
  + tf_xgrad = tf_op(tf_ygrad, indices, segment_ids, output_dim0)
  + self.assertAllClose(tf_xgrad, np_xgrad)
  +
  def testGradientValid(self):
  # Baseline for the testGradient*Invalid* methods below.
  tf_x, _ = self._input([3, 4], dtype=dtypes_lib.float32)
  