r"""Performs one-hot encoding for the input tensor. Args: inp: input tensor. num_classes: number of classes denotes the last dimension of the output tensor. Examples: >>> import numpy as np >>> x = Tensor(np.arange(1, 4, dtype=np.int32)) >>> F.one_ho
(inp: Tensor, num_classes: int)
| 1642 | |
| 1643 | |
| 1644 | def one_hot(inp: Tensor, num_classes: int) -> Tensor: |
| 1645 | r"""Performs one-hot encoding for the input tensor. |
| 1646 | |
| 1647 | Args: |
| 1648 | inp: input tensor. |
| 1649 | num_classes: number of classes denotes the last dimension of the output tensor. |
| 1650 | |
| 1651 | Examples: |
| 1652 | >>> import numpy as np |
| 1653 | >>> x = Tensor(np.arange(1, 4, dtype=np.int32)) |
| 1654 | >>> F.one_hot(x, num_classes=4) |
| 1655 | Tensor([[0 1 0 0] |
| 1656 | [0 0 1 0] |
| 1657 | [0 0 0 1]], dtype=int32, device=xpux:0) |
| 1658 | """ |
| 1659 | zeros_tensor = zeros( |
| 1660 | list(inp.shape) + [num_classes], dtype=inp.dtype, device=inp.device |
| 1661 | ) |
| 1662 | ones_tensor = ones(list(inp.shape) + [1], dtype=inp.dtype, device=inp.device) |
| 1663 | |
| 1664 | op = builtin.IndexingSetOneHot(axis=inp.ndim, ndim=inp.ndim) |
| 1665 | (result,) = apply(op, zeros_tensor, inp, ones_tensor) |
| 1666 | return result |
| 1667 | |
| 1668 | |
| 1669 | def embedding( |