torch.cat¶
- torch.cat(tensors, dim=0, *, out=None) Tensor ¶
在给定维度中连接
tensors
中给定的张量序列。所有张量必须具有相同的形状(连接维度除外),或者是大小为(0,)
的 1-D 空张量。torch.cat()
可以看作是torch.split()
和torch.chunk()
的逆运算。torch.cat()
可以通过示例最好地理解。另请参阅
torch.stack()
沿新维度连接给定的序列。- 参数
tensors (张量的序列) – 相同类型的张量的任何 python 序列。提供的非空张量必须具有相同的形状,连接维度除外。
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]])