当前位置: 代码迷 >> 综合 >> Pytorch RuntimeError: Expected 4-dimensional input for 4-dimensional weight [512, 512, 3, 3], but go
  详细解决方案

Pytorch RuntimeError: Expected 4-dimensional input for 4-dimensional weight [512, 512, 3, 3], but go

热度:43   发布时间:2023-12-15 16:07:01.0

报错:

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [512, 512, 3, 3], but got 2-dimensional input of size [4, 512] instead

conv2d卷积层输入大小不匹配
要求输入 [512, 512, 3, 3]
实际输入 [4, 512]

临时解法:把2d tensor expand成4d

someTensor = someTensor.unsqueeze(2).unsqueeze(3)

效果如下:

>>> a.shape
torch.Size([2, 2])
>>> a.unsqueeze(2).shape
torch.Size([2, 2, 1])
>>> a.unsqueeze(2).unsqueeze(3).shape
torch.Size([2, 2, 1, 1])

https://stackoverflow.com/questions/57237381/runtimeerror-expected-4-dimensional-input-for-4-dimensional-weight-32-3-3-but

  相关解决方案