PyTorch torch.row_stack function
PyTorch torch reference manual
torch.row_stack is a function in PyTorch used to stack tensors by row, equivalent to vertical stacking.
Function definition
torch.row_stack(tensors, *, out=None)
Usage examples
Example
import
torch
Row stacking of one-dimensional tensors
x1
=
torch.
tensor
(
[
1
,
2
,
3
]
)
x2
=
torch.
tensor
(
[
4
,
5
,
6
]
)
result
=
torch.
row_stack
(
[
x1
,
x2
]
)
(
"Row stacking of one-dimensional tensors:"
)
(
f
" x1: {x1}"
)
(
f
" x2: {x2}"
)
(
f
" row_stack:n{result}"
)
Row stacking of two-dimensional tensors
y1
=
torch.
tensor
(
[
[
1
,
2
,
3
]
]
)
y2
=
torch.
tensor
(
[
[
4
,
5
,
6
]
]
)
result
=
torch.
row_stack
(
[
y1
,
y2
]
)
(
"nRow stacking of two-dimensional tensors:"
)
(
f
" y1:n{y1}"
)
(
f
" y2:n{y2}"
)
(
f
" row_stack:n{result}"
)
Equivalent to vstack
z1
=
torch.
tensor
(
[
1
,
2
,
3
]
)
z2
=
torch.
tensor
(
[
4
,
5
,
6
]
)
result_vstack
=
torch.
vstack
(
[
z1
,
z2
]
)
result_row_stack
=
torch.
row_stack
(
[
z1
,
z2
]
)
(
"nrow_stack is equivalent to vstack:"
)
(
f
" vstack: {result_vstack.tolist()}"
)
(
f
" row_stack: {result_row_stack.tolist()}"
)
The output is:
一维张量行堆叠:
x1: tensor([1, 2, 3])
x2: tensor([4, 5, 6])
row_stack:
tensor([[1, 2, 3],
[4, 5, 6]])
Row stacking of 2D tensors:
y1:
tensor([[1, 2, 3]])
y2:
tensor([[4, 5, 6]])
row_stack:
tensor([[1, 2, 3],
[4, 5, 6]])
row_stack 等价于 vstack:
vstack: [[1, 2, 3], [4, 5, 6]]
row_stack: [[1, 2, 3], [4, 5, 6]]