Shortcuts

torch.flatten

torch.flatten(input, start_dim=0, end_dim=-1) 张量

input 通过重塑为一维张量来展平。如果传递了 start_dimend_dim,则仅展平从 start_dim 开始到 end_dim 结束的维度。input 中元素的顺序保持不变。

与NumPy的flatten不同,后者总是复制输入的数据,此函数可能返回原始对象、视图或副本。如果没有维度被展平,则返回原始对象input。否则,如果输入可以被视为展平形状,则返回该视图。最后,仅当输入不能被视为展平形状时,才复制输入的数据。有关何时返回视图的详细信息,请参阅torch.Tensor.view()

注意

展平一个零维张量将返回一个一维视图。

Parameters
  • 输入 (张量) – 输入张量。

  • start_dim (int) – 要展平的第一个维度

  • end_dim (int) – 要展平的最后一个维度

示例:

>>> t = torch.tensor([[[1, 2],
...                    [3, 4]],
...                   [[5, 6],
...                    [7, 8]]])
>>> torch.flatten(t)
tensor([1, 2, 3, 4, 5, 6, 7, 8])
>>> torch.flatten(t, start_dim=1)
tensor([[1, 2, 3, 4],
        [5, 6, 7, 8]])
优云智算