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

PyTorch torch.row_stack function

PyTorch torch.row_stack function


PyTorch torch reference manual 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

]

)

print

(

"Row stacking of one-dimensional tensors:"

)

print

(

f

"  x1: {x1}"

)

print

(

f

"  x2: {x2}"

)

print

(

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

]

)

print

(

"nRow stacking of two-dimensional tensors:"

)

print

(

f

"  y1:n{y1}"

)

print

(

f

"  y2:n{y2}"

)

print

(

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

]

)

print

(

"nrow_stack is equivalent to vstack:"

)

print

(

f

"  vstack: {result_vstack.tolist()}"

)

print

(

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]]


PyTorch torch reference manual PyTorch torch reference manual