RuntimeError:预期的跨度为单个整数值

时间:2018-09-25 17:24:06

标签: python pytorch

我是Pytorch的新手,对基本问题感到抱歉。该模型给我尺寸失配误差该如何解决? 也许不止一个问题。 任何帮助将被申请。 谢谢

class PR(nn.Module):
        def __init__(self):
            super(PR, self).__init__()
            self.conv1     = nn.Conv2d(3,6,kernel_size=5)
            self.conv2     = nn.Conv2d(6,1,kernel_size=2)
            self.dens1     = nn.Linear(300, 256)
            self.dens2     = nn.Linear(256, 256)
            self.dens3     = nn.Linear(512, 24)
            self.drop      = nn.Dropout()

        def forward(self, x):
            out = self.conv1(x)
            out = self.conv2(x)
            out = self.dens1(x)
            out = self.dens2(x)
            out = self.dens3(x)
            return out

model = PR()
input = torch.rand(28,28,3)
output = model(input)

2 个答案:

答案 0 :(得分:2)

请查看更正后的代码。我给我进行更正的行编号,并在下面进行描述。

class PR(torch.nn.Module):
    def __init__(self):
        super(PR, self).__init__()
        self.conv1     = torch.nn.Conv2d(3,6, kernel_size=5) # (2a) in 3x28x28 out 6x24x24
        self.conv2     = torch.nn.Conv2d(6,1, kernel_size=2) # (2b) in 6x24x24 out 1x23x23 (6)
        self.dens1     = torch.nn.Linear(529, 256) # (3a)
        self.dens2     = torch.nn.Linear(256, 256)
        self.dens3     = torch.nn.Linear(256, 24) # (4)
        self.drop      = torch.nn.Dropout()

    def forward(self, x):
        out = self.conv1(x) 
        out = self.conv2(out) # (5)

        out = out.view(-1, 529) # (3b)

        out = self.dens1(out)
        out = self.dens2(out)
        out = self.dens3(out)
        return out

model = PR()
ins = torch.rand(1, 3, 28, 28) # (1)
output = model(ins)
  1. 首先,pytorch处理图像张量(您执行2d卷积,因此我认为这是图像输入),如下所示:[batch_size x image_depth x height width]
  2. 了解内核,填充和跨度的卷积如何工作非常重要。在您的情况下,kernel_size为5,并且没有填充(步幅为1)。这意味着特征图的尺寸会减小(如图所示)。您的情况是第一次转化。层采用3x28x28张量并产生6x24x24张量,第二层采用6x24x24张量1x23x23。我发现在conv定义层旁边带有in和out张量维度的注释非常有用(请参见上面的代码)

No padding, stride 1 2-d conv - https://github.com/vdumoulin/conv_arithmetic

  1. 在这里,您需要将[batch_size x深度x高度x宽度]张量“展平”到[batch_size x完全连接的输入]。这可以通过tensor.view()完成。

  2. 线性层输入错误

  3. 前向传递中的每个操作都使用输入值x,相反,我认为您可能希望将每一层的结果传递给下一层

尽管此代码现在可以运行,但这并不意味着它很完美。最重要的是(对于一般的神经网络而言)激活函数。这些完全消失了。

要开始使用pytorch中的神经网络,我强烈推荐出色的pytorch教程:https://pytorch.org/tutorials/(我将从60分钟的闪电战教程开始)

希望这会有所帮助!

答案 1 :(得分:1)

您的代码几乎没有问题。我已经在下面对其进行了检查和更正:

class PR(nn.Module):
        def __init__(self):
            super(PR, self).__init__()
            self.conv1     = nn.Conv2d(3, 6, kernel_size=5)
            self.conv2     = nn.Conv2d(6, 1, kernel_size=2)
            # 300 does not match the shape of the previous layer's output,
            # for the specified input, the output of conv2 is [1, 1, 23, 23]
            # this output should be flattened before feeding it to the dense layers
            # the shape then becomes [1, 529], which should match the input shape of dens1
            # self.dens1     = nn.Linear(300, 256)
            self.dens1     = nn.Linear(529, 256)
            self.dens2     = nn.Linear(256, 256)
            # The input should match the output of the previous layer, which is 256
            # self.dens3     = nn.Linear(512, 24)
            self.dens3     = nn.Linear(256, 24)
            self.drop      = nn.Dropout()

        def forward(self, x):
            # The output of each layer should be fed to the next layer
            x = self.conv1(x)
            x = self.conv2(x)
            # The output should be flattened before feeding it to the dense layers
            x = x.view(x.size(0), -1)
            x = self.dens1(x)
            x = self.dens2(x)
            x = self.dens3(x)
            return x

model = PR()
# The input shape should be (N,Cin,H,W)
# where N is the batch size, Cin is input channels, H and W are height and width respectively
# so the input should be torch.rand(1,3,28,28)
# input = torch.rand(28,28,3)
input = torch.rand(1, 3, 28, 28)
output = model(input)

如果您有任何后续问题,请告诉我。