Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.chunk function

PyTorch torch.chunk function


PyTorch torch reference manual PyTorch torch reference manual

torch.chunk is a function in PyTorch used to split a tensor into chunks along a specified dimension.

Function definition

torch.chunk(tensor, chunks, dim)


Usage examples

Example

import

torch

x

=

torch.

arange

(

12

)

.

reshape

(

3

,

4

)

Split into 3 chunks

result

=

torch.

chunk

(

x

,

3

,

dim

=

0

)

print

(

"Number of chunks:"

,

len

(

result

)

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

"Chunk {i}:"

,

t.

shape

)


PyTorch torch reference manual PyTorch torch reference manual