mindspore.mint.diag
- mindspore.mint.diag(input, diagonal=0)[源代码]
如果 input 是向量(一维张量),则返回一个二维张量,其中 input 的元素作为对角线。
如果 input 是矩阵(二维张量),则返回具有 input 对角线元素的 1-D 张量。
参数 diagonal 控制要考虑的对角线:
如果 diagonal = 0,则它是主对角线。
如果 diagonal > 0,则它位于主对角线的上方。
如果 diagonal < 0,则它位于主对角线下方。
警告
这是一个实验性API,后续可能修改或删除。
- 参数:
input (Tensor) - 输入tensor。
diagonal (int, 可选) - 对角线的偏移索引。默认
0。
- 返回:
Tensor,具有与输入tensor相同的数据类型,其shape由 diagonal 决定。
如果输入 input 的shape为 \((x_0)\) :输出shape为 \((x_0 + \left | diagonal \right | , x_0 + \left | diagonal \right | )\) 的二维tensor。
如果输入 input 的shape为 \((x_0, x_1)\) :输出shape为主对角线上下平移 \((\left | diagonal \right |)\) 个单位后所剩元素的长度的一维tensor。
- 异常:
ValueError - input 的shape不是1-D或2-D。
- 支持平台:
Ascend
样例:
>>> import mindspore >>> input = mindspore.tensor([1,2,3,4]) >>> mindspore.mint.diag(input) Tensor(shape=[4, 4], dtype=Int64, value= [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]) >>> mindspore.mint.diag(input, diagonal=1) Tensor(shape=[5, 5], dtype=Int64, value= [[0, 1, 0, 0, 0], [0, 0, 2, 0, 0], [0, 0, 0, 3, 0], [0, 0, 0, 0, 4], [0, 0, 0, 0, 0]]) >>> mindspore.mint.diag(input, diagonal=-1) Tensor(shape=[5, 5], dtype=Int64, value= [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 3, 0, 0], [0, 0, 0, 4, 0]]) >>> input = mindspore.tensor([[1,2],[3,4]]) >>> mindspore.mint.diag(input) Tensor(shape=[2], dtype=Int64, value= [1, 4]) >>> mindspore.mint.diag(input, diagonal=1) Tensor(shape=[1], dtype=Int64, value= [2]) >>> mindspore.mint.diag(input, diagonal=-1) Tensor(shape=[1], dtype=Int64, value= [3])