megengine.functional.nn.one_hot#

one_hot(inp, num_classes)[源代码]#

对输入张量进行 one-hot 编码。

参数:
  • inp (Tensor) – 输入张量。

  • num_classes (int) – 表示输出张量最后一个维度的类数。

实际案例

>>> import numpy as np
>>> x = Tensor(np.arange(1, 4, dtype=np.int32))
>>> F.one_hot(x, num_classes=4)
Tensor([[0 1 0 0]
 [0 0 1 0]
 [0 0 0 1]], dtype=int32, device=xpux:0)
返回类型:

Tensor