mindspore.nn.MatrixDiagPart

class mindspore.nn.MatrixDiagPart[源代码]

返回批对角矩阵的对角线部分。

假设 xk 个维度 [I,J,K,...,M,N] ,则输出秩为 k1 且维度为 [I,J,K,...,min(M,N)] 的Tensor,其中: output[i,j,k,...,n]=x[i,j,k,...,n,n]

输入:
  • x (Tensor) - 输入具有批对角的Tensor。支持的数据类型包括:float32、float16、int32、int8和uint8。

输出:

Tensor,shape与输入 x 相同。shape必须为 x.shape[:2]+[min(x.shape[2:])]

异常:
  • TypeError - x 的数据类型不是float32、float16、int32、int8或uint8。

支持平台:

Ascend

样例:

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> x = Tensor([[[-1, 0], [0, 1]],
...             [[-1, 0], [0, 1]],
...             [[-1, 0], [0, 1]]], mindspore.float32)
>>> matrix_diag_part = nn.MatrixDiagPart()
>>> output = matrix_diag_part(x)
>>> print(output)
[[-1.  1.]
 [-1.  1.]
 [-1.  1.]]
>>> x = Tensor([[-1, 0, 0, 1],
...             [-1, 0, 0, 1],
...             [-1, 0, 0, 1],
...             [-1, 0, 0, 1]], mindspore.float32)
>>> matrix_diag_part = nn.MatrixDiagPart()
>>> output = matrix_diag_part(x)
>>> print(output)
[-1.  0.  0.  1.]