mindspore.Tensor.diagonal

Tensor.diagonal(offset=0, axis1=0, axis2=1)[source]

Return specified diagonals.

Parameters
  • offset (int, optional) – Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal.

  • axis1 (int, optional) – Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).

  • axis2 (int, optional) – Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis.

Returns

Tensor, if Tensor is 2-D, return a 1-D Tensor containing the diagonal.

Raises

ValueError – If the input tensor has less than two dimensions.

Supported Platforms:

Ascend GPU CPU

See also

mindspore.Tensor.trace(): Return the sum along diagonals of the tensor.

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(4).reshape(2, 2))
>>> print(a)
[[0 1]
[2 3]]
>>> output = a.diagonal()
>>> print(output)
[0 3]