PyTorch torch.hsplit function
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
)
(
"Original 1D tensor:"
)
(
x
)
result
=
torch.
hsplit
(
x
,
2
)
(
"Evenly split into 2 parts:"
)
for
i
,
t
in
enumerate
(
result
)
:
(
f
" Chunk {i}: {t}"
)
Horizontally split a 2D tensor
y
=
torch.
arange
(
12
)
.
reshape
(
3
,
4
)
(
"nOriginal 2D tensor:"
)
(
y
)
result
=
torch.
hsplit
(
y
,
2
)
(
"Split into 2 parts along columns:"
)
for
i
,
t
in
enumerate
(
result
)
:
(
f
" chunk {i}:n{t}"
)
Split by index
result
=
torch.
hsplit
(
y
,
[
1
,
3
]
)
(
"nSplit by index [1, 3]:"
)
for
i
,
t
in
enumerate
(
result
)
:
(
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]])