class_docstr: Performs constrained optimization via backtracking line search.
  
  The search direction is computed using a conjugate gradient algorithm,
  which gives x = A^{-1}g, where A is a second order approximation of the
  constraint and g is the gradient of the loss function.
  
  Args:
      params (iterable): Iterable of parameters to optimize.
      max_constraint_value (float): Maximum constraint value.
      cg_iters (int): The number of CG iterations used to calculate A^-1 g
      max_backtracks (int): Max number of iterations for backtrack
          linesearch.
      backtrack_ratio (float): backtrack ratio for backtracking line search.
      hvp_reg_coeff (float): A small value so that A -> A + reg*I. It is
          used by Hessian Vector Product calculation.
      accept_violation (bool): whether to accept the descent step if it
          violates the line search condition after exhausting all
          backtracking budgets.
class_docstr_tok: ["Performs","constrained","optimization","via","backtracking","line","search",".","The","search","direction","is","computed","using","a","conjugate","gradient","algorithm",",","which","gives","x","=","A^","{","-1","}","g",",","where","A","is","a","second","order","approximation","of","the","constraint","and","g","is","the","gradient","of","the","loss","function",".","Args",":","params","(","iterable",")",":","Iterable","of","parameters","to","optimize",".","max_constraint_value","(","float",")",":","Maximum","constraint","value",".","cg_iters","(","int",")",":","The","number","of","CG","iterations","used","to","calculate","A^-1","g","max_backtracks","(","int",")",":","Max","number","of","iterations","for","backtrack","linesearch",".","backtrack_ratio","(","float",")",":","backtrack","ratio","for","backtracking","line","search",".","hvp_reg_coeff","(","float",")",":","A","small","value","so","that","A","-",">","A","+","reg","*","I",".","It","is","used","by","Hessian","Vector","Product","calculation",".","accept_violation","(","bool",")",":","whether","to","accept","the","descent","step","if","it","violates","the","line","search","condition","after","exhausting","all","backtracking","budgets","."]
docstr: Take an optimization step.
  
  Args:
      f_loss (callable): Function to compute the loss.
      f_constraint (callable): Function to compute the constraint value.
docstr_tok: ["Take","an","optimization","step",".","Args",":","f_loss","(","callable",")",":","Function","to","compute","the","loss",".","f_constraint","(","callable",")",":","Function","to","compute","the","constraint","value","."]
code: def step(
      self, f_loss, f_constraint
  ):  # pylint: disable=arguments-differ
      """Take an optimization step.
  
      Args:
          f_loss (callable): Function to compute the loss.
          f_constraint (callable): Function to compute the constraint value.
  
      """
      # Collect trainable parameters and gradients
      params = []
      grads = []
      for group in self.param_groups:
          for p in group["params"]:
              if p.grad is not None:
                  params.append(p)
                  grads.append(p.grad.reshape(-1))
      flat_loss_grads = torch.cat(grads)
  
      # Build Hessian-vector-product function
      f_Ax = _build_hessian_vector_product(
          f_constraint, params, self._hvp_reg_coeff
      )
  
      # Compute step direction
      step_dir = _conjugate_gradient(
          f_Ax, flat_loss_grads, self._cg_iters
      )
  
      # Replace nan with 0.
      step_dir[step_dir.ne(step_dir)] = 0.0
  
      # Compute step size
      step_size = np.sqrt(
          2.0
          * self._max_constraint_value
          * (1.0 / (torch.dot(step_dir, f_Ax(step_dir)) + 1e-8))
      )
  
      if np.isnan(step_size):
          step_size = 1.0
  
      descent_step = step_size * step_dir
  
      # Update parameters using backtracking line search
      self._backtracking_line_search(
          params, descent_step, f_loss, f_constraint
      )
  
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 2 for calling the ConjugateGradientOptimizer obj's underlying member method code with example usage: obj.step(f_loss, f_constraint) without return types