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

PyTorch torch.hsplit function

PyTorch torch.hsplit function


PyTorch torch reference manual PyTorch torch reference manual

torch.hsplit A function in PyTorch for splitting a tensor horizontally (along columns).

Function definition

torch.hsplit(input, indices_or_sections)


Usage examples

Example

import

torch

Horizontally split a 1D tensor

x

=

torch.

arange

(

10

)

print

(

"Original 1D tensor:"

)

print

(

x

)

result

=

torch.

hsplit

(

x

,

2

)

print

(

"Evenly split into 2 parts:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" Chunk {i}: {t}"

)

Horizontally split a 2D tensor

y

=

torch.

arange

(

12

)

.

reshape

(

3

,

4

)

print

(

"nOriginal 2D tensor:"

)

print

(

y

)

result

=

torch.

hsplit

(

y

,

2

)

print

(

"Split into 2 parts along columns:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" chunk {i}:n{t}"

)

Split by index

result

=

torch.

hsplit

(

y

,

[

1

,

3

]

)

print

(

"nSplit by index [1, 3]:"

)

for

i

,

t

in

enumerate

(

result

)

:

   

print

(

f

" chunk {i}:n{t}"

)

The output is:

原始一维张量:

tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Divided into 2 parts:

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

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



Original two-dimensional tensor:

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

        [ 4,  5,  6,  7],

        [ 8,  9, 10, 11]])

Split into 2 parts by column:

  块 0:

tensor([[0, 1],

        [4, 5],

        [8, 9]])

  块 1:

tensor([[ 2,  3],

        [ 6,  7],

        [10, 11]])



Split by index [1, 3]:

  块 0:

tensor([[0],

        [4],

        [8]])

  块 1:

tensor([[ 1,  2],

        [ 5,  6],

        [ 9, 10]])

  块 2:

tensor([[ 3],

        [ 7],

        [11]])


PyTorch torch reference manual PyTorch torch reference manual