Pytorch 尺寸变化

问题描述

是否有任何方法可以将 [1,512,1,1] 更改为 [1,2,2] 张量。 我知道仅通过更改尺寸是不可能的。 有没有什么方法可以在 PyTorch 中使用 concat 或 stack(torch.stack、torch.cat)

我使用以下代码制作张量

a = torch.rand([1,1])

如何将其更改为维度为 [1,2] 的张量

解决方法

我试过了

tmp = torch.cat([a,a],2)
a = torch.cat([tmp,tmp],3)
,

那就是torch.repeat,这将复制数据:

>>> a = a.repeat(1,1,2,2)

如果您不想复制数据,请使用torch.expand

>>> a = a.expand(-1,-1,2)