mindspore.Tensor.diag
- Tensor.diag()[source]
Constructs a diagonal tensor with a given diagonal values.
Assume self tensor has dimensions
, the output is a tensor of rank 2k with dimensions where: and 0 everywhere else.- Returns
Tensor, has the same dtype as self tensor.
- Raises
ValueError – If rank of self tensor is less than 1.
- Supported Platforms:
Ascend
GPU
Examples
>>> from mindspore import Tensor >>> x = Tensor([1, 2, 3, 4]).astype('int32') >>> output = x.diag() >>> print(output) [[1 0 0 0] [0 2 0 0] [0 0 3 0] [0 0 0 4]]