torch的拼接函数_Pytorch中的torch.cat()函数
cat( )的用法
按維數0拼接(豎著拼)
C = torch.cat( (A,B),0 )
按維數1拼接(橫著拼)
C = torch.cat( (A,B),1 )
按維數0拼接
A=torch.ones(2,3) #2x3的張量(矩陣)
print("A:\n",A,"\nA.shape:\n",A.shape,"\n")
B=2*torch.ones(4,3) #4x3的張量(矩陣)
print("B:\n",B,"\nB.shape:\n",B.shape,"\n")
C=torch.cat((A,B),0) #按維數0(行)拼接
print("C:\n",C,"\nC.shape:\n",C.shape,"\n")
A:
tensor([[1., 1., 1.],
[1., 1., 1.]])
A.shape:
torch.Size([2, 3])
B:
tensor([[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]])
B.shape:
torch.Size([4, 3])
C:
tensor([[1., 1., 1.],
[1., 1., 1.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]])
C.shape:
torch.Size([6, 3])
按維數1拼接
A=torch.ones(2,3) #2x3的張量(矩陣)
print("A:\n",A,"\nA.shape:\n",A.shape,"\n")
B=2*torch.ones(2,4) #4x3的張量(矩陣)
print("B:\n",B,"\nB.shape:\n",B.shape,"\n")
C=torch.cat((A,B),1) #按維數0(行)拼接
print("C:\n",C,"\nC.shape:\n",C.shape,"\n")
A:
tensor([[1., 1., 1.],
[1., 1., 1.]])
A.shape:
torch.Size([2, 3])
B:
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.]])
B.shape:
torch.Size([2, 4])
C:
tensor([[1., 1., 1., 2., 2., 2., 2.],
[1., 1., 1., 2., 2., 2., 2.]])
C.shape:
torch.Size([2, 7])
總結
以上是生活随笔為你收集整理的torch的拼接函数_Pytorch中的torch.cat()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自己动手,组建简单好用的NAS!(上)
- 下一篇: 新建HttpServletRequest