torch.chunk#
- torch.chunk(input: Tensor, chunks: int, dim: int = 0) Tuple[Tensor, ...] #
尝试将张量分割为指定数量的块。每个块都是输入张量的一个视图。
注意
此函数返回的块数量可能少于指定的数量!
另请参阅
torch.tensor_split()
是一个总是返回确切指定数量块的函数如果沿给定维度
dim
的张量大小可被chunks
整除,则所有返回的块大小相同。如果沿给定维度dim
的张量大小不可被chunks
整除,则所有返回的块大小相同,除了最后一个。如果无法进行此类划分,此函数返回的块数量可能少于指定的数量。示例
>>> torch.arange(11).chunk(6) (tensor([0, 1]), tensor([2, 3]), tensor([4, 5]), tensor([6, 7]), tensor([8, 9]), tensor([10])) >>> torch.arange(12).chunk(6) (tensor([0, 1]), tensor([2, 3]), tensor([4, 5]), tensor([6, 7]), tensor([8, 9]), tensor([10, 11])) >>> torch.arange(13).chunk(6) (tensor([0, 1, 2]), tensor([3, 4, 5]), tensor([6, 7, 8]), tensor([ 9, 10, 11]), tensor([12]))