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