简体   繁体   English

RuntimeError:张量 a (38) 的大小必须与非单维 3 处的张量 b (34) 的大小相匹配

[英]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.conv1self.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.

相关问题 RuntimeError:张量 a (128) 的大小必须与非单维 3 的张量 b (256) 的大小相匹配 - RuntimeError: The size of tensor a (128) must match the size of tensor b (256) at non-singleton dimension 3 RuntimeError:张量 a (4000) 的大小必须与非单维 1 的张量 b (512) 的大小相匹配 - RuntimeError: The size of tensor a (4000) must match the size of tensor b (512) at non-singleton dimension 1 对于 SNN,如何解决`RuntimeError:张量 a (3) 的大小必须与非单维 1 的张量 b (128) 的大小相匹配? - How to resolve `RuntimeError: The size of tensor a (3) must match the size of tensor b (128) at non-singleton dimension 1` for SNN? Pytorch RuntimeError:张量 a (120000) 的大小必须与张量 b (2) 在非单维 2 的大小匹配 - Pytorch RuntimeError: The size of tensor a (120000) must match the size of tensor b (2) at non-singleton dimension 2 导入透明图像会出现 RuntimeError:张量 a (4) 的大小必须与非单维 0 处的张量 b (3) 的大小匹配 - Importing Transparent images gives RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 PyTorch:RuntimeError:张量 a (224) 的大小必须与非单维 3 的张量 b (244) 的大小相匹配 - PyTorch: RuntimeError: The size of tensor a (224) must match the size of tensor b (244) at non-singleton dimension 3 运行时错误:张量 a (1024) 的大小必须与非单维 3 处的张量 b (512) 的大小匹配 - RuntimeError: The size of tensor a (1024) must match the size of tensor b (512) at non-singleton dimension 3 Pytorch RuntimeError:张量 a (4) 的大小必须与非单维 0 处的张量 b (3) 的大小相匹配 - Pytorch RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 RuntimeError:张量 a (80) 的大小必须与非单维 3 处的张量 b (56) 的大小匹配 - RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton dimension 3 训练 CNN 时出错:“RuntimeError:张量 a (10) 的大小必须与非单维 1 的张量 b (64) 的大小相匹配” - Error when training CNN: "RuntimeError: The size of tensor a (10) must match the size of tensor b (64) at non-singleton dimension 1"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM