评价此页

torch.split#

torch.split(tensor, split_size_or_sections, dim=0)[源代码]#

将张量分割成多个块。每个块都是原始张量的一个视图。

如果 split_size_or_sections 是整数类型,则 tensor 将(如果可能)被分割成大小相等的块。如果给定维度 dim 上的张量大小不能被 split_size 整除,则最后一个块会更小。

如果 split_size_or_sections 是一个列表,则 tensor 将被分割成 len(split_size_or_sections) 个块,其在 dim 维度上的大小由 split_size_or_sections 指定。

参数
  • tensor (Tensor) – 要分割的张量。

  • split_size_or_sections (int) 或 (list(int)) – 单个块的大小或每个块的大小列表

  • dim (int) – 沿此维度分割张量。

返回类型

元组[torch.Tensor, …]

示例

>>> a = torch.arange(10).reshape(5, 2)
>>> a
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
>>> torch.split(a, 2)
(tensor([[0, 1],
         [2, 3]]),
 tensor([[4, 5],
         [6, 7]]),
 tensor([[8, 9]]))
>>> torch.split(a, [1, 4])
(tensor([[0, 1]]),
 tensor([[2, 3],
         [4, 5],
         [6, 7],
         [8, 9]]))