PyTorch torch.vsplit function
PyTorch torch reference manual
torch.vsplit A function in PyTorch for splitting a tensor vertically (along rows).
Function definition
torch.vsplit(input, indices_or_sections)
Usage examples
Example
import
torch
Vertically split a 2D tensor
x
=
torch.
arange
(
12
)
.
reshape
(
4
,
3
)
(
"Original 2D tensor:"
)
(
x
)
result
=
torch.
vsplit
(
x
,
2
)
(
"Evenly split into 2 parts along rows:"
)
for
i
,
t
in
enumerate
(
result
)
:
(
f
" chunk {i}:n{t}"
)
Split by index
result
=
torch.
vsplit
(
x
,
[
1
,
3
]
)
(
"nSplit by index [1, 3]:"
)
for
i
,
t
in
enumerate
(
result
)
:
(
f
" chunk {i}:n{t}"
)
Vertically split a 3D tensor
y
=
torch.
arange
(
24
)
.
reshape
(
4
,
3
,
2
)
(
"n3D tensor:"
)
(
y
)
result
=
torch.
vsplit
(
y
,
2
)
(
"nSplit into 2 parts along the first dimension:"
)
for
i
,
t
in
enumerate
(
result
)
:
(
f
" Chunk {i}: {t.shape}"
)
The output is:
原始二维张量:
tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Average along the row is divided into 2 parts:
块 0:
tensor([[0, 1, 2],
[3, 4, 5]])
块 1:
tensor([[ 6, 7, 8],
[ 9, 10, 11]])
Split by index [1, 3]:
块 0:
tensor([[0, 1, 2]])
块 1:
tensor([[3, 4, 5],
[6, 7, 8]])
块 2:
tensor([[ 9, 10, 11]])
Three-dimensional tensor:
tensor([[[ 0, 1],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]],
[[13, 14],
[15, 16],
[17, 18]],
[[19, 20],
[21, 22],
[23, 24]]])
Split into 2 parts along the first dimension:
块 0: torch.Size([2, 3, 2])
块 1: torch.Size([2, 3, 2])