mindspore.ops.diag

查看源文件
mindspore.ops.diag(input)[源代码]

返回一个用输入tensor作为对角线,其余元素为 0 的tensor。

警告

这是一个实验性API,后续可能修改或删除。

参数:
  • input (Tensor) - 输入tensor。

返回:

Tensor

支持平台:

Ascend GPU CPU

样例:

>>> 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)