训练报错TypeError: conv2d() received an invalid combination of arguments解决方法

报错如下:

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)  # 注意看,这行代码叫小帅,他刚刚多加了一个斜杠

改后解决

后记

其实这个有人提到过传入参数格式不对,帖子如下:

https://www.jianshu.com/p/749439fb026d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

但我扫了一眼没有重视。首先以为是参数初始化问题,然后又以为是预处理时传入的数据的问题,搞来搞去,最后,我自己一步步搭简单网络试着输出,就明白问题在哪里了

建议就是,

自己定位问题时,可以先从确定的下手,一步步搭建简单的网络和你的网络一样,看这些步骤有没有错,最后就可以定位到你出错的地方了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值