mindspore.mint.eye

查看源文件
mindspore.mint.eye(n, m=None, dtype=None)[源代码]

创建一个主对角线上元素为1,其余元素为0的Tensor。

说明

Ascend平台支持返回Tensor的数据类型包括float16、float32、int8、int16、int32、int64、uint8和bool。

参数:
  • n (int) - 指定返回Tensor的行数。仅支持常量值。

  • m (int,可选) - 指定返回Tensor的列数。仅支持常量值。默认值为 None ,返回Tensor的列数默认与n相等。

  • dtype (mindspore.dtype,可选) - 指定返回Tensor的数据类型。数据类型必须是 bool_number 。默认值为 None ,返回Tensor的数据类型默认为mindspore.float32。

返回:

Tensor,主对角线上为1,其余的元素为0。它的shape由 nm 指定。数据类型由 dtype 指定。

异常:
  • TypeError - mn 不是int。

  • ValueError - mn 小于0。

支持平台:

Ascend

样例:

>>> import mindspore
>>> output = mindspore.mint.eye(3)
>>> print(output)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
>>>
>>> output = mindspore.mint.eye(3, 4)
>>> print(output)
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]