This OP creates a square matrix which has diagonal values specified by input diagonal.


diagonal (Variable|numpy.ndarray) – The input tensor should be 1D tensor, the input shape is \([ N]\) , specifying diagonal values by this input tensor. The input data type should be float32, float64, int32, int64.


The tensor variable storing the square matrix, the diagonal values specified by input diagonal. the output shape is \([N, N]\) with two dims.

Return type

Variable, the output data type is the same as input data type.


# [[3, 0, 0]
#  [0, 4, 0]
#  [0, 0, 5]

import paddle.fluid as fluid
import numpy as np
diagonal = np.arange(3, 6, dtype='int32')
data = fluid.layers.diag(diagonal)
# diagonal.shape=(3,) data.shape=(3, 3)