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 and arbitrary shape.
- 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.
- 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
>>> 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.]]]