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

PyTorch torch.unbind function

PyTorch torch.unbind function


PyTorch torch reference manual PyTorch torch reference manual

torch.unbind is a function in PyTorch used to split a tensor into a tuple along a specified dimension.

Function definition

torch.unbind(input, dim=0)


Usage examples

Example

import

torch

x

=

torch.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

"Original tensor:"

)

print

(

x

)

result

=

torch.

unbind

(

x

,

dim

=

0

)

print

(

"Split along dim=0:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" Chunk {i}: {t}"

)

result

=

torch.

unbind

(

x

,

dim

=

1

)

print

(

"Split along dim=1:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" Chunk {i}: {t}"

)

The output is:

原始张量:

tensor([[ 0,  1,  2,  3],

        [ 4,  5,  6,  7],

        [ 8,  9, 10, 11]])

Split along dim=0:

  块 0: tensor([0, 1, 2, 3])

  块 1: tensor([4, 5, 6, 7])

  块 2: tensor([ 8,  9, 10, 11])

# Split along dim=1:

  块 0: tensor([0, 4, 8])

  块 1: tensor([1, 5, 9])

  块 2: tensor([ 2,  6, 10])

  块 3: tensor([ 3,  7, 11])


PyTorch torch reference manual PyTorch torch reference manual