class_docstr: Test class for FiniteDifferenceHvp
class_docstr_tok: ["Test","class","for","FiniteDifferenceHvp"]
docstr: Test Hessian-vector product for a function with two variables.
docstr_tok: ["Test","Hessian-vector","product","for","a","function","with","two","variables","."]
code: def test_finite_difference_hvp_2x2(
      self, a_val, b_val, x_val, y_val, vector
  ):
      """Test Hessian-vector product for a function with two variables."""
      a_val = [a_val]
      b_val = [b_val]
      vector = np.array([vector], dtype=np.float32)
  
      policy = HelperPolicy(n_vars=2)
      params = policy.get_params()
      x, y = params[0], params[1]
      a = tf.constant(a_val)
      b = tf.constant(b_val)
      f = a * (x**2) + b * (y**2)
  
      expected_hessian = compute_hessian(f, [x, y])
      expected_hvp = tf.matmul(vector, expected_hessian)
      reg_coeff = 1e-8
      hvp = FiniteDifferenceHVP(base_eps=1.0)
  
      self.sess.run(tf.compat.v1.global_variables_initializer())
      self.sess.run(x.assign([x_val]))
      self.sess.run(y.assign([y_val]))
      hvp.update_hvp(f, policy, (a, b), reg_coeff)
      hx = hvp.build_eval((np.array(a_val), np.array(b_val)))
      hvp = hx(vector[0])
      expected_hvp = expected_hvp.eval()
      assert np.allclose(hvp, expected_hvp)
  
imports: import pickle
  import numpy
  import pytest
  import tensorflow
  from garage.tf.models import Module
  from garage.tf.optimizers.conjugate_gradient_optimizer import ConjugateGradientOptimizer
  from garage.tf.optimizers.conjugate_gradient_optimizer import FiniteDifferenceHVP
  from garage.tf.optimizers.conjugate_gradient_optimizer import _cg, PearlmutterHVP
  from garage.tf.policies import Policy
  from tests.fixtures import TfGraphTestCase
label_desc: Use image node_id 2 for calling the TestFiniteDifferenceHVP obj's underlying member method code with example usage: obj.test_finite_difference_hvp_2x2(a_val, b_val, x_val, y_val, vector) without return types