如何将形状 (3, 1, 2) 的 3D 张量重塑为 (1, 2, 3)
How reshape 3D tensor of shape (3, 1, 2) to (1, 2, 3)
我打算
(Pdb) aa = torch.tensor([[[1,2]], [[3,4]], [[5,6]]])
(Pdb) aa.shape
torch.Size([3, 1, 2])
(Pdb) aa
tensor([[[ 1, 2]],
[[ 3, 4]],
[[ 5, 6]]])
(Pdb) aa.view(1, 2, 3)
tensor([[[ 1, 2, 3],
[ 4, 5, 6]]])
但我真正想要的是
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
如何?
在我的应用程序中,我试图将形状为 (L, N, C_in) 的输入数据转换为 (N, C_in, L) 以便使用 Conv1d,其中
- L:序列长度
- N:批量大小
- C_in:输入的通道数,我也理解为输入在一个序列的每个位置的维数。
我还想知道 Conv1d 的输入与 GRU 的输入形状不同吗?
这是一种方法,还是希望能看到一次操作的解决方案。
(Pdb) torch.transpose(aa, 0, 2).t()
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
(Pdb) torch.transpose(aa, 0, 2).t().shape
torch.Size([1, 2, 3])
可以permute the axes to the desired shape. (This is similar to numpy.moveaxis()
操作)。
In [90]: aa
Out[90]:
tensor([[[ 1, 2]],
[[ 3, 4]],
[[ 5, 6]]])
In [91]: aa.shape
Out[91]: torch.Size([3, 1, 2])
# pass the desired ordering of the axes as argument
# assign the result back to some tensor since permute returns a "view"
In [97]: permuted = aa.permute(1, 2, 0)
In [98]: permuted.shape
Out[98]: torch.Size([1, 2, 3])
In [99]: permuted
Out[99]:
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
我打算
(Pdb) aa = torch.tensor([[[1,2]], [[3,4]], [[5,6]]])
(Pdb) aa.shape
torch.Size([3, 1, 2])
(Pdb) aa
tensor([[[ 1, 2]],
[[ 3, 4]],
[[ 5, 6]]])
(Pdb) aa.view(1, 2, 3)
tensor([[[ 1, 2, 3],
[ 4, 5, 6]]])
但我真正想要的是
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
如何?
在我的应用程序中,我试图将形状为 (L, N, C_in) 的输入数据转换为 (N, C_in, L) 以便使用 Conv1d,其中
- L:序列长度
- N:批量大小
- C_in:输入的通道数,我也理解为输入在一个序列的每个位置的维数。
我还想知道 Conv1d 的输入与 GRU 的输入形状不同吗?
这是一种方法,还是希望能看到一次操作的解决方案。
(Pdb) torch.transpose(aa, 0, 2).t()
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
(Pdb) torch.transpose(aa, 0, 2).t().shape
torch.Size([1, 2, 3])
可以permute the axes to the desired shape. (This is similar to numpy.moveaxis()
操作)。
In [90]: aa
Out[90]:
tensor([[[ 1, 2]],
[[ 3, 4]],
[[ 5, 6]]])
In [91]: aa.shape
Out[91]: torch.Size([3, 1, 2])
# pass the desired ordering of the axes as argument
# assign the result back to some tensor since permute returns a "view"
In [97]: permuted = aa.permute(1, 2, 0)
In [98]: permuted.shape
Out[98]: torch.Size([1, 2, 3])
In [99]: permuted
Out[99]:
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])