eye

paddle. eye ( num_rows, num_columns=None, dtype=None, name=None ) [source]

This function constructs 2-D Tensor with ones on the diagonal and zeros elsewhere.

Parameters
  • num_rows (int) – the number of rows in each batch Tensor.

  • num_columns (int, optional) – the number of columns in each batch Tensor. If None, default: num_rows.

  • dtype (np.dtype|str, optional) – The data type of the returned Tensor. It should be int32, int64, float16, float32, float64. Default: if None, the data type is float32.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

An identity Tensor or LoDTensor of shape [num_rows, num_columns].

Return type

Tensor

Examples

>>> import paddle

>>> data = paddle.eye(3, dtype='int32')
>>> print(data.numpy())
[[1 0 0]
 [0 1 0]
 [0 0 1]]
>>> data = paddle.eye(2, 3, dtype='int32')
>>> print(data.numpy())
[[1 0 0]
 [0 1 0]]