评价此页

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])