torch.tile¶
- torch.tile(input, dims) Tensor ¶
透過重複
input
的元素來建構張量。dims
參數指定每個維度中的重複次數。如果
dims
指定的維度數量少於input
擁有的維度數量,則會在dims
前面補上 1,直到所有維度都被指定。例如,如果input
的形狀為 (8, 6, 4, 2),而dims
為 (2, 2),則dims
會被視為 (1, 1, 2, 2)。類似地,如果
input
擁有的維度數量少於dims
指定的維度數量,則input
會被視為在維度 0 上執行了 unsqueeze 操作,直到它擁有與dims
指定的維度數量相同的維度數量。例如,如果input
的形狀為 (4, 2),而dims
為 (3, 3, 2, 2),則input
會被視為具有形狀 (1, 1, 4, 2)。注意
此函數類似於 NumPy 的 tile 函數。
範例
>>> 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]])