OnehotOperation

Applicable Products

Hardware Model

Supported

Atlas 350 accelerator cards

x

Atlas A3 inference products/Atlas A3 training products

√

Atlas A2 training products/Atlas A2 inference products

√

Atlas training products

x

Atlas inference products

x

Atlas 200I/500 A2 inference products

x

Description

One-hot encoding.

One of the preprocessing encoding modes of machine learning: onehot encoding. In one-hot encoding, depth states are encoded, a corresponding position of each state is set to 1, and other positions are set to 0. The output result is a vector with only one bit being 1 and a length being depth.

The operator outputs the one-hot encoding corresponding to each element of the input tensor x. The result is that the dimension depth is added to the axis position.

Computational process (Python):

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

Example 1 (Python):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
x shape: torch.Size([2, 3])
x: tensor([[4, 4, 6],
          [6, 7, 6]])
depth: 10
axis: -1

# If axis is -1, the last dimension output[i][j][:] is the onehot code corresponding to x[i][j].
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')

Example 2 (Python):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
x shape: torch.Size([2, 3])
x: tensor([[2, 0, 4],
        [4, 3, 0]])
depth: 5
axis: 0

# output[:][i][j] is the onehot code corresponding to x[i][j]. If axis is 1, output[i][:][j] is the onehot code corresponding to x[i][j].
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')

Definition

1
2
3
4
5
struct OnehotParam {
    int64_t axis = 0;
    int64_t depth = 0;
    uint8_t rsv[8] = {0};
};

Parameters

Member

Type

Default Value

Description

axis

int64_t

0

Index of depth. The value can be a negative number. If the value is a negative number, the last axisth dimension of output is the one-hot code of the corresponding input. If the value is a negative number, the value can only be -1.

depth

int64_t

0

Length of the onehot code corresponding to each input.

rsv[8]

uint8_t

{0}

Reserved

Input

Parameter

Dimension

Data Type

Format

Description

x

[dim_0, dim_1, ..., dim_n]

int32/int64

ND

Input tensor, indicating the states of the onehot code to be obtained. Each element of x must be greater than or equal to 0, and the dimension cannot be greater than 7.

one

[1]

int32/int64

Same as that of x

ND

Scalar 1. The type and format are the same as those of x. It is passed to the operator and has no actual meaning.

zero

[1]

int32/int64

Same as that of x

ND

Scalar 0. The type and format are the same as those of x. It is passed to the operator and has no actual meaning.

Output

Parameter

Dimension

Data Type

Format

Description

output

Compared with x, it has one more depth dimension on the axis.

int32/int64

Same as that of x

ND

Output tensor. The type and format are the same as those of x. The output is a onehot code.

Constraints

  • The absolute value of axis must be less than the number of dimensions of x.
  • The element in x is less than depth.