class_docstr: This class implements a regressor with the PyTorch framework.
class_docstr_tok: ["This","class","implements","a","regressor","with","the","PyTorch","framework","."]
docstr: Compute the loss.
  
  :param x: Sample input with shape as expected by the model.
  :param y: Target values (class labels) one-hot-encoded of shape `(nb_samples, nb_classes)` or indices
            of shape `(nb_samples,)`.
  :param reduction: Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'.
             'none': no reduction will be applied
             'mean': the sum of the output will be divided by the number of elements in the output,
             'sum': the output will be summed.
  :return: Array of losses of the same shape as `x`.
docstr_tok: ["Compute","the","loss",".",":","param","x",":","Sample","input","with","shape","as","expected","by","the","model",".",":","param","y",":","Target","values","(","class","labels",")","one-hot-encoded","of","shape","`","(","nb_samples",",","nb_classes",")","`","or","indices","of","shape","`","(","nb_samples",",",")","`",".",":","param","reduction",":","Specifies","the","reduction","to","apply","to","the","output",":","'none","'","|","'mean","'","|","'sum","'",".","'none","'",":","no","reduction","will","be","applied","'mean","'",":","the","sum","of","the","output","will","be","divided","by","the","number","of","elements","in","the","output",",","'sum","'",":","the","output","will","be","summed",".",":","return",":","Array","of","losses","of","the","same","shape","as","`","x","`","."]
code: def compute_loss(  # type: ignore # pylint: disable=W0221
      self,
      x: Union[np.ndarray, "torch.Tensor"],
      y: Union[np.ndarray, "torch.Tensor"],
      reduction: str = "none",
      **kwargs,
  ) -> Union[np.ndarray, "torch.Tensor"]:
      """
      Compute the loss.
  
      :param x: Sample input with shape as expected by the model.
      :param y: Target values (class labels) one-hot-encoded of shape `(nb_samples, nb_classes)` or indices
                of shape `(nb_samples,)`.
      :param reduction: Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'.
                 'none': no reduction will be applied
                 'mean': the sum of the output will be divided by the number of elements in the output,
                 'sum': the output will be summed.
      :return: Array of losses of the same shape as `x`.
      """
      import torch
  
      self._model.eval()
  
      # Apply preprocessing
      x_preprocessed, y_preprocessed = self._apply_preprocessing(
          x, y, fit=False
      )
  
      # Check label shape
      # y_preprocessed = self.reduce_labels(y_preprocessed)
  
      if isinstance(x, torch.Tensor):
          inputs_t = x_preprocessed
          labels_t = y_preprocessed
      else:
          # Convert the inputs to Tensors
          inputs_t = torch.from_numpy(x_preprocessed).to(self._device)
          # Convert the labels to Tensors
          labels_t = torch.from_numpy(y_preprocessed).to(self._device)
  
      # Compute the loss and return
      model_outputs = self._model(inputs_t)
      prev_reduction = self._loss.reduction
  
      # Return individual loss values
      self._loss.reduction = reduction
      loss = self._loss(
          model_outputs[-1].reshape(
              -1,
          ),
          labels_t,
      )
      self._loss.reduction = prev_reduction
  
      if isinstance(x, torch.Tensor):
          return loss
  
      return loss.detach().cpu().numpy()
  
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 16 for calling the PyTorchRegressor obj's underlying member method code with example usage: obj.compute_loss(x, y, reduction) and returns: loss, loss