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

PyTorch torch.column_stack function

PyTorch torch.column_stack function


PyTorch torch reference manual PyTorch torch reference manual

torch.column_stack is a function in PyTorch used to stack tensors column by column.

Function definition

torch.column_stack(tensors, *, out=None)


Usage examples

Example

import

torch

Column-wise stacking of 1D tensors

x1

=

torch.

tensor

(

[

1

,

2

,

3

]

)

x2

=

torch.

tensor

(

[

4

,

5

,

6

]

)

result

=

torch.

column_stack

(

[

x1

,

x2

]

)

print

(

"Column-wise stacking of 1D tensors:"

)

print

(

f

"  x1: {x1}"

)

print

(

f

"  x2: {x2}"

)

print

(

f

"  column_stack:n{result}"

)

Column-wise stacking of 2D tensors

y1

=

torch.

tensor

(

[

[

1

]

,

[

2

]

,

[

3

]

]

)

y2

=

torch.

tensor

(

[

[

4

]

,

[

5

]

,

[

6

]

]

)

result

=

torch.

column_stack

(

[

y1

,

y2

]

)

print

(

"nColumn-wise stacking of 2D tensors:"

)

print

(

f

"  y1:n{y1}"

)

print

(

f

"  y2:n{y2}"

)

print

(

f

"  column_stack:n{result}"

)

Column-wise stacking of multiple tensors

z1

=

torch.

tensor

(

[

1

,

2

,

3

]

)

z2

=

torch.

tensor

(

[

4

,

5

,

6

]

)

z3

=

torch.

tensor

(

[

7

,

8

,

9

]

)

result

=

torch.

column_stack

(

[

z1

,

z2

,

z3

]

)

print

(

"nColumn-wise stacking of multiple 1D tensors:"

)

print

(

f

"  result:n{result}"

)

The output is:

一维张量列堆叠:

  x1: tensor([1, 2, 3])

  x2: tensor([4, 5, 6])

  column_stack:

tensor([[1, 4],

        [2, 5],

        [3, 6]])



Column stacking of 2D tensors:

  y1:

tensor([[1],

        [2],

        [3]])

  y2:

tensor([[4],

        [5],

        [6]])

  column_stack:

tensor([[1, 4],

        [2, 5],

        [3, 6]])



Stack multiple 1D tensors column-wise:

  result:

tensor([[1, 4, 7],

        [2, 5, 8],

        [3, 6, 9]])


PyTorch torch reference manual PyTorch torch reference manual