class_docstr: This class implements a classifier with the PyTorch framework.
class_docstr_tok: ["This","class","implements","a","classifier","with","the","PyTorch","framework","."]
docstr: Reduce labels from one-hot encoded to index labels.
code: def reduce_labels(
      self, y: Union[np.ndarray, "torch.Tensor"]
  ) -> Union[np.ndarray, "torch.Tensor"]:
      """
      Reduce labels from one-hot encoded to index labels.
      """
      # pylint: disable=R0911
      import torch
  
      # Check if the loss function supports probability labels and probability labels are provided
      if self._probability_labels and len(y.shape) == 2:
          if isinstance(y, torch.Tensor):
              is_one_hot = torch.equal(
                  y.float().floor(), y.float().ceil()
              )
          else:
              is_one_hot = np.array_equal(np.floor(y), np.ceil(y))
          if not is_one_hot:  # probability labels
              return y
  
      # Check if the loss function requires as input index labels instead of one-hot-encoded labels
      # Checking for exactly 2 classes to support binary classification
      if self.nb_classes > 2 or (
          self.nb_classes == 2 and len(y.shape) == 2 and y.shape[1] == 2
      ):
          if self._reduce_labels and self._int_labels:
              if isinstance(y, torch.Tensor):
                  return torch.argmax(y, dim=1)
              return np.argmax(y, axis=1)
          if self._reduce_labels:  # float labels
              if isinstance(y, torch.Tensor):
                  return torch.argmax(y, dim=1).float()
              y_index = np.argmax(y, axis=1).astype(np.float32)
              y_index = np.expand_dims(y_index, axis=1)
              return y_index
          return y
  
      if isinstance(y, torch.Tensor):
          return y.float()
      return y.astype(np.float32)
  
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 10 for calling the PyTorchClassifier obj's underlying member method code with example usage: obj.reduce_labels(y) and returns: y, y, y, y, np, y_index, torch, torch