[英]RuntimeError: The size of tensor a (38) must match the size of tensor b (34) at non-singleton dimension 3
I studied Resnet 50 using cifar-10我使用 cifar-10 研究了 Resnet 50
but, I faced RuntimeError.但是,我遇到了 RuntimeError。
Here is code这是代码
class BasicBlock(nn.Module):
def __init__(self, in_planes, planes, stride = 1):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size = 1, stride = stride, padding = 1, bias = False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size = 3, stride = 1, padding = 1, bias = False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size = 1, stride = 1, padding = 1, bias = False)
self.bn3 = nn.BatchNorm2d(planes * 4)
if stride != 1:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, planes, kernel_size = 1, stride = stride, bias = False),
nn.BatchNorm2d(planes)
)
else:
self.shortcut = nn.Sequential()
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out = self.bn3(self.conv3(out))
out += self.shortcut(x) #shortcut connection
out = F.relu(out)
and Error is和错误是
RuntimeError: The size of tensor a (38) must match the size of tensor b (34) at non-singleton dimension 3 RuntimeError:张量 a (38) 的大小必须与非单维 3 处的张量 b (34) 的大小相匹配
How can I fix it?我该如何解决?
You don't want padding in your self.conv1
nor self.conv3
, because you'll be increasing your image size by 2 (1 each size) each time.您不希望在
self.conv1
或self.conv3
中进行填充,因为您每次都会将图像大小增加 2(每个大小 1)。 Padding should only be used to avoid reducing your image size when using a kernel size of above 1.仅当使用大于 1 的 kernel 大小时,应使用填充来避免减小图像大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.