PyTorch torch.stack function
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:
tensors(Sequence of Tensor): The sequence of tensors to stack. All tensors must have the same shape.dim(int, optional): The dimension along which to stack, defaulting to 0. The new dimension is inserted at this position.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the stacked tensor.
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
]
)
(
"Shape of a:"
,
a.
shape
)
(
"Shape of b:"
,
b.
shape
)
(
"Shape of c:"
,
c.
shape
)
(
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
)
(
"Shape of c:"
,
c.
shape
)
(
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
)
(
"Result shape:"
,
result.
shape
)
(
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
)
(
"Shape of losses for each epoch:"
,
all_losses.
shape
)
(
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
]
)
(
"Shape of the stack result:"
,
stack_result.
shape
)
cat does not create a new dimension
cat_result
=
torch.
cat
(
[
a
,
b
]
,
dim
=
0
)
(
"Shape of the cat result:"
,
cat_result.
shape
)
The output is:
stack 结果形状: torch.Size([2, 2, 3])
cat 结果形状: torch.Size([4, 3])
torch.stack: Stacks along a new dimension; input tensors must have exactly the same shape, and a new dimension is added.torch.cat: Concatenates along an existing dimension; input tensors may differ in the concatenation dimension, and no new dimension is added.