torch.triu_indices#
- torch.triu_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) Tensor #
返回一个
row
xcol
矩阵的上三角部分的索引,以一个 2xN 的 Tensor 返回,其中第一行包含所有索引的行坐标,第二行包含列坐标。索引按行然后按列排序。矩阵的上三角部分定义为对角线上的元素及其上方的元素。
offset
参数控制考虑哪个对角线。如果offset
= 0,则保留主对角线及其上方的所有元素。正值会排除主对角线上方的相应数量的对角线,负值则包含主对角线下方的相应数量的对角线。主对角线是一组索引 ,其中 ,其中 是矩阵的维度。注意
在 CUDA 上运行时,
row * col
必须小于 以防止计算过程中溢出。- 参数
row (
int
) – 2D 矩阵的行数。col (
int
) – 2D 矩阵的列数。offset (
int
) – 相对于主对角线的对角线偏移量。默认值:如果未提供,则为 0。
- 关键字参数
dtype (
torch.dtype
, optional) – 返回张量的期望数据类型,仅支持torch.int
、torch.long
。默认值:如果为None
,则为torch.long
。device (
torch.device
, 可选) – 返回张量的所需设备。默认:如果为None
,则使用当前设备作为默认张量类型(参见torch.set_default_device()
)。对于 CPU 张量类型,device
将是 CPU;对于 CUDA 张量类型,device
将是当前的 CUDA 设备。layout (
torch.layout
, optional) – 目前仅支持torch.strided
。
示例
>>> a = torch.triu_indices(3, 3) >>> a tensor([[0, 0, 0, 1, 1, 2], [0, 1, 2, 1, 2, 2]]) >>> a = torch.triu_indices(4, 3, -1) >>> a tensor([[0, 0, 0, 1, 1, 1, 2, 2, 3], [0, 1, 2, 0, 1, 2, 1, 2, 2]]) >>> a = torch.triu_indices(4, 3, 1) >>> a tensor([[0, 0, 1], [1, 2, 2]])