mindspore.ops.one_hot
- mindspore.ops.one_hot(indices, depth, on_value=1, off_value=0, axis=- 1)[源代码]
生成一个新的tensor,索引 indices 表示的位置上取值 on_value ,其他所有位置上取值 off_value 。
说明
如果 indices 的秩为 n ,则输出Tensor的秩为 n+1 。新轴在 axis 处创建。当执行设备是 Ascend 时,如果 on_value 为int64类型,则 indices 也必须为int64类型,且 on_value 和 off_value 的取值只能是1和0。
- 参数:
indices (Tensor) - 输入索引。
depth (int) - one-hot深度。
on_value (Union[Tensor, int, float],可选) - 填充索引位置上的值。默认
1
。off_value (Union[Tensor, int, float],可选) - 填充非索引位置上的值。默认
0
。axis (int,可选) - 指定计算轴。默认
-1
。
- 返回:
Tensor
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> indices = mindspore.tensor([0, 1, 2, 4]) >>> mindspore.ops.one_hot(indices, depth=5) Tensor(shape=[4, 5], dtype=Int64, value= [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 1]]) >>> >>> mindspore.ops.one_hot(indices, depth=3) Tensor(shape=[4, 3], dtype=Int64, value= [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) >>> # If shape of indices is (N, C), and axis=-1, the returned shape will be (N, C, depth). >>> indices = mindspore.tensor([[0, 2], [1, -1]]) >>> mindspore.ops.one_hot(indices, depth=3, on_value=10, off_value=4, axis=-1) Tensor(shape=[2, 2, 3], dtype=Int64, value= [[[10, 4, 4], [ 4, 4, 10]], [[ 4, 10, 4], [ 4, 4, 4]]]) >>> # If axis=0, the returned shape will be (depth, N, C). >>> mindspore.ops.one_hot(indices, depth=3, on_value=10, off_value=4, axis=0) Tensor(shape=[3, 2, 2], dtype=Int64, value= [[[10, 4], [ 4, 4]], [[ 4, 4], [10, 4]], [[ 4, 10], [ 4, 4]]])