mindspore.ops.triu_indices
- mindspore.ops.triu_indices(row, col, offset=0, *, dtype=mstype.int64)[源代码]
返回一个二维的tensor,包含 row * col 矩阵的上三角元素的索引。第一行是所有索引的行坐标,第二行是所有索引的列坐标。 坐标先按行排序,后按列排序。
说明
在CUDA上运行的时候, row * col 必须小于2^59以防止计算时溢出。
- 参数:
row (int) - 二维tensor的行数。
col (int) - 二维tensor的列数。
offset (int, 可选) - 对角线偏移量。默认
0
。当 offset 是正整数时,对角线向上方偏移。
当 offset 是负整数时,对角线向下方偏移。
- 关键字参数:
dtype (
mindspore.dtype
, 可选) - 指定数据类型,支持 mstype.int32 和 mstype.int64 ,默认mstype.int64
。
- 返回:
Tensor
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> # case 1: By default, offset=0, all elements on and below the main diagonal are retained. >>> mindspore.ops.triu_indices(3, 2, 0) Tensor(shape=[2, 3], dtype=Int64, value= [[0, 0, 1], [0, 1, 1]]) >>> >>> # case 2: If offset=1, the indices on and above the first sub-diagonal above the main diagonal are returned. >>> mindspore.ops.triu_indices(3, 2, 1) Tensor(shape=[2, 1], dtype=Int64, value= [[0], [1]]) >>> >>> # case 3: If offset=-1, the indices on and above the first sub-diagonal below the main diagonal are returned. >>> mindspore.ops.triu_indices(3, 2, -1) Tensor(shape=[2, 5], dtype=Int64, value= [[0, 0, 1, 1, 2], [0, 1, 0, 1, 1]])