PyTorch中conv2d()参数类型不匹配错误排查求助
conv2d参数类型不匹配问题排查与解决
错误原因分析
Block类初始化参数顺序错误
Block类的__init__方法定义参数顺序为(in_channel, out_channel, stride=1, downsample=None),但在ResNet的layers方法中调用Block时,传参顺序写成了block(self.in_channels, out_channels, downsample, stride),将downsample和stride的位置搞反。这导致Block初始化时,stride参数被赋值为downsample(一个Sequential模块或None),而Conv2d的stride参数要求是int或tuple类型,最终在forward阶段调用conv2d时触发参数类型不匹配错误。Block类中的拼写与逻辑错误
- BatchNorm实例拼写错误:
self.batcnorm2应为self.batchnorm2; - forward中重复调用同一BatchNorm:第二次归一化错误使用
self.batchnorm1,而非正确的self.batchnorm2; - 残差边下采样对象错误:对经过卷积的
x而非原始identity进行下采样,导致维度不匹配。
- BatchNorm实例拼写错误:
ResNet类中的多处错误
- forward中大写
X应为小写x; - 全连接层调用错误:不存在
linear_layer1属性,且调用写法错误; - 通道数数值错误:layer2的out_channels写为124,不符合ResNet标准的128;
- AdaptiveAvgPool2d输出未展平,直接传入全连接层会导致维度错误。
- forward中大写
解决方案
1. 修正Block类的调用参数顺序
在ResNet的layers方法中,调整Block的传参顺序,匹配其__init__定义:
# 原错误代码 # layers.append(block(self.in_channels,out_channels,downsample,stride)) # 修改后 layers.append(block(self.in_channels, out_channels, stride, downsample))
2. 修复Block类的拼写与逻辑错误
class blocks(nn.Module): def __init__(self, in_channel, out_channel, stride=1, downsample=None): super(blocks,self).__init__() self.conv1=nn.Conv2d(in_channel,out_channel,kernel_size=3,stride=stride,padding=1) self.batchnorm1=nn.BatchNorm2d(out_channel) self.conv2=nn.Conv2d(out_channel,out_channel,kernel_size=3,padding=1,stride=1) # 修正拼写错误 self.batchnorm2=nn.BatchNorm2d(out_channel) self.relu=nn.ReLU() self.identity_downsample=downsample def forward(self,x): identity=x x=self.conv1(x) x=self.batchnorm1(x) x=self.relu(x) x=self.conv2(x) # 修正BatchNorm调用对象 x=self.batchnorm2(x) if self.identity_downsample is not None: # 修正下采样对象为原始identity identity=self.identity_downsample(identity) x=x+identity x=self.relu(x) return x
3. 修复ResNet类的错误
class ResNet(nn.Module): def __init__(self,block,layers,num_classes=136): super(ResNet,self).__init__() self.in_channels = 64 self.conv1=nn.Conv2d(3,self.in_channels,kernel_size=7,padding=1) self.norm1=nn.BatchNorm2d(self.in_channels) self.relu=nn.ReLU() self.layer1=self.layers(block,64,layers[0]) # 修正通道数为128 self.layer2=self.layers(block,128,layers[1],stride=2) self.layer3=self.layers(block,256,layers[2],stride=2) self.layer4=self.layers(block,512,layers[3],stride=2) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.linear_classifier1=nn.Linear(512,256) self.linear_classifier2=nn.Linear(256,num_classes) def layers(self,block,out_channels,no_of_blocks,stride=1): downsample=None if stride!=1 or self.in_channels!=out_channels: downsample=nn.Sequential( nn.Conv2d(self.in_channels,out_channels,kernel_size=1,stride=stride), nn.BatchNorm2d(out_channels) ) layers=[] # 已修正参数顺序 layers.append(block(self.in_channels,out_channels,stride,downsample)) self.in_channels=out_channels for i in range(1,no_of_blocks): layers.append(block(self.in_channels,out_channels)) return nn.Sequential(*layers) def forward(self,x): x=self.conv1(x) x=self.norm1(x) x=ff.max_pool2d(x,kernel_size=3,stride=2,padding=1) x=self.relu(x) x=self.layer1(x) x=self.layer2(x) x=self.layer3(x) x=self.layer4(x) # 修正大写X为小写x x=self.avgpool(x) # 展平张量,适配全连接层输入 x = x.view(x.size(0), -1) # 修正全连接层调用 x=self.linear_classifier1(x) x=self.relu(x) x=ff.dropout(x,0.4) x=self.linear_classifier2(x) x=ff.sigmoid(x) return x
4. 补充必要导入
确保代码开头导入torch.nn.functional:
import torch.nn.functional as ff
内容的提问来源于stack exchange,提问作者Sachin
相关产品推荐
相关产品推荐

