torch.cat()
可以对tensor进行上下或左右拼接,其传入的参数为:
torch.cat(tensors, dim=0)
dim
控制拼接的维度,dim=0
为上下拼接,dim=1
为左右拼接[1]。
下面是实例详解:
>>> import torch
>>> a = torch.rand((2,3))
>>> a
tensor([[0.7515, 0.1021, 0.0726],
[0.0575, 0.1666, 0.2763]])
>>> b = torch.rand((2,3))
>>> b
tensor([[0.7485, 0.8340, 0.2617],
[0.7847, 0.2847, 0.3445]])
>>> c =torch.cat((a,b),dim=0)
>>> c
tensor([[0.7515, 0.1021, 0.0726],
[0.0575, 0.1666, 0.2763],
[0.7485, 0.8340, 0.2617],
[0.7847, 0.2847, 0.3445]])
>>> d = torch.cat((a,b),dim=1)
>>> d
tensor([[0.7515, 0.1021, 0.0726, 0.7485, 0.8340, 0.2617],
[0.0575, 0.1666, 0.2763, 0.7847, 0.2847, 0.3445]])
网友评论