docstr: Computes forward gradient values for the interpolate function.
docstr_tok: ["Computes","forward","gradient","values","for","the","interpolate","function","."]
code: def test_grad(self):
      """Computes forward gradient values for the interpolate function."""
      dtype = np.float64
      times = tf.constant([2.0, 2.5, 3], dtype=dtype)
      strikes = tf.constant([16, 22, 35], dtype=dtype)
  
      times_data = tf.constant([1.5, 2.5, 3.5, 4.5, 5.5], dtype=dtype)
      sigma_square_data = tf.constant(
          [
              [0.15, 0.25, 0.35, 0.4, 0.45, 0.4],
              [0.2, 0.35, 0.55, 0.45, 0.4, 0.6],
              [0.3, 0.45, 0.25, 0.4, 0.5, 0.65],
              [0.25, 0.25, 0.45, 0.25, 0.5, 0.55],
              [0.35, 0.35, 0.25, 0.4, 0.55, 0.55],
          ],
          dtype=dtype,
      )
      total_variance = (
          tf.expand_dims(times_data, -1) * sigma_square_data
      )
      strike_data = tf.broadcast_to(
          tf.constant([15, 25, 35, 40, 50, 55], dtype=dtype), [5, 6]
      )
  
      interpolator = interpolation_2d.Interpolation2D(
          times_data, strike_data, total_variance, dtype=dtype
      )
  
      def _times_fn(x):
          return interpolator.interpolate(x, strikes)
  
      def _strike_fn(x):
          return interpolator.interpolate(times, x)
  
      # Compute forward gradients with respect to `times` and `strikes`
      grad_times = tff.math.fwd_gradient(_times_fn, times)
      grad_strikes = tff.math.fwd_gradient(_strike_fn, strikes)
  
      # Expected values are computed with finite difference method
      expected_grad_times = np.array([0.284797, 0.386694, -0.5])
      expected_grad_strikes = np.array(
          [0.02002702, 0.04353051, 0.01577941]
      )
      self.assertAllClose(
          grad_times, expected_grad_times, rtol=1e-04, atol=1e-04
      )
      self.assertAllClose(
          grad_strikes, expected_grad_strikes, rtol=1e-04, atol=1e-04
      )
  
file_path: tf-quant-finance/tf_quant_finance/math/interpolation/interpolation_2d/interpolation_2d_test.py
imports: import numpy
  import tensorflow.compat.v1
  import tf_quant_finance
  from tensorflow.python.framework import test_util
label_desc: Use image node_id 5 for calling the Interpolation2DTest obj's underlying member method code with example usage: obj.test_grad() and returns: interpolator, interpolator