eye

paddle. eye ( num_rows: int | paddle.Tensor, num_columns: int | paddle.Tensor | None = None, dtype: DTypeLike | None = None, name: str | None = None, *, out: paddle.Tensor | None = None, device: PlaceLike | None = None, requires_grad: bool = False, pin_memory: bool = False ) paddle.Tensor [source]

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

Note

Alias Support: The parameter name n can be used as an alias for num_rows, and m can be used as an alias for num_columns. For example, eye(n=tensor_x, m=tensor_y, ...) is equivalent to eye(num_rows=tensor_x, num_columns=tensor_y, ...).

Parameters
  • num_rows (int | paddle.Tensor) – the number of rows in each batch Tensor. Alias: n.

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

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

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

  • out (Tensor, optional) – The output tensor.

  • device (PlaceLike|None, optional) – The desired device of returned tensor. if None, uses the current device for the default tensor type (see paddle.device.set_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. Default: None.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • pin_memory (bool, optional) – If set, return tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False

Returns

An identity Tensor or DenseTensor 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]]