【发布时间】:2018-10-08 22:42:14
【问题描述】:
我现在在 windows 中使用 pytorch 0.4.0 来构建 CNN,这是我的代码:
class net(nn.Module):
def __init__(self):
super(net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(1,3),stride=1 )
self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=(1,3), stride=1)
self.dense1 = nn.Linear(32 * 28 * 24, 60)
self.out = nn.Linear(60,3)
def forward(self, input):
x = F.relu(self.conv1(input))
x = F.relu(self.conv2(x))
x = x.view(x.size(0), -1) # flatten(batch,32*7*7)
x = self.dense1(x)
output = self.out(x)
return output
但我得到 错误
File "D:\Anaconda\lib\site-packages\torch\nn\modules\conv.py", line 301, in forward
self.padding, self.dilation, self.groups)
RuntimeError: expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]
我认为这表明我在上面的代码中犯了一些错误,但我不知道如何修复它,任何人都可以帮助我吗?提前致谢!
【问题讨论】:
-
你的错误是什么时候产生的?我无法重现它,无论是向前还是向后传递。
-
保存模型然后重新加载后,我遇到了同样的问题(Anaconda、Python 3.6、pytorch 0.4.0)。
-
好的,所以对我来说,当我喂一个单品时它缺少批次维度。见this。
-
我发现是因为我输入的数据类型,我的数据应该是DoubleTensor但我用的是FloatTensor,同样当维度不匹配时,也会出现这个错误。
-
顺便说一句,回答和接受你自己的问题完全没问题,它可能对未来的读者有所帮助。 :)
标签: python pytorch stride convolutional-neural-network