参数:
- inputs (sequence of Tensors) – 可以是任意相同Tensor 类型的python 序列
- dimension (int, optional) – 沿着此维连接张量序列
代码
import torch
x = torch.randn(2, 3)
print(torch.cat((x, x, x))) #默认按行连接张量
print(torch.cat((x, x, x), 0)) #按行连接张量
print(torch.cat((x, x, x), 1)) #按列连接张量
运行结果:
tensor([[-0.4339, -0.2350, -2.1694],
[ 1.2595, -0.8431, 0.4704],
[-0.4339, -0.2350, -2.1694],
[ 1.2595, -0.8431, 0.4704],
[-0.4339, -0.2350, -2.1694],
[ 1.2595, -0.8431, 0.4704]])
tensor([[-0.4339, -0.2350, -2.1694],
[ 1.2595, -0.8431, 0.4704],
[-0.4339, -0.2350, -2.1694],
[ 1.2595, -0.8431, 0.4704],
[-0.4339, -0.2350, -2.1694],
[ 1.2595, -0.8431, 0.4704]])
tensor([[-0.4339, -0.2350, -2.1694, -0.4339, -0.2350, -2.1694, -0.4339, -0.2350,
-2.1694],
[ 1.2595, -0.8431, 0.4704, 1.2595, -0.8431, 0.4704, 1.2595, -0.8431,
0.4704]])