torch.cat可以直接用来连接多个tensor,不需要两个两个之间相互concatenate。话不多说,下面写个例程演示一下:
In [1]: import torch
In [2]: a = torch.ones(3,4)
In [3]: b = 2*torch.ones(3,4)
In [4]: c = 3*torch.ones(3,4)
In [5]: d = torch.cat((a,b,c),axis = 1)
In [6]: e = torch.cat((a,b,c),axis = 0)
In [7]: d
Out[7]:
tensor([[1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.],
[1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.],
[1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.]])
In [8]: e
Out[8]:
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])