文章目录
cat和concat的区别
先说结论:没有区别在功能、用法以及作用上,concat函数就是cat函数的别名(官方就是这样说的)。下面截图为证:
因此接下来就主要是介绍 torch.cat 函数的功能和用法。
torch.cat介绍
参考🔗:link
torch.cat(tensors, dim=0, *, out=None) → Tensor
作用
将给定序列的张量在给定维度上连接起来。所有张量必须具有相同的形状(除了连接维度之外)
,或者是一个尺寸为(0,)的一维空张量。
Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be a 1-D empty tensor with size (0,)
参数
- 第一个参数 tensors :除了要连接的维度外,其他维度的形状都要相同的张量。tensors: Tuple[Tensor, …] | List[Tensor]。写法可以是
(x, x, x)or [x, x, x]
。 - 第二个参数 dim:
(int, optiona)
指定的连接的维度,可选,默认就是 dim=0,表示水平方向上拼接,即行拼接
。这个参数可以是整数,负数,0,以及没有。
- 其他参数不用管。
使用实例
import torch
x = torch.randn(2, 3)
x
# 输出
tensor([[ 1.3524, 0.7867, -0.1423],
[ 1.1235, 0.0221, -0.5478]])
dim=0 表示水平方向的拼接,也就说从shape(2, 3) -> shape(6, 3):
<