Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeError: expected stride to be a single integer value

Tags:

python

pytorch

I am new at Pytorch sorry for the basic question. The model gives me dimension mismatch error how to solve this ? Maybe more than one problems in it. Any help would be appriciated. Thanks

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)
like image 503
John Thomas Avatar asked Mar 16 '26 23:03

John Thomas


1 Answers

Please have a look at the corrected code. I numbered the lines where I did corrections and described them below.

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. First of all, pytorch handles image tensors (you perform 2d convolution therefore I assume this is an image input) as follows: [batch_size x image_depth x height width]
  2. It is important to understand how the convolution with kernel, padding and stride works. In your case kernel_size is 5 and you have no padding (and stride 1). This means that the dimensions of the feature-map gets reduced (as depicted). In your case the first conv. layer takes a 3x28x28 tensor and produces a 6x24x24 tensor, the second one takes 6x24x24 out 1x23x23. I find it very useful to have comments with the in and out tensor dimensions next to the definition conv layers (see in the code above)

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

  1. Here you need to "flatten" the [batch_size x depth x height x width] tensor to [batch_size x fully connected input]. This can be done via tensor.view().

  2. There was a wrong input for the linear layer

  3. Each operation in the forward-pass took the input value x, instead I think you might want to pass the results of each layer to the next one

Altough this code is now runnable, it does not mean that it makes perfect sense. The most important thing (for neural networks in general i would say) are activation functions. These are missing completely.

For getting started with neural networks in pytorch I can highly recommend the great pytorch tutorials: https://pytorch.org/tutorials/ (I would start with the 60min blitz tutorial)

Hope this helps!

like image 58
luk Avatar answered Mar 18 '26 13:03

luk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!