class_docstr: This class implements a classifier with the TensorFlow framework.
class_docstr_tok: ["This","class","implements","a","classifier","with","the","TensorFlow","framework","."]
docstr: Compute per-class derivatives w.r.t. `x`.
  
  :param x: Sample input with shape as expected by the model.
  :param label: Index of a specific per-class derivative. If an integer is provided, the gradient of that class
                output is computed for all samples. If multiple values as provided, the first dimension should
                match the batch size of `x`, and each value will be used as target for its corresponding sample in
                `x`. If `None`, then gradients for all classes will be computed for each sample.
  :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
  :return: Array of gradients of input features w.r.t. each class in the form
           `(batch_size, nb_classes, input_shape)` when computing for all classes, otherwise shape becomes
           `(batch_size, 1, input_shape)` when `label` parameter is specified.
docstr_tok: ["Compute","per-class","derivatives","w.r.t",".","`","x","`",".",":","param","x",":","Sample","input","with","shape","as","expected","by","the","model",".",":","param","label",":","Index","of","a","specific","per-class","derivative",".","If","an","integer","is","provided",",","the","gradient","of","that","class","output","is","computed","for","all","samples",".","If","multiple","values","as","provided",",","the","first","dimension","should","match","the","batch","size","of","`","x","`",",","and","each","value","will","be","used","as","target","for","its","corresponding","sample","in","`","x","`",".","If","`","None","`",",","then","gradients","for","all","classes","will","be","computed","for","each","sample",".",":","param","training_mode",":","`","True","`","for","model","set","to","training","mode","and","`","'False","`","for","model","set","to","evaluation","mode",".",":","return",":","Array","of","gradients","of","input","features","w.r.t",".","each","class","in","the","form","`","(","batch_size",",","nb_classes",",","input_shape",")","`","when","computing","for","all","classes",",","otherwise","shape","becomes","`","(","batch_size",",","1",",","input_shape",")","`","when","`","label","`","parameter","is","specified","."]
code: def class_gradient(  # pylint: disable=W0221
      self,
      x: np.ndarray,
      label: Optional[Union[int, List[int], np.ndarray]] = None,
      training_mode: bool = False,
      **kwargs,
  ) -> np.ndarray:
      """
      Compute per-class derivatives w.r.t. `x`.
  
      :param x: Sample input with shape as expected by the model.
      :param label: Index of a specific per-class derivative. If an integer is provided, the gradient of that class
                    output is computed for all samples. If multiple values as provided, the first dimension should
                    match the batch size of `x`, and each value will be used as target for its corresponding sample in
                    `x`. If `None`, then gradients for all classes will be computed for each sample.
      :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
      :return: Array of gradients of input features w.r.t. each class in the form
               `(batch_size, nb_classes, input_shape)` when computing for all classes, otherwise shape becomes
               `(batch_size, 1, input_shape)` when `label` parameter is specified.
      """
      if self.learning is not None:
          self.feed_dict[self.learning] = training_mode
  
      # Check value of label for computing gradients
      if not (  # pragma: no cover
          label is None
          or (
              isinstance(label, int) and label in range(self.nb_classes)
          )
          or (
              isinstance(label, np.ndarray)
              and len(label.shape) == 1
              and (label < self.nb_classes).all()
              and label.shape[0] == x.shape[0]
          )
      ):
          raise ValueError(f"Label {label} is out of range.")
  
      self._init_class_grads(label=label)
  
      # Apply preprocessing
      x_preprocessed, _ = self._apply_preprocessing(
          x, y=None, fit=False
      )
  
      # Create feed_dict
      feed_dict = {self.input_ph: x_preprocessed}
      feed_dict.update(self.feed_dict)
  
      # Compute the gradient and return
      if label is None:
          # Compute the gradients w.r.t. all classes
          grads = self._sess.run(self._class_grads, feed_dict=feed_dict)
          grads = np.swapaxes(np.array(grads), 0, 1)
  
      elif isinstance(label, int):
          # Compute the gradients only w.r.t. the provided label
          grads = self._sess.run(
              self._class_grads[label], feed_dict=feed_dict
          )
          grads = grads[None, ...]
          grads = np.swapaxes(np.array(grads), 0, 1)
  
      else:
          # For each sample, compute the gradients w.r.t. the indicated target class (possibly distinct)
          unique_label = list(np.unique(label))
          grads = self._sess.run(
              [self._class_grads[ul] for ul in unique_label],
              feed_dict=feed_dict,
          )
          grads = np.swapaxes(np.array(grads), 0, 1)
          lst = [unique_label.index(i) for i in label]
          grads = np.expand_dims(
              grads[np.arange(len(grads)), lst], axis=1
          )
  
      grads = self._apply_preprocessing_gradient(x, grads)
  
      return grads
  
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 TensorFlowClassifier obj's underlying member method code with example usage: obj.class_gradient(x, label, training_mode) and returns: grads