PyTorch torch.cat function
PyTorch torch reference manual
torch.cat is a function in PyTorch used to concatenate multiple tensors along a specified dimension. It joins multiple tensors along the specified dimension into a larger tensor.
This is a very common operation in deep learning, for example when concatenating feature maps or joining batches of data.
Function definition
torch.cat(tensors, dim=0, out=None)
Parameters:
tensors(Sequence of Tensor): The sequence of tensors to concatenate. All tensors must have the same shape in every dimension except the concatenation dimension.dim(int, optional): The dimension along which to concatenate; defaults to 0.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the concatenated tensor.
Usage examples
Example 1: Concatenate along the first dimension
Example
import
torch
Create two tensors
a
=
torch.
randn
(
2
,
3
)
b
=
torch.
randn
(
2
,
3
)
Concatenate along the first dimension (rows)
c
=
torch.
cat
(
[
a
,
b
]
,
dim
=
0
)
(
"Shape of a:"
,
a.
shape
)
(
"Shape of b:"
,
b.
shape
)
(
"Shape of c:"
,
c.
shape
)
(
c
)
The output is:
a 的形状: torch.Size([2, 3])
b 的形状: torch.Size([2, 3])
c 的形状: torch.Size([4, 3])
tensor([[ 0.2532, 0.3643, 0.5341],
[ 0.9578, 0.9086, -0.2847],
[-0.7108, -0.0142, 0.7168],
[-0.1542, -0.9841, -1.4945]])
Example 2: Concatenate along the second dimension
Example
import
torch
Create two tensors
a
=
torch.
randn
(
2
,
3
)
b
=
torch.
randn
(
2
,
4
)
Concatenate along the second dimension (columns)
c
=
torch.
cat
(
[
a
,
b
]
,
dim
=
1
)
(
"Shape of a:"
,
a.
shape
)
(
"Shape of b:"
,
b.
shape
)
(
"Shape of c:"
,
c.
shape
)
The output is:
a 的形状: torch.Size([2, 3])
b 的形状: torch.Size([2, 4])
c 的形状: torch.Size([2, 7])
Example 3: Concatenate multiple tensors
Example
import
torch
Create multiple tensors
a
=
torch.
tensor
(
[
1
,
2
]
)
b
=
torch.
tensor
(
[
3
,
4
]
)
c
=
torch.
tensor
(
[
5
,
6
]
)
Concatenate multiple tensors
result
=
torch.
cat
(
[
a
,
b
,
c
]
)
(
result
)
The output is:
tensor([1, 2, 3, 4, 5, 6])
示例 4: 在神经网络中拼接特征
实例
import torch
# 模拟来自不同层的特征图
feature1 = torch.randn(1, 64, 32, 32) # 来自第一层的特征
feature2 = torch.randn(1, 128, 32, 32) # 来自第二层的特征
# 在通道维度(dim=1)拼接特征
combined = torch.cat([feature1, feature2], dim=1)
print("特征1 形状:", feature1.shape)
print("特征2 形状:", feature2.shape)
print("拼接后形状:", combined.shape)
输出结果为:
特征1 形状: torch.Size([1, 64, 32, 32])
特征2 形状: torch.Size([1, 128, 32, 32])
拼接后形状: torch.Size([1, 192, 32, 32])
In neural networks, torch.cat Commonly used in structures such as the feature pyramid (FPN) to fuse features from different layers.
The difference between torch.cat and torch.stack
torch.cat: Concatenate along an existing dimension, the tensor shape will be added along the concatenation dimension.torch.stack: stack along a new dimension, which adds a new dimension.
Example
import
torch
a
=
torch.
randn
(
2
,
3
)
b
=
torch.
randn
(
2
,
3
)
The difference between cat and stack
cat_result
=
torch.
cat
(
[
a
,
b
]
,
dim
=
0
)
stack_result
=
torch.
stack
(
[
a
,
b
]
,
dim
=
0
)
(
"cat result shape:"
,
cat_result.
shape
)
(4, 3)
(
"stack result shape:"
,
stack_result.
shape
)