Shortcuts

torch.cat

torch.cat(tensors, dim=0, *, out=None) 张量

在给定的维度上连接给定的 seq 张量序列。 所有张量必须具有相同的形状(在连接维度上除外),或者是一个大小为 (0,) 的 1-D 空张量。

torch.cat() 可以被视为 torch.split()torch.chunk() 的逆操作。

torch.cat() 可以通过示例最好地理解。

另请参阅

torch.stack() 沿一个新维度连接给定的序列。

Parameters
  • 张量序列张量)—— 任何相同类型的张量的 Python 序列。 提供的非空张量必须具有相同的形状,除了在 cat 维度上。

  • dim (int, 可选) – 张量连接的维度

Keyword Arguments

输出 (张量, 可选) – 输出张量。

示例:

>>> 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]])
优云智算