- paddle. trace ( x, offset=0, axis1=0, axis2=1, name=None )
This OP computes the sum along diagonals of the input tensor x.
xis 2D, returns the sum of diagonal.
xhas larger dimensions, then returns an tensor of diagonals sum, diagonals be taken from the 2D planes specified by axis1 and axis2. By default, the 2D planes formed by the first and second axes of the input tensor x.
offsetdetermines where diagonals are taken from input tensor x:
If offset = 0, it is the main diagonal.
If offset > 0, it is above the main diagonal.
If offset < 0, it is below the main diagonal.
Note that if offset is out of input’s shape indicated by axis1 and axis2, 0 will be returned.
x (Tensor) – The input tensor x. Must be at least 2-dimensional. The input data type should be float32, float64, int32, int64.
offset (int, optional) – Which diagonals in input tensor x will be taken. Default: 0 (main diagonals).
axis1 (int, optional) – The first axis with respect to take diagonal. Default: 0.
axis2 (int, optional) – The second axis with respect to take diagonal. Default: 1.
name (str, optional) – Normally there is no need for user to set this property. For more information, please refer to Name. Default: None.
the output data type is the same as input data type.
- Return type
import paddle case1 = paddle.randn([2, 3]) case2 = paddle.randn([3, 10, 10]) case3 = paddle.randn([3, 10, 5, 10]) data1 = paddle.trace(case1) # data1.shape =  data2 = paddle.trace(case2, offset=1, axis1=1, axis2=2) # data2.shape =  data3 = paddle.trace(case3, offset=-3, axis1=1, axis2=-1) # data2.shape = [3, 5]