torch.cat()

import torch
a = torch.tensor([[1,2,3],[4,5,6]])
print(a.size())
b = torch.tensor([[7,8,9]])
print(b.size())
c = torch.cat((a,b),dim=0)
print(c.size())
print(c)


torch.Size([2, 3])
torch.Size([1, 3])
torch.Size([3, 3])
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
import torch
a = torch.tensor([[1,2],[5,6]])
print(a.size())
b = torch.tensor([[7,8,9],[10,11,12]])
print(b.size())
c = torch.cat((a,b),dim=1)
print(c.size())
print(c)


torch.Size([2, 2])
torch.Size([2, 3])
torch.Size([2, 5])
tensor([[ 1,  2,  7,  8,  9],
        [ 5,  6, 10, 11, 12]])

torch.cat(inputs,dim)

imputs:要合并的tensor。要求剔除dim维度之后,所有tensor的形状要相同。

dim:所有tensor要在dim维度上合并,tensor 在dim上的数不一定要一致。

你可能感兴趣的:(pytorch)