class_docstr: This class implements a regressor with the PyTorch framework.
class_docstr_tok: ["This","class","implements","a","regressor","with","the","PyTorch","framework","."]
docstr: Initialization specifically for the PyTorch-based implementation.
  
  :param model: PyTorch model. The output of the model can be logits, probabilities or anything else. Logits
         output should be preferred where possible to ensure attack efficiency.
  :param loss: The loss function for which to compute gradients for training. The target label must be raw
         categorical, i.e. not converted to one-hot encoding.
  :param input_shape: The shape of one input instance.
  :param optimizer: The optimizer used to train the regressor.
  :param use_amp: Whether to use the automatic mixed precision tool to enable mixed precision training or
                  gradient computation, e.g. with loss gradient computation. When set to True, this option is
                  only triggered if there are GPUs available.
  :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted
                    values are `O0`, `O1`, `O2`, and `O3`.
  :param loss_scale: Loss scaling. Used when use_amp is True. If passed as a string, must be a string
                     representing a number, e.g., “1.0”, or the string “dynamic”.
  :param optimizer: The optimizer used to train the regressor.
  :param channels_first: Set channels first or last.
  :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
         maximum values allowed for features. If floats are provided, these will be used as the range of all
         features. If arrays are provided, each value will be considered the bound for a feature, thus
         the shape of clip values needs to match the total number of features.
  :param preprocessing_defences: Preprocessing defence(s) to be applied by the regressor.
  :param postprocessing_defences: Postprocessing defence(s) to be applied by the regressor.
  :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
         used for data preprocessing. The first value will be subtracted from the input. The input will then
         be divided by the second one.
  :param device_type: Type of device on which the regressor is run, either `gpu` or `cpu`.
docstr_tok: ["Initialization","specifically","for","the","PyTorch-based","implementation",".",":","param","model",":","PyTorch","model",".","The","output","of","the","model","can","be","logits",",","probabilities","or","anything","else",".","Logits","output","should","be","preferred","where","possible","to","ensure","attack","efficiency",".",":","param","loss",":","The","loss","function","for","which","to","compute","gradients","for","training",".","The","target","label","must","be","raw","categorical",",","i.e",".","not","converted","to","one-hot","encoding",".",":","param","input_shape",":","The","shape","of","one","input","instance",".",":","param","optimizer",":","The","optimizer","used","to","train","the","regressor",".",":","param","use_amp",":","Whether","to","use","the","automatic","mixed","precision","tool","to","enable","mixed","precision","training","or","gradient","computation",",","e.g",".","with","loss","gradient","computation",".","When","set","to","True",",","this","option","is","only","triggered","if","there","are","GPUs","available",".",":","param","opt_level",":","Specify","a","pure","or","mixed","precision","optimization","level",".","Used","when","use_amp","is","True",".","Accepted","values","are","`","O0","`",",","`","O1","`",",","`","O2","`",",","and","`","O3","`",".",":","param","loss_scale",":","Loss","scaling",".","Used","when","use_amp","is","True",".","If","passed","as","a","string",",","must","be","a","string","representing","a","number",",","e.g.",",","\u201c","1.0","\u201d",",","or","the","string","\u201c","dynamic","\u201d",".",":","param","optimizer",":","The","optimizer","used","to","train","the","regressor",".",":","param","channels_first",":","Set","channels","first","or","last",".",":","param","clip_values",":","Tuple","of","the","form","`","(","min",",","max",")","`","of","floats","or","`","np.ndarray","`","representing","the","minimum","and","maximum","values","allowed","for","features",".","If","floats","are","provided",",","these","will","be","used","as","the","range","of","all","features",".","If","arrays","are","provided",",","each","value","will","be","considered","the","bound","for","a","feature",",","thus","the","shape","of","clip","values","needs","to","match","the","total","number","of","features",".",":","param","preprocessing_defences",":","Preprocessing","defence","(","s",")","to","be","applied","by","the","regressor",".",":","param","postprocessing_defences",":","Postprocessing","defence","(","s",")","to","be","applied","by","the","regressor",".",":","param","preprocessing",":","Tuple","of","the","form","`","(","subtrahend",",","divisor",")","`","of","floats","or","`","np.ndarray","`","of","values","to","be","used","for","data","preprocessing",".","The","first","value","will","be","subtracted","from","the","input",".","The","input","will","then","be","divided","by","the","second","one",".",":","param","device_type",":","Type","of","device","on","which","the","regressor","is","run",",","either","`","gpu","`","or","`","cpu","`","."]
code: def __init__(
      self,
      model: "torch.nn.Module",
      loss: "torch.nn.modules.loss._Loss",
      input_shape: Tuple[int, ...],
      optimizer: Optional["torch.optim.Optimizer"] = None,  # type: ignore
      use_amp: bool = False,
      opt_level: str = "O1",
      loss_scale: Optional[Union[float, str]] = "dynamic",
      channels_first: bool = True,
      clip_values: Optional["CLIP_VALUES_TYPE"] = None,
      preprocessing_defences: Union[
          "Preprocessor", List["Preprocessor"], None
      ] = None,
      postprocessing_defences: Union[
          "Postprocessor", List["Postprocessor"], None
      ] = None,
      preprocessing: "PREPROCESSING_TYPE" = (0.0, 1.0),
      device_type: str = "gpu",
  ) -> None:
      """
      Initialization specifically for the PyTorch-based implementation.
  
      :param model: PyTorch model. The output of the model can be logits, probabilities or anything else. Logits
             output should be preferred where possible to ensure attack efficiency.
      :param loss: The loss function for which to compute gradients for training. The target label must be raw
             categorical, i.e. not converted to one-hot encoding.
      :param input_shape: The shape of one input instance.
      :param optimizer: The optimizer used to train the regressor.
      :param use_amp: Whether to use the automatic mixed precision tool to enable mixed precision training or
                      gradient computation, e.g. with loss gradient computation. When set to True, this option is
                      only triggered if there are GPUs available.
      :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted
                        values are `O0`, `O1`, `O2`, and `O3`.
      :param loss_scale: Loss scaling. Used when use_amp is True. If passed as a string, must be a string
                         representing a number, e.g., “1.0”, or the string “dynamic”.
      :param optimizer: The optimizer used to train the regressor.
      :param channels_first: Set channels first or last.
      :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
             maximum values allowed for features. If floats are provided, these will be used as the range of all
             features. If arrays are provided, each value will be considered the bound for a feature, thus
             the shape of clip values needs to match the total number of features.
      :param preprocessing_defences: Preprocessing defence(s) to be applied by the regressor.
      :param postprocessing_defences: Postprocessing defence(s) to be applied by the regressor.
      :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
             used for data preprocessing. The first value will be subtracted from the input. The input will then
             be divided by the second one.
      :param device_type: Type of device on which the regressor is run, either `gpu` or `cpu`.
      """
      import torch
  
      super().__init__(
          model=model,
          clip_values=clip_values,
          channels_first=channels_first,
          preprocessing_defences=preprocessing_defences,
          postprocessing_defences=postprocessing_defences,
          preprocessing=preprocessing,
          device_type=device_type,
      )
      self._input_shape = input_shape
      self._model = self._make_model_wrapper(model)
      self._loss = loss
      self._optimizer = optimizer
      self._use_amp = use_amp
      self._learning_phase: Optional[bool] = None
      self._opt_level = opt_level
      self._loss_scale = loss_scale
  
      # Check if model is RNN-like to decide if freezing batch-norm and dropout layers might be required for loss and
      # class gradient calculation
      self.is_rnn = any(
          (
              isinstance(m, torch.nn.modules.RNNBase)
              for m in self._model.modules()
          )
      )
  
      # Get the internal layers
      self._layer_names: List[str] = self._model.get_layers  # type: ignore
  
      self._model.to(self._device)
  
      # Index of layer at which the class gradients should be calculated
      self._layer_idx_gradients = -1
  
      # Setup for AMP use
      if self._use_amp:  # pragma: no cover
          from apex import amp  # pylint: disable=E0611
  
          if self._optimizer is None:
              logger.warning(
                  "An optimizer is needed to use the automatic mixed precision tool, but none for provided. "
                  "A default optimizer is used."
              )
  
              # Create the optimizers
              parameters = self._model.parameters()
              self._optimizer = torch.optim.SGD(parameters, lr=0.01)
  
          if self.device.type == "cpu":
              enabled = False
          else:
              enabled = True
  
          self._model, self._optimizer = amp.initialize(
              models=self._model,
              optimizers=self._optimizer,
              enabled=enabled,
              opt_level=opt_level,
              loss_scale=loss_scale,
          )
  
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
  import numpy
  import six
  from art import config
  from art.estimators.regression.regressor import RegressorMixin
  from art.estimators.pytorch import PyTorchEstimator
label_desc: Use image node_id 1 to create a new PyTorchRegressor object from inherited base classes: RegressorMixin, PyTorchEstimator with example: obj = PyTorchRegressor(model, loss, input_shape, optimizer, use_amp, opt_level, loss_scale, channels_first, clip_values, preprocessing_defences, postprocessing_defences, preprocessing, device_type)