问题描述
我正在doc
上关注此示例In [42]: x = torch.tensor([1,2,3])
In [45]: x.repeat(4,2)
Out[45]: tensor([[1,3,1,3],[1,3]])
In [46]: x.repeat(4,2).shape
Out[46]: torch.Size([4,6])
到目前为止,很好。
但是为什么在3维上仅重复1次,会使3d暗淡扩展为3(不是1)?
[On the doc]
>>> x.repeat(4,1).size()
torch.Size([4,3])
仔细检查。
In [43]: x.repeat(4,1)
Out[43]:
tensor([[[1,3]],[[1,3]]])
为什么会这样?
解决方法
它仅在第一次变暗时将size([3])张量扩展一次。 (4,2,1)是您要重复(3,)张量的次数。因此,最终张量为(4,3),因为您对最后一个轴重复了(3,)一次,对第二个倒数重复了两次,对第一个轴重复了4次。
x = torch.tensor([1,3])
x.shape
torch.Size([3])
然后
xx = x.repeat(4,1)
xx.shape
torch.Size([4,3])