mindspore.dataset.transforms.OneHot

class mindspore.dataset.transforms.OneHot(num_classes, smoothing_rate=0.0)[source]

Tensor operation to apply one hot encoding.

Parameters
  • num_classes (int) – Number of classes of objects in dataset. It should be larger than the largest label number in the dataset.

  • smoothing_rate (float, optional) – Adjustable hyperparameter for label smoothing level. Default: 0.0, means no smoothing is applied.

Raises
  • TypeErrornum_classes is not of type int.

  • TypeErrorsmoothing_rate is not of type float or int.

  • ValueErrorsmoothing_rate is not in range [0.0, 1.0].

  • RuntimeError – Input tensor is not of type int.

  • RuntimeError – Input tensor is not a 1-D tensor.

Supported Platforms:

CPU

Examples

>>> import mindspore.dataset as ds
>>> import mindspore.dataset.transforms as transforms
>>>
>>> mnist_dataset_dir = "/path/to/mnist_dataset_directory"
>>> mnist_dataset = ds.MnistDataset(dataset_dir=mnist_dataset_dir)
>>>
>>> # Assume that dataset has 10 classes, thus the label ranges from 0 to 9
>>> onehot_op = transforms.OneHot(num_classes=10)
>>> mnist_dataset = mnist_dataset.map(operations=onehot_op, input_columns=["label"])