torch.triu_indices¶
- torch.triu_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) Tensor ¶
返回一個 2-by-N Tensor,其中包含一個
row
xcol
矩陣的上三角部分的索引。第一行包含所有索引的列座標,第二行包含行座標。索引的排序方式是基於列,然後是行。矩陣的上三角部分定義為對角線上及對角線上方的元素。
參數
offset
控制要考慮的對角線。 如果offset
= 0,則保留主對角線上及之上的所有元素。 正值會排除主對角線上方的相同數量的對角線,類似地,負值會包含主對角線下方的相同數量的對角線。 主對角線是索引集合 ,其中 ,其中 是矩陣的維度。注意
在 CUDA 上運行時,
row * col
必須小於 ,以防止計算期間溢位。- 參數
row (
int
) – 2D 矩陣中的列數。col (
int
) – 2D 矩陣中的行數。offset (
int
) – 與主對角線的對角線偏移量。預設值:如果未提供,則為 0。
- 關鍵字參數
dtype (
torch.dtype
, optional) – 返回的 tensor 的所需資料類型。預設值:如果None
,則為torch.long
。device (
torch.device
, optional) – 返回的 tensor 的所需裝置。預設值:如果None
,則使用預設 tensor 類型的目前裝置(請參閱torch.set_default_device()
)。device
對於 CPU tensor 類型將為 CPU,而對於 CUDA tensor 類型將為目前的 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]])