1. 程式人生 > >torch.cat(inputs, dimension=0)

torch.cat(inputs, dimension=0)

引數:

  • inputs (sequence of Tensors) – 可以是任意相同Tensor 型別的python 序列
  • dimension (int, optional) – 沿著此維連線張量序列

程式碼

import torch
x = torch.randn(2, 3)
print(torch.cat((x, x, x)))    #預設按行連線張量
print(torch.cat((x, x, x), 0)) #按行連線張量
print(torch.cat((x, x, x), 1)) #按列連線張量

執行結果:

tensor([[-0.4339, -0.2350, -2.1694],
        [ 1.2595, -0.8431,  0.4704],
        [-0.4339, -0.2350, -2.1694],
        [ 1.2595, -0.8431,  0.4704],
        [-0.4339, -0.2350, -2.1694],
        [ 1.2595, -0.8431,  0.4704]])
tensor([[-0.4339, -0.2350, -2.1694],
        [ 1.2595, -0.8431,  0.4704],
        [-0.4339, -0.2350, -2.1694],
        [ 1.2595, -0.8431,  0.4704],
        [-0.4339, -0.2350, -2.1694],
        [ 1.2595, -0.8431,  0.4704]])
tensor([[-0.4339, -0.2350, -2.1694, -0.4339, -0.2350, -2.1694, -0.4339, -0.2350,
         -2.1694],
        [ 1.2595, -0.8431,  0.4704,  1.2595, -0.8431,  0.4704,  1.2595, -0.8431,
          0.4704]])