code: def compute_gradient(model_fn, loss_fn, x, y, targeted):
      """
      Computes the gradient of the loss with respect to the input tensor.
      :param model_fn: a callable that takes an input tensor and returns the model logits.
      :param loss_fn: loss function that takes (labels, logits) as arguments and returns loss.
      :param x: input tensor
      :param y: Tensor with true labels. If targeted is true, then provide the target label.
      :param targeted:  bool. Is the attack targeted or untargeted? Untargeted, the default, will
                        try to make the label incorrect. Targeted will instead try to move in the
                        direction of being more like y.
      :return: A tensor containing the gradient of the loss with respect to the input tensor.
      """
  
      with tf.GradientTape() as g:
          g.watch(x)
          # Compute loss
          loss = loss_fn(labels=y, logits=model_fn(x))
          if (
              targeted
          ):  # attack is targeted, minimize loss of target label rather than maximize loss of correct label
              loss = -loss
  
      # Define gradient of loss wrt input
      grad = g.gradient(loss, x)
      return grad
  
file_path: cleverhans/cleverhans/tf2/utils.py
imports: import numpy
  import tensorflow
label_desc: Use image node_id 7 for calling a global function with example usage: compute_gradient(model_fn, loss_fn, x, y, targeted) and returns: grad