class_docstr: This class implements a regressor with the PyTorch framework.
class_docstr_tok: ["This","class","implements","a","regressor","with","the","PyTorch","framework","."]
code: def _make_model_wrapper(
      self, model: "torch.nn.Module"
  ) -> "torch.nn.Module":
      # Try to import PyTorch and create an internal class that acts like a model wrapper extending torch.nn.Module
      try:
          import torch
  
          # Define model wrapping class only if not defined before
          if not hasattr(self, "_model_wrapper"):
  
              class ModelWrapper(torch.nn.Module):
                  """
                  This is a wrapper for the input model.
                  """
  
                  import torch
  
                  def __init__(self, model: torch.nn.Module):
                      """
                      Initialization by storing the input model.
  
                      :param model: PyTorch model. The forward function of the model must return the logit output.
                      """
                      super().__init__()
                      self._model = model
  
                  # pylint: disable=W0221
                  # disable pylint because of API requirements for function
                  def forward(self, x):
                      """
                      This is where we get outputs from the input model.
  
                      :param x: Input data.
                      :type x: `torch.Tensor`
                      :return: a list of output layers, where the last 2 layers are logit and final outputs.
                      :rtype: `list`
                      """
                      # pylint: disable=W0212
                      # disable pylint because access to _model required
  
                      result = []
                      if isinstance(self._model, torch.nn.Sequential):
                          for (
                              _,
                              module_,
                          ) in self._model._modules.items():
                              x = module_(x)
                              result.append(x)
  
                      elif isinstance(self._model, torch.nn.Module):
                          x = self._model(x)
                          result.append(x)
  
                      else:  # pragma: no cover
                          raise TypeError(
                              "The input model must inherit from `nn.Module`."
                          )
  
                      return result
  
                  @property
                  def get_layers(self) -> List[str]:
                      """
                      Return the hidden layers in the model, if applicable.
  
                      :return: The hidden layers in the model, input and output layers excluded.
  
                      .. warning:: `get_layers` tries to infer the internal structure of the model.
                                   This feature comes with no guarantees on the correctness of the result.
                                   The intended order of the layers tries to match their order in the model, but this
                                   is not guaranteed either. In addition, the function can only infer the internal
                                   layers if the input model is of type `nn.Sequential`, otherwise, it will only
                                   return the logit layer.
                      """
                      import torch
  
                      result = []
                      if isinstance(self._model, torch.nn.Module):
                          for (
                              name,
                              _,
                          ) in (
                              self._model._modules.items()
                          ):  # pylint: disable=W0212
                              result.append(name)
  
                      else:  # pragma: no cover
                          raise TypeError(
                              "The input model must inherit from `nn.Module`."
                          )
                      logger.info(
                          "Inferred %i hidden layers on PyTorch regressor.",
                          len(result),
                      )
  
                      return result
  
              # Set newly created class as private attribute
              self._model_wrapper = ModelWrapper
  
          # Use model wrapping class to wrap the PyTorch model received as argument
          return self._model_wrapper(model)
  
      except ImportError:  # pragma: no cover
          raise ImportError(
              "Could not find PyTorch (`torch`) installation."
          ) from ImportError
  
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 26 for calling the PyTorchRegressor obj's underlying member method code with example usage: obj._make_model_wrapper(model) and returns: self, result, result