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