code: def testGradients(self):
      x = [
          [2.0, -1.0, 1000.0, 3.0, 1000.0],
          [1.0, 5.0, 2.0, 4.0, 3.0],
          [2.0, 2.0, 2.0, 2.0, 2.0],
      ]
      grad_ys = [[-1.0, 2.0, 5.0]]
      result = [
          [0, 0, -0.5, 0, -0.5],
          [0, 0, 0, 2, 0],
          [1, 1, 1, 1, 1],
      ]
      if context.executing_eagerly():
          inputs = ops.convert_to_tensor(x)
          with backprop.GradientTape() as tape:
              tape.watch(inputs)
              values = nn_ops.nth_element(inputs, 3)
          grad = tape.gradient(
              values, inputs, ops.convert_to_tensor(grad_ys)
          )
          self.assertAllClose(grad[0], result)
  
      # Test with tf.gradients
      with ops.Graph().as_default():
          with self.session(use_gpu=False) as sess:
              inputs = array_ops.placeholder(
                  dtypes.float32, shape=[3, 5]
              )
              values = nn_ops.nth_element(inputs, 3)
              grad = sess.run(
                  gradients_impl.gradients(
                      values, inputs, grad_ys=grad_ys
                  ),
                  feed_dict={inputs: x},
              )
      self.assertAllClose(grad[0], result)
  
imports: import numpy
  from tensorflow.python.eager import backprop
  from tensorflow.python.eager import context
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import errors
  from tensorflow.python.framework import ops
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import gradients_impl
  from tensorflow.python.ops import nn_ops
  import tensorflow.python.ops.nn_grad
  from tensorflow.python.platform import test
label_desc: Use image node_id 14 for calling the NthElementTest obj's underlying member method code with example usage: obj.testGradients() without return types