PyTorch中Tensor的拼接与拆分的实现-创新互联
拼接张量:torch.cat() 、torch.stack()

- torch.cat(inputs, dimension=0) → Tensor
在给定维度上对输入的张量序列 seq 进行连接操作
举个例子:
>>> import torch
>>> x = torch.randn(2, 3)
>>> x
tensor([[-0.1997, -0.6900, 0.7039],
[ 0.0268, -1.0140, -2.9764]])
>>> torch.cat((x, x, x), 0) # 在 0 维(纵向)进行拼接
tensor([[-0.1997, -0.6900, 0.7039],
[ 0.0268, -1.0140, -2.9764],
[-0.1997, -0.6900, 0.7039],
[ 0.0268, -1.0140, -2.9764],
[-0.1997, -0.6900, 0.7039],
[ 0.0268, -1.0140, -2.9764]])
>>> torch.cat((x, x, x), 1) # 在 1 维(横向)进行拼接
tensor([[-0.1997, -0.6900, 0.7039, -0.1997, -0.6900, 0.7039, -0.1997, -0.6900,
0.7039],
[ 0.0268, -1.0140, -2.9764, 0.0268, -1.0140, -2.9764, 0.0268, -1.0140,
-2.9764]])
>>> y1 = torch.randn(5, 3, 6)
>>> y2 = torch.randn(5, 3, 6)
>>> torch.cat([y1, y2], 2).size()
torch.Size([5, 3, 12])
>>> torch.cat([y1, y2], 1).size()
torch.Size([5, 6, 6]) 分享题目:PyTorch中Tensor的拼接与拆分的实现-创新互联
网页网址:http://www.jxjierui.cn/article/dpjphi.html


咨询
建站咨询
