mindspore.ops.diag
- mindspore.ops.diag(input)[source]
Return a tensor with the input as its diagonal elements and
0
elsewhere.Warning
This is an experimental API that is subject to change or deletion.
- Parameters
input (Tensor) – The input tensor.
- Returns
Tensor
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> # case 1: When input is a 1-D tensor: >>> input = mindspore.ops.randn(3) >>> output = mindspore.ops.diag(input) >>> print(output) [[ 1.7477764 0. 0. ] [ 0. -1.2616369 0. ] [ 0. 0. 2.3283238]] >>> >>> # case 2: When input is a multi-dimensional tensor: >>> input = mindspore.ops.randn(2, 3) >>> print(input) [[ 0.21546374 -0.0120403 -0.7330481 ] [-2.5405762 0.44775972 -1.4063131 ]] >>> output = mindspore.ops.diag(input) >>> print(output) [[[[ 0.21546374 0. 0. ] [ 0. 0. 0. ]] [[ 0. -0.0120403 0. ] [ 0. 0. 0. ]] [[ 0. 0. -0.7330481 ] [ 0. 0. 0. ]]] [[[ 0. 0. 0. ] [-2.5405762 0. 0. ]] [[ 0. 0. 0. ] [ 0. 0.44775972 0. ]] [[ 0. 0. 0. ] [ 0. 0. -1.4063131 ]]]] >>> # Assume input has dimensions (D_1,... D_k), the output is a tensor of rank 2k with dimensions (D_1,..., D_k, D_1,..., D_k). >>> print(output.shape) (2, 3, 2, 3)