code: def get_grad_or_attr(
      model,
      layer_name,
      prev_layer_name,
      obses,
      *,
      act_dir=None,
      act_poses=None,
      score_fn=default_score_fn,
      grad_or_attr,
      override=None,
      integrate_steps=1,
  ):
      with tf.Graph().as_default(), tf.Session(), gradient_override_map(
          override or {}
      ):
          t_obses = tf.placeholder_with_default(
              obses.astype(np.float32), (None, None, None, None)
          )
          T = render.import_model(model, t_obses, t_obses)
          t_acts = T(layer_name)
          if prev_layer_name is None:
              t_acts_prev = t_obses
          else:
              t_acts_prev = T(prev_layer_name)
          if act_dir is not None:
              t_acts = act_dir[None, None, None] * t_acts
          if act_poses is not None:
              t_acts = tf.gather_nd(
                  t_acts,
                  tf.concat(
                      [tf.range(obses.shape[0])[..., None], act_poses],
                      axis=-1,
                  ),
              )
          t_scores = score_fn(t_acts)
          assert (
              len(t_scores.shape) >= 1
          ), "score_fn should not reduce the batch dim"
          t_score = tf.reduce_sum(t_scores)
          t_grad = tf.gradients(t_score, [t_acts_prev])[0]
          if integrate_steps > 1:
              acts_prev = t_acts_prev.eval()
              grad = (
                  sum(
                      [
                          t_grad.eval(
                              feed_dict={t_acts_prev: acts_prev * alpha}
                          )
                          for alpha in np.linspace(
                              0, 1, integrate_steps + 1
                          )[1:]
                      ]
                  )
                  / integrate_steps
              )
          else:
              acts_prev = None
              grad = t_grad.eval()
          if grad_or_attr == "grad":
              return grad
          elif grad_or_attr == "attr":
              if acts_prev is None:
                  acts_prev = t_acts_prev.eval()
              return acts_prev * grad
          else:
              raise NotImplementedError
  
file_path: lucid/lucid/scratch/rl_util/attribution.py
imports: import numpy
  import tensorflow
  import lucid.optvis.render
  import itertools
  from lucid.misc.gradient_override import gradient_override_map
label_desc: Use image node_id 4 for calling a global function with example usage: get_grad_or_attr(model, layer_name, prev_layer_name, obses) and returns: grad, unknown