評價此頁

torch.tile#

torch.tile(input, dims) Tensor#

透過重複 input 的元素來構造張量。dims 引數指定每個維度的重複次數。

如果 dims 指定的維度少於 input 的維度,則會在 dims 前面加上 ones,直到指定所有維度。例如,如果 input 的形狀為 (8, 6, 4, 2) 且 dims 為 (2, 2),則 dims 被視為 (1, 1, 2, 2)。

類似地,如果 input 的維度少於 dims 指定的維度,則 input 會被視為在維度零處進行了 unsqueeze,直到其維度與 dims 指定的維度數量相同。例如,如果 input 的形狀為 (4, 2) 且 dims 為 (3, 3, 2, 2),則 input 會被視為具有形狀 (1, 1, 4, 2)。

注意

此函式類似於 NumPy 的 tile 函式。

引數
  • input (Tensor) – 需要重複其元素的張量。

  • dims (tuple) – 每個維度的重複次數。

示例

>>> x = torch.tensor([1, 2, 3])
>>> x.tile((2,))
tensor([1, 2, 3, 1, 2, 3])
>>> y = torch.tensor([[1, 2], [3, 4]])
>>> torch.tile(y, (2, 2))
tensor([[1, 2, 1, 2],
        [3, 4, 3, 4],
        [1, 2, 1, 2],
        [3, 4, 3, 4]])