mindspore.ops.eye
- mindspore.ops.eye(n, m=None, dtype=None)[source]
Creates a tensor with ones on the diagonal and zeros in the rest.
Note
Combines ReverseV2 operator to get an anti-diagonal Tensor, but ReverseV2 only supports Ascend and GPU platforms currently.
- Parameters
n (int) – The number of rows of returned tensor. Constant value only.
m (int) – The number of columns of returned tensor. Constant value only. Default:
None
, ifNone
, the number of columns is as the same as n.dtype (mindspore.dtype) – MindSpore’s dtype, the data type of the returned tensor. The data type can be bool or Number. Default:
None
, the data type of the returned tensor is mindspore.float32.
- Returns
Tensor, a tensor with ones on the diagonal and the rest of elements are zero. The shape of output depends on the user’s Inputs n and m. And the data type depends on Inputs dtype.
- Raises
TypeError – If m or n is not an int.
ValueError – If m or n is less than 0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> from mindspore import ops >>> output = ops.eye(2, 2, mindspore.int32) >>> print(output) [[1 0] [0 1]] >>> print(output.dtype) Int32 >>> output = ops.eye(1, 2, mindspore.float64) >>> print(output) [[1. 0.]] >>> print(output.dtype) Float64 >>> output = ops.eye(2, dtype=mindspore.int32) >>> print(output) [[1 0] [0 1]] >>> print(output.dtype) Int32 >>> output = ops.eye(2) >>> print(output) [[1. 0.] [0. 1.]] >>> print(output.dtype) Float32