torch.cat¶
- torch.cat(tensors, dim=0, *, out=None) Tensor ¶
在给定维度上拼接输入序列
tensors
中的张量。所有张量除了拼接维度外必须具有相同的形状,或者是一个大小为(0,)
的一维空张量。torch.cat()
可以被视为torch.split()
和torch.chunk()
的逆操作。torch.cat()
通过示例最容易理解。另请参阅
torch.stack()
沿着一个新维度拼接输入序列。- 参数
tensors (Tensor 序列) – 提供的非空张量除了拼接维度外必须具有相同的形状。
dim (int, 可选) – 拼接张量的维度
- 关键字参数
out (Tensor, 可选) – 输出张量。
示例
>>> x = torch.randn(2, 3) >>> x tensor([[ 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497]]) >>> torch.cat((x, x, x), 0) tensor([[ 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497], [ 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497], [ 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497]]) >>> torch.cat((x, x, x), 1) tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497]])