报错如下:
Input In [7], in <cell line: 1>()
7 print(f'{name} - {param.type()} - {param.shape}')
8 # print(torch.unsqueeze(x, 0).shape)
9 # out = model(torch.unsqueeze(x, 0))
---> 10 out = model(x)
11 print("out: ", out)
12 los = criterion(out, y)
# 说明是模型的问题
...
Input In [1], in Net.forward(self, x)
188 if 'bias' in name:
189 print(f'{name} - {param.type()} - {param.shape}')
--> 190 f3 = self.relu(self.f3(x))
191 f5 = self.relu(self.f5(x))
192 f7 = self.relu(self.f7(x))
# 说明是模型具体的块出现了问题
...
TypeError: conv2d() received an invalid combination of arguments - got (Tensor, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (Tensor, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (Tensor, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
解决方法:
检查模块中conv2d传入的参数是否正确,比如我传错的地方就是
nn.Conv2d(1, 32, K, padding=(K-3)/2)
其中,padding应该传入整数,这里传的是小数,所以改为
nn.Conv2d(1, 32, K, padding=(K-3)//2) # 注意看,这行代码叫小帅,他刚刚多加了一个斜杠
改后解决
后记
其实这个有人提到过传入参数格式不对,帖子如下:
但我扫了一眼没有重视。首先以为是参数初始化问题,然后又以为是预处理时传入的数据的问题,搞来搞去,最后,我自己一步步搭简单网络试着输出,就明白问题在哪里了。
建议就是,
自己定位问题时,可以先从确定的下手,一步步搭建简单的网络到和你的网络一样,看这些步骤有没有错,最后就可以定位到你出错的地方了。