eye

paddle.fluid.layers.tensor. eye ( num_rows, num_columns=None, batch_shape=None, dtype='float32', name=None ) [source]

This function constructs a or a batch of 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.

  • batch_shape (list, optional) – If provided, the returned tensor will have a leading batch size of this shape, the data type of batch_shape is int. Default is None.

  • dtype (np.dtype|str, optional) – The data type of the returned tensor. It should be int32, int64, float16, float32, float64, default is ‘float32’.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

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

Return type

Tensor

Examples

import paddle.fluid as fluid
data = fluid.layers.eye(3, dtype='int32')
# [[1, 0, 0]
#  [0, 1, 0]
#  [0, 0, 1]]

data = fluid.layers.eye(2, 3, dtype='int32')
# [[1, 0, 0]
#  [0, 1, 0]]

data = fluid.layers.eye(2, batch_shape=[3])
# Construct a batch of 3 identity tensors, each 2 x 2.
# data[i, :, :] is a 2 x 2 identity tensor, i = 0, 1, 2.