code: def _build_hessian_vector_product(func, params, reg_coeff=1e-5):
      """Computes Hessian-vector product using Pearlmutter's algorithm.
  
      `Pearlmutter, Barak A. "Fast exact multiplication by the Hessian." Neural
      computation 6.1 (1994): 147-160.`
  
      Args:
          func (callable): A function that returns a torch.Tensor. Hessian of
              the return value will be computed.
          params (list[torch.Tensor]): A list of function parameters.
          reg_coeff (float): A small value so that A -> A + reg*I.
  
      Returns:
          function: It can be called to get the final result.
  
      """
      param_shapes = [p.shape or torch.Size([1]) for p in params]
      f = func()
      f_grads = torch.autograd.grad(f, params, create_graph=True)
  
      def _eval(vector):
          """The evaluation function.
  
          Args:
              vector (torch.Tensor): The vector to be multiplied with
                  Hessian.
  
          Returns:
              torch.Tensor: The product of Hessian of function f and v.
  
          """
          unflatten_vector = unflatten_tensors(vector, param_shapes)
  
          assert len(f_grads) == len(unflatten_vector)
          grad_vector_product = torch.sum(
              torch.stack(
                  [
                      torch.sum(g * x)
                      for g, x in zip(f_grads, unflatten_vector)
                  ]
              )
          )
  
          hvp = list(
              torch.autograd.grad(
                  grad_vector_product, params, retain_graph=True
              )
          )
          for i, (hx, p) in enumerate(zip(hvp, params)):
              if hx is None:
                  hvp[i] = torch.zeros_like(p)
  
          flat_output = torch.cat([h.reshape(-1) for h in hvp])
          return flat_output + reg_coeff * vector
  
      return _eval
  
file_path: garage/src/garage/torch/optimizers/conjugate_gradient_optimizer.py
imports: import warnings
  from dowel import logger
  import numpy
  import torch
  from torch.optim import Optimizer
  from garage.np import unflatten_tensors
label_desc: Use image node_id 1 for calling a global function with example usage: _build_hessian_vector_product(func, params, reg_coeff) and returns: _eval, unknown