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

PyTorch torch.stack function

PyTorch torch.stack function


PyTorch torch reference manual PyTorch torch reference manual

torch.stack is a function in PyTorch used to stack multiple tensors along a new dimension. It creates a new dimension and places all input tensors along this new dimension.

In deep learning, this is commonly used for creating batched data, stacking outputs from multiple models, and similar scenarios.

Function definition

torch.stack(tensors, dim=0, out=None)

Parameters:

Return Value:


Usage examples

Example 1: Basic stacking

Example

import

torch

Create two one-dimensional tensors

a

=

torch.

tensor

(

[

1

,

2

,

3

]

)

b

=

torch.

tensor

(

[

4

,

5

,

6

]

)

Stack along a new dimension

c

=

torch.

stack

(

[

a

,

b

]

)

print

(

"Shape of a:"

,

a.

shape

)

print

(

"Shape of b:"

,

b.

shape

)

print

(

"Shape of c:"

,

c.

shape

)

print

(

c

)

The output is:

a 的形状: torch.Size([3])

b 的形状: torch.Size([3])

c 的形状: torch.Size([2, 3])

tensor([[1, 2, 3],

        [4, 5, 6]])

Example 2: Specify the stacking dimension

Example

import

torch

a

=

torch.

tensor

(

[

1

,

2

,

3

]

)

b

=

torch.

tensor

(

[

4

,

5

,

6

]

)

Stack along dim=1

c

=

torch.

stack

(

[

a

,

b

]

,

dim

=

1

)

print

(

"Shape of c:"

,

c.

shape

)

print

(

c

)

The output is:

c 的形状: torch.Size([3, 2])

tensor([[1, 4],

        [2, 5],

        [3, 6]])

Example 3: Stack multiple tensors

Example

import

torch

Create multiple tensors

tensors

=

[

torch.

tensor

(

[

i

,

i+

1

,

i+

2

]

)

for

i

in

range

(

5

)

]

Stack all tensors

result

=

torch.

stack

(

tensors

)

print

(

"Result shape:"

,

result.

shape

)

print

(

result

)

The output is:

结果形状: torch.Size([5, 3])

tensor([[0, 1, 2],

        [1, 2, 3],

        [2, 3, 4],

        [3, 4, 5],

        [4, 5, 6]])

Example 4: Save multiple states in a neural network

Example

import

torch

Simulate saving loss values across multiple epochs

losses

=

[

]

for

epoch

in

range

(

5

)

:

    loss

=

torch.

tensor

(

[

epoch *

0.1

,

(

epoch +

1

)

0.1

]

)

    losses.

append

(

loss

)

Stack the losses from all epochs

all_losses

=

torch.

stack

(

losses

)

print

(

"Shape of losses for each epoch:"

,

all_losses.

shape

)

print

(

all_losses

)

The output is:

各 epoch 损失形状: torch.Size([5, 2])

tensor([[0.0000, 0.1000],

        [0.1000, 0.2000],

        [0.2000, 0.3000],

        [0.3000, 0.4000],

        [0.4000, 0.5000]])


The difference between torch.stack and torch.cat

Example

import

torch

a

=

torch.

randn

(

2

,

3

)

b

=

torch.

randn

(

2

,

3

)

stack creates a new dimension

stack_result

=

torch.

stack

(

[

a

,

b

]

)

print

(

"Shape of the stack result:"

,

stack_result.

shape

)

cat does not create a new dimension

cat_result

=

torch.

cat

(

[

a

,

b

]

,

dim

=

0

)

print

(

"Shape of the cat result:"

,

cat_result.

shape

)

The output is:

stack 结果形状: torch.Size([2, 2, 3])

cat 结果形状: torch.Size([4, 3])


PyTorch torch reference manual PyTorch torch reference manual