class_docstr: Our Method: Double Noise Mechanism
docstr: computing direction
docstr_tok: ["computing","direction"]
code: def noisy_direction(self, w_cur, lrp, hyper_dict, noisy_grad):
      """computing direction"""
      total = hyper_dict["total"]
      grad_frac = hyper_dict["grad_frac"]
      frac_trace = hyper_dict["trace_frac"]
      trace_coeff = hyper_dict["trace_coeff"]
      iters = hyper_dict["num_iteration"]
      rho_hess = (1 - grad_frac) * total / iters
      smooth_param = 0.25
      hess_cur = self.hess(w_cur)
      noisy_trace = trace_coeff * max(
          np.trace(hess_cur)
          + np.random.normal(
              scale=(0.25 / lrp.num_samples)
              * np.sqrt(0.5 / (frac_trace * rho_hess))
          ),
          0,
      )
      min_eval = max(
          (
              noisy_trace
              / ((lrp.num_samples) ** 2 * (1 - frac_trace) * rho_hess)
          )
          ** (1 / 3),
          1 / (lrp.num_samples),
      )
      grad_norm = np.linalg.norm(noisy_grad)
      if (
          self.type_reg == "add"
      ):  # Sensitivity is different for add vs clip
          sens2 = (
              grad_norm
              * smooth_param
              / (
                  lrp.num_samples * min_eval**2
                  + smooth_param * min_eval
              )
          )
          noise2 = np.random.normal(
              scale=sens2
              * np.sqrt(0.5 / ((1 - frac_trace) * rho_hess)),
              size=lrp.dim,
          )
          return (
              w_cur
              - np.linalg.solve(
                  hess_cur + min_eval * np.eye(lrp.dim), noisy_grad
              )
              + noise2
          )
      # type_reg=clip
      sens2 = (
          grad_norm
          * smooth_param
          / (lrp.num_samples * min_eval**2 - smooth_param * min_eval)
      )
      noise2 = np.random.normal(
          scale=sens2 * np.sqrt(0.5 / ((1 - frac_trace) * rho_hess)),
          size=lrp.dim,
      )
      eval_hess, evec_hess = np.linalg.eigh(hess_cur)
      eval_trunc = eval_hess[eval_hess >= min_eval]
      num_eig = len(eval_trunc)
      if num_eig == 0:
          hess_modified_inv = 1 / min_eval * np.eye(lrp.dim)
      else:
          evec_trun = evec_hess[:, -num_eig:]
          hess_modified_inv = np.dot(
              evec_trun
              * (1 / eval_trunc - 1 / min_eval * np.ones(num_eig)),
              evec_trun.T,
          ) + 1 / min_eval * np.eye(lrp.dim)
      return w_cur - (hess_modified_inv @ noisy_grad) + noise2
  
imports: import time
  from my_logistic_regression import MyLogisticRegression
  import numpy
label_desc: Use image node_id 5 for calling the DoubleNoiseMech obj's underlying member method code with example usage: obj.noisy_direction(w_cur, lrp, hyper_dict, noisy_grad) and returns: unknown, unknown