参数:
- 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]])

本文详细介绍了PyTorch中张量连接的功能与使用方法,通过实例演示了如何使用torch.cat函数沿不同维度连接张量序列,适用于深度学习与数据处理领域的开发者。
7921

被折叠的 条评论
为什么被折叠?



