快捷方式

torch.stack

torch.stack(tensors, dim=0, *, out=None) Tensor

沿新维度连接张量序列。

所有张量需要具有相同的大小。

另请参阅

torch.cat() 沿现有维度连接给定的序列。

参数
  • tensors (张量序列 of Tensors) – 要连接的张量序列

  • dim (int, 可选) – 要插入的维度。必须介于 0 和连接张量的维度数之间(包括端点)。默认值:0

关键字参数

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

示例

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.3367,  0.1288,  0.2345],
        [ 0.2303, -1.1229, -0.1863]])
>>> torch.stack((x, x)) # same as torch.stack((x, x), dim=0)
tensor([[[ 0.3367,  0.1288,  0.2345],
         [ 0.2303, -1.1229, -0.1863]],

        [[ 0.3367,  0.1288,  0.2345],
         [ 0.2303, -1.1229, -0.1863]]])
>>> torch.stack((x, x)).size()
torch.Size([2, 2, 3])
>>> torch.stack((x, x), dim=1)
tensor([[[ 0.3367,  0.1288,  0.2345],
         [ 0.3367,  0.1288,  0.2345]],

        [[ 0.2303, -1.1229, -0.1863],
         [ 0.2303, -1.1229, -0.1863]]])
>>> torch.stack((x, x), dim=2)
tensor([[[ 0.3367,  0.3367],
         [ 0.1288,  0.1288],
         [ 0.2345,  0.2345]],

        [[ 0.2303,  0.2303],
         [-1.1229, -1.1229],
         [-0.1863, -0.1863]]])
>>> torch.stack((x, x), dim=-1)
tensor([[[ 0.3367,  0.3367],
         [ 0.1288,  0.1288],
         [ 0.2345,  0.2345]],

        [[ 0.2303,  0.2303],
         [-1.1229, -1.1229],
         [-0.1863, -0.1863]]])

文档

访问 PyTorch 的综合开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得您的问题解答

查看资源