捷徑

torch.stack

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

沿著一個新的維度串聯一系列的張量。

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

另請參閱

torch.cat() 沿著現有的維度串聯給定的序列。

參數
  • tensors (Tensors 的序列) – 要串聯的張量序列

  • dim (int, optional) – 要插入的維度。必須介於 0 和串聯張量的維度數量(包括 0 和維度數量)之間。預設值:0

關鍵字引數

out (Tensor, optional) – 輸出張量。

範例

>>> 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]]])

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources