mindspore.nn.OneHot

class mindspore.nn.OneHot(axis=-1, depth=1, on_value=1.0, off_value=0.0, dtype=mstype.float32)[source]

Returns a one-hot tensor.

The locations represented by indices in argument indices take value on_value, while all other locations take value off_value.

Note

If the input indices is rank \(N\), the output will have rank \(N+1\). The new axis is created at dimension axis.

If indices is a scalar, the output shape will be a vector of length depth.

If indices is a vector of length features, the output shape will be:

features * depth if axis == -1

depth * features if axis == 0

If indices is a matrix with shape [batch, features], the output shape will be:

batch * features * depth if axis == -1

batch * depth * features if axis == 1

depth * batch * features if axis == 0
Parameters
  • axis (int) – Features x depth if axis is -1, depth x features if axis is 0. Default: -1.

  • depth (int) – A scalar defining the depth of the one hot dimension. Default: 1.

  • on_value (float) – A scalar defining the value to fill in output[i][j] when indices[j] = i. Default: 1.0.

  • off_value (float) – A scalar defining the value to fill in output[i][j] when indices[j] != i. Default: 0.0.

  • dtype (mindspore.dtype) – Data type of ‘on_value’ and ‘off_value’, not the data type of indices. Default: mindspore.float32.

Inputs:
  • indices (Tensor) - A tensor of indices with data type of int32 or int64. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, the one-hot tensor of data type dtype with dimension at axis expanded to depth and filled with on_value and off_value. The dimension of the Outputs is equal to the dimension of the indices plus one.

Raises
  • TypeError – If axis or depth is not an int.

  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If axis is not in range [-1, len(indices_shape)].

  • ValueError – If depth is less than 0.

Supported Platforms:

Ascend GPU CPU

Examples

>>> # 1st sample: add new coordinates at axis 1
>>> net = nn.OneHot(depth=4, axis=1)
>>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
>>> output = net(indices)
>>> print(output)
[[[0. 0.]
  [1. 0.]
  [0. 0.]
  [0. 1.]]
 [[1. 0.]
  [0. 0.]
  [0. 1.]
  [0. 0.]]]
>>> # The results are shown below:
>>> print(output.shape)
(2, 4, 2)
>>> # 2nd sample: add new coordinates at axis 0
>>> net = nn.OneHot(depth=4, axis=0)
>>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
>>> output = net(indices)
>>> print(output)
[[[0. 0.]
  [1. 0.]]
 [[1. 0.]
  [0. 0.]]
 [[0. 0.]
  [0. 1.]]
 [[0. 1.]
  [0. 0.]]]
>>> # The results are shown below:
>>> print(output.shape)
(4, 2, 2)
>>> # 3rd sample: add new coordinates at the last dimension.
>>> net = nn.OneHot(depth=4, axis=-1)
>>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
>>> output = net(indices)
>>> # The results are shown below:
>>> print(output)
[[[0. 1. 0. 0.]
  [0. 0. 0. 1.]]
 [[1. 0. 0. 0.]
  [0. 0. 1. 0.]]]
>>> print(output.shape)
(2, 2, 4)
>>> indices = Tensor([1, 3, 0, 2], dtype=mindspore.int32)
>>> output = net(indices)
>>> print(output)
[[0. 1. 0. 0.]
 [0. 0. 0. 1.]
 [1. 0. 0. 0.]
 [0. 0. 1. 0.]]
>>> print(output.shape)
(4, 4)