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","."]
code: def _backtracking_line_search(
      self, params, descent_step, f_loss, f_constraint
  ):
      prev_params = [p.clone() for p in params]
      ratio_list = self._backtrack_ratio ** np.arange(
          self._max_backtracks
      )
      loss_before = f_loss()
  
      param_shapes = [p.shape or torch.Size([1]) for p in params]
      descent_step = unflatten_tensors(descent_step, param_shapes)
      assert len(descent_step) == len(params)
  
      for ratio in ratio_list:
          for step, prev_param, param in zip(
              descent_step, prev_params, params
          ):
              step = ratio * step
              new_param = prev_param.data - step
              param.data = new_param.data
  
          loss = f_loss()
          constraint_val = f_constraint()
          if (
              loss < loss_before
              and constraint_val <= self._max_constraint_value
          ):
              break
  
      if (
          torch.isnan(loss)
          or torch.isnan(constraint_val)
          or loss >= loss_before
          or constraint_val >= self._max_constraint_value
      ) and not self._accept_violation:
          logger.log(
              "Line search condition violated. Rejecting the step!"
          )
          if torch.isnan(loss):
              logger.log("Violated because loss is NaN")
          if torch.isnan(constraint_val):
              logger.log("Violated because constraint is NaN")
          if loss >= loss_before:
              logger.log("Violated because loss not improving")
          if constraint_val >= self._max_constraint_value:
              logger.log("Violated because constraint is violated")
          for prev, cur in zip(prev_params, params):
              cur.data = prev.data
  
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 6 for calling the ConjugateGradientOptimizer obj's underlying member method code with example usage: obj._backtracking_line_search(params, descent_step, f_loss, f_constraint) without return types