Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.ops.one_hot

mindspore.ops.one_hot(indices, depth, on_value=1, off_value=0, axis=- 1)[source]

Computes a one-hot tensor.

The locations represented by indices in indices take value on_value, while all other locations take value off_value.

Note

If the input indices has rank N, the output will have rank N+1. The new axis is created at dimension axis. On Ascend, if on_value is int64 dtype, indices must be int64 dtype, and the value for on_value and off_value can only be 1 and 0.

Parameters
  • indices (Tensor) – A tensor of indices. Tensor of shape (X0,,Xn). Data type must be int32 or int64.

  • depth (int) – A scalar defining the depth of the one-hot dimension.

  • on_value (Union[Tensor, int, float], optional) – A value to fill in output when indices[j] = i. Data type must be int32, int64, float16 or float32. Default: 1 .

  • off_value (Union[Tensor, int, float], optional) – A value to fill in output when indices[j] != i. Has the same data type as on_value. Default: 0 .

  • axis (int, optional) – Position to insert the value. e.g. If shape of self is (N,C), and axis is -1, the output shape will be (N,C,depth), If axis is 0, the output shape will be (depth,N,C). Default: -1 .

Returns

Tensor, one-hot tensor. Tensor of shape (X0,,Xaxis,depth,Xaxis+1,,Xn), and it has the same data type as on_value.

Raises
  • TypeError – If axis or depth is not an int.

  • TypeError – If dtype of indices is not int32 or int64.

  • TypeError – If dtype of on_value is not int32, int64, float16 or float32.

  • TypeError – If indices, on_value or off_value is not a Tensor.

  • ValueError – If axis is not in range [-1, ndim].

  • ValueError – If depth is less than 0.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> indices = Tensor(np.array([0, 1, 2]), mindspore.int32)
>>> depth, on_value, off_value = 3, Tensor(1.0, mindspore.float32), Tensor(0.0, mindspore.float32)
>>> output = ops.one_hot(indices, depth, on_value, off_value, axis=-1)
>>> print(output)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]