class_docstr: This class implements a classifier with the PyTorch framework.
class_docstr_tok: ["This","class","implements","a","classifier","with","the","PyTorch","framework","."]
docstr: Return the output of the specified layer for input `x`. `layer` is specified by layer index (between 0 and
  `nb_layers - 1`) or by name. The number of layers can be determined by counting the results returned by
  calling `layer_names`.
  
  :param x: Input for computing the activations.
  :param layer: Layer for computing the activations
  :param batch_size: Size of batches.
  :param framework: If true, return the intermediate tensor representation of the activation.
  :return: The output of `layer`, where the first dimension is the batch size corresponding to `x`.
code: def get_activations(  # type: ignore
      self,
      x: Union[np.ndarray, "torch.Tensor"],
      layer: Optional[Union[int, str]] = None,
      batch_size: int = 128,
      framework: bool = False,
  ) -> Union[np.ndarray, "torch.Tensor"]:
      """
      Return the output of the specified layer for input `x`. `layer` is specified by layer index (between 0 and
      `nb_layers - 1`) or by name. The number of layers can be determined by counting the results returned by
      calling `layer_names`.
  
      :param x: Input for computing the activations.
      :param layer: Layer for computing the activations
      :param batch_size: Size of batches.
      :param framework: If true, return the intermediate tensor representation of the activation.
      :return: The output of `layer`, where the first dimension is the batch size corresponding to `x`.
      """
      import torch
  
      self._model.eval()
  
      # Apply defences
      if framework:
          no_grad = False
      else:
          no_grad = True
      x_preprocessed, _ = self._apply_preprocessing(
          x=x, y=None, fit=False, no_grad=no_grad
      )
  
      # Get index of the extracted layer
      if isinstance(layer, six.string_types):
          if layer not in self._layer_names:  # pragma: no cover
              raise ValueError(f"Layer name {layer} not supported")
          layer_index = self._layer_names.index(layer)
  
      elif isinstance(layer, int):
          layer_index = layer
  
      else:  # pragma: no cover
          raise TypeError("Layer must be of type str or int")
  
      def get_feature(name):
          # the hook signature
          def hook(model, input, output):  # pylint: disable=W0622,W0613
              self._features[name] = output
  
          return hook
  
      if not hasattr(self, "_features"):
          self._features: Dict[str, torch.Tensor] = {}
          # register forward hooks on the layers of choice
  
      if layer not in self._features:
          interim_layer = dict(
              [*self._model._model.named_modules()]
          )[  # pylint: disable=W0212,W0622,W0613
              self._layer_names[layer_index]
          ]
          interim_layer.register_forward_hook(
              get_feature(self._layer_names[layer_index])
          )
  
      if framework:
          if isinstance(x_preprocessed, torch.Tensor):
              self._model(x_preprocessed)
              return self._features[self._layer_names[layer_index]]
          input_tensor = torch.from_numpy(x_preprocessed)
          self._model(input_tensor.to(self._device))
          return self._features[
              self._layer_names[layer_index]
          ]  # pylint: disable=W0212
  
      # Run prediction with batch processing
      results = []
      num_batch = int(np.ceil(len(x_preprocessed) / float(batch_size)))
  
      for m in range(num_batch):
          # Batch indexes
          begin, end = (
              m * batch_size,
              min((m + 1) * batch_size, x_preprocessed.shape[0]),
          )
  
          # Run prediction for the current batch
          self._model(
              torch.from_numpy(x_preprocessed[begin:end]).to(
                  self._device
              )
          )
          layer_output = self._features[
              self._layer_names[layer_index]
          ]  # pylint: disable=W0212
          results.append(layer_output.detach().cpu().numpy())
  
      results_array = np.concatenate(results)
  
      return results_array
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import copy
  import logging
  import os
  import time
  from typing import Any, 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.pytorch import PyTorchEstimator
  from art.utils import check_and_transform_label_format
label_desc: Use image node_id 22 for calling the PyTorchClassifier obj's underlying member method code with example usage: obj.get_activations(x, layer, batch_size, framework) and returns: results_array, hook, self, self