---
title: OnehotOperation
description: "| 硬件型号 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendtb/ascendtb_01_0068.html
sourcePath: /source/zh/canncommercial/900/API/ascendtb/ascendtb_01_0068.html
indexId: c3a7d5aa4b3f6436f8e5ba4e25cd16a58c0b1119795d71c72c9563ac4d89e24b64
---
# OnehotOperation

#### 产品支持情况

| 硬件型号 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | x |
| Atlas A3 推理系列产品 / Atlas A3 训练系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 训练系列产品 | x |
| Atlas 推理系列产品 | x |
| Atlas 200I/500 A2 推理产品 | x |


#### 功能说明

onehot编码。


机器学习的预处理编码方式的一种：onehot编码。onehot编码对depth个状态进行编码，每个状态的对应位置置1，其它位置置0；输出结果为只有一位为1且长为depth的向量。

算子输出输入tensor x各个元素对应的独热编码，结果表现为在axis位置上增加维度depth。

计算过程示意（Python）：

```
res = np.eye(depth)[input0]
```

示例1（Python）：

```
x shape: torch.Size([2, 3])
x: tensor([[4, 4, 6],
          [6, 7, 6]])
depth: 10
axis: -1

# axis为-1表示output的最后一维output[i][j][:] 是x[i][j]对应的onehot编码
out_tensor shape: torch.Size([2, 3, 10])
out_tensor: tensor([[[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]],

        [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
         [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]], device='npu:0')
```

示例2（Python）：

```
x shape: torch.Size([2, 3])
x: tensor([[2, 0, 4],
        [4, 3, 0]])
depth: 5
axis: 0

# output[:][i][j] 是x[i][j]对应的onehot编码，若axis为1则output[i][:][j] 是x[i][j]对应的onehot编码
out_tensor shape: torch.Size([5, 2, 3])
out_tensor: tensor([[[0, 1, 0],
         [0, 0, 1]],

        [[0, 0, 0],
         [0, 0, 0]],

        [[1, 0, 0],
         [0, 0, 0]],

        [[0, 0, 0],
         [0, 1, 0]],

        [[0, 0, 1],
         [1, 0, 0]]], device='npu:0')
```

#### 定义

```
struct OnehotParam {
    int64_t axis = 0;
    int64_t depth = 0;
    uint8_t rsv[8] = {0};
};
```


#### 参数列表

| 成员名称 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| axis | int64\_t | 0 | depth所在下标。可为负数，为负数表示output的倒数第axis个维度是对应输入的独热编码，为负数时只能为\-1。 |
| depth | int64\_t | 0 | 每个输入对应的独热编码长度。 |
| rsv[8] | uint8\_t | {0} | 预留参数。 |


#### 输入

| 参数 | 维度 | 数据类型 | 格式 | 描述 |
| --- | --- | --- | --- | --- |
| x | [dim\_0, dim\_1, ..., dim\_n] | int32/int64 | ND | 输入tensor，表示要获取哪些状态的独热编码。x每个元素需要大于等于0且维度不能大于7。 |
| one | [1] | int32/int64 与x相同 | ND | 标量1。类型/格式与x保持一致。传给算子用，没有实际意义。 |
| zero | [1] | int32/int64 与x相同 | ND | 标量0。类型/格式与x保持一致。传给算子用，没有实际意义。 |


#### 输出

| 参数 | 维度 | 数据类型 | 格式 | 描述 |
| --- | --- | --- | --- | --- |
| output | 和x相比，在axis上多一个depth维度。 | int32/int64 与x相同 | ND | 输出tensor。类型/格式与x保持一致，输出的独热编码。 |


#### 约束说明

- axis的绝对值小于x的维度数量。
- x中元素小于depth。
