評價此頁

torch.unflatten#

torch.unflatten(input, dim, sizes) Tensor#

將輸入張量的某個維度展開為多個維度。

另請參閱

torch.flatten() 是此函式的逆操作。它將多個維度合併為一個。

引數
  • input (Tensor) – 輸入張量。

  • dim (int) – 要解展的維度,指定為 input.shape 中的索引。

  • sizes (Tuple[int]) – 解展後的維度的新形狀。其元素之一可以是 -1,在這種情況下,將推斷出相應的輸出維度。否則,sizes 的乘積必須等於 input.shape[dim]

返回

輸入張量的一個檢視,其中指定的維度已被解展。

示例:
>>> torch.unflatten(torch.randn(3, 4, 1), 1, (2, 2)).shape
torch.Size([3, 2, 2, 1])
>>> torch.unflatten(torch.randn(3, 4, 1), 1, (-1, 2)).shape
torch.Size([3, 2, 2, 1])
>>> torch.unflatten(torch.randn(5, 12, 3), -2, (2, 2, 3, 1, 1)).shape
torch.Size([5, 2, 2, 3, 1, 1, 3])