class_docstr: This class implements a classifier with the TensorFlow v2 framework.
class_docstr_tok: ["This","class","implements","a","classifier","with","the","TensorFlow","v2","framework","."]
docstr: Compute the gradient of the loss function w.r.t. `x`.
  
  :param x: Sample input with shape as expected by the model.
  :param y: Correct labels, one-vs-rest encoding.
  :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
  :return: Array of gradients of the same shape as `x`.
docstr_tok: ["Compute","the","gradient","of","the","loss","function","w.r.t",".","`","x","`",".",":","param","x",":","Sample","input","with","shape","as","expected","by","the","model",".",":","param","y",":","Correct","labels",",","one-vs-rest","encoding",".",":","param","training_mode",":","`","True","`","for","model","set","to","training","mode","and","`","'False","`","for","model","set","to","evaluation","mode",".",":","return",":","Array","of","gradients","of","the","same","shape","as","`","x","`","."]
code: def loss_gradient(  # pylint: disable=W0221
      self,
      x: Union[np.ndarray, "tf.Tensor"],
      y: Union[np.ndarray, "tf.Tensor"],
      training_mode: bool = False,
      **kwargs,
  ) -> Union[np.ndarray, "tf.Tensor"]:
      """
      Compute the gradient of the loss function w.r.t. `x`.
  
      :param x: Sample input with shape as expected by the model.
      :param y: Correct labels, one-vs-rest encoding.
      :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
      :return: Array of gradients of the same shape as `x`.
      """
      import tensorflow as tf
  
      if self._loss_object is None:  # pragma: no cover
          raise TypeError(
              "The loss function `loss_object` is required for computing loss gradients, but it has not been "
              "defined."
          )
  
      if tf.executing_eagerly():
          with tf.GradientTape() as tape:
              # Apply preprocessing
              if self.all_framework_preprocessing:
                  x_grad = tf.convert_to_tensor(x)
                  tape.watch(x_grad)
                  x_input, y_input = self._apply_preprocessing(
                      x_grad, y=y, fit=False
                  )
              else:
                  (
                      x_preprocessed,
                      y_preprocessed,
                  ) = self._apply_preprocessing(x, y=y, fit=False)
                  x_grad = tf.convert_to_tensor(x_preprocessed)
                  tape.watch(x_grad)
                  x_input = x_grad
                  y_input = y_preprocessed
  
              predictions = self.model(x_input, training=training_mode)
  
              if self._reduce_labels:
                  loss = self._loss_object(
                      np.argmax(y_input, axis=1), predictions
                  )
              else:
                  loss = self._loss_object(y_input, predictions)
  
          gradients = tape.gradient(loss, x_grad)
  
          if isinstance(x, np.ndarray):
              gradients = gradients.numpy()
  
      else:
          raise NotImplementedError("Expecting eager execution.")
  
      # Apply preprocessing gradients
      if not self.all_framework_preprocessing:
          gradients = self._apply_preprocessing_gradient(x, gradients)
  
      return gradients
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import os
  import random
  import shutil
  import time
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union, TYPE_CHECKING
  from tqdm.auto import tqdm
  import numpy
  import six
  from art import config
  from art.estimators.classification.classifier import ClassGradientsMixin, ClassifierMixin
  from art.estimators.tensorflow import TensorFlowEstimator, TensorFlowV2Estimator
  from art.utils import check_and_transform_label_format
label_desc: Use image node_id 13 for calling the TensorFlowV2Classifier obj's underlying member method code with example usage: obj.loss_gradient(x, y, training_mode) and returns: gradients