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

PyTorch torch.dsplit function

PyTorch torch.dsplit function


PyTorch torch reference manual PyTorch torch reference manual

torch.dsplit is a PyTorch function for splitting a tensor along the depth (third dimension).

Function definition

torch.dsplit(input, indices_or_sections)


Usage examples

Example

import

torch

Depth splitting of a 3D tensor

x

=

torch.

arange

(

24

)

.

reshape

(

2

,

3

,

4

)

print

(

"Original 3D tensor:"

)

print

(

x

)

result

=

torch.

dsplit

(

x

,

2

)

print

(

"Split into 2 parts along depth:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" chunk {i}:n{t}"

)

Split by index

result

=

torch.

dsplit

(

x

,

[

1

,

3

]

)

print

(

"nSplit by index [1, 3]:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" chunk {i}:n{t}"

)

Depth splitting of a 4D tensor

y

=

torch.

arange

(

32

)

.

reshape

(

2

,

2

,

4

,

2

)

print

(

"n4D tensor shape:"

,

y.

shape

)

result

=

torch.

dsplit

(

y

,

2

)

print

(

"Split into 2 parts along the third dimension:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" chunk {i} shape: {t.shape}"

)

The output is:

原始三维张量:

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

         [ 4,  5,  6,  7],

         [ 8,  9, 10, 11]],



        [[12, 13, 14, 15],

         [16, 17, 18, 19],

         [20, 21, 22, 23]]])

Divided into 2 parts along the depth:

  块 0:

tensor([[[ 0,  1],

         [ 4,  5],

         [ 8,  9]],



        [[12, 13],

         [16, 17],

         [20, 21]]])

  块 1:

tensor([[[ 2,  3],

         [ 6,  7],

         [10, 11]],



        [[14, 15],

         [18, 19],

         [22, 23]]])



Split by index [1, 3]:

  块 0:

tensor([[[ 0],

         [ 4],

         [ 8]],



        [[12],

         [16],

         [20]]])

  块 1:

tensor([[[ 1,  2],

         [ 5,  6],

         [ 9, 10]],



        [[13, 14],

         [17, 18],

         [21, 22]]])

  块 2:

tensor([[[ 3],

         [ 7],

         [11]],



        [[15],

         [19],

         [23]]])



四维张量形状: torch.Size([2, 2, 4, 2])

Divide along the third dimension into 2 parts:

  块 0 形状: torch.Size([2, 2, 2, 2])

  块 1 形状: torch.Size([2, 2, 2, 2])


PyTorch torch reference manual PyTorch torch reference manual