评价此页

torch.tril_indices#

torch.tril_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) Tensor#

返回一个 2xN 的 Tensor,其中包含一个 rowcol 列的矩阵的下三角部分的索引,第一个行包含所有索引的行坐标,第二个行包含列坐标。索引按行排序,然后按列排序。

矩阵的下三角部分定义为对角线及其以下的所有元素。

参数 offset 控制要考虑哪个对角线。如果 offset = 0,则保留主对角线及其下方所有元素。正值会包含主对角线上方的对角线数量,负值则排除主对角线下方的对角线数量。主对角线是索引集 {(i,i)}\lbrace (i, i) \rbrace,其中 i[0,min{d1,d2}1]i \in [0, \min\{d_{1}, d_{2}\} - 1] 其中 d1,d2d_{1}, d_{2} 为矩阵的维度。

注意

在 CUDA 上运行时,row * col 必须小于 2592^{59} 以防止计算过程中发生溢出。

参数
  • row (int) – 2D 矩阵的行数。

  • col (int) – 2D 矩阵的列数。

  • offset (int) – 主对角线偏移量。默认值:如果未提供,则为 0。

关键字参数
  • dtype (torch.dtype, optional) – 返回张量的期望数据类型,仅支持 torch.int, torch.long。默认值:如果为 None,则为 torch.long

  • device (torch.device, optional) – 返回张量的期望设备。默认值:如果为 None,则使用当前默认张量类型的设备(请参见 torch.set_default_device())。对于 CPU 张量类型,device 将是 CPU,对于 CUDA 张量类型,将是当前 CUDA 设备。

  • layout (torch.layout, optional) – 目前仅支持 torch.strided

示例

>>> a = torch.tril_indices(3, 3)
>>> a
tensor([[0, 1, 1, 2, 2, 2],
        [0, 0, 1, 0, 1, 2]])

>>> a = torch.tril_indices(4, 3, -1)
>>> a
tensor([[1, 2, 2, 3, 3, 3],
        [0, 0, 1, 0, 1, 2]])

>>> a = torch.tril_indices(4, 3, 1)
>>> a
tensor([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3],
        [0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 2]])