Output size is too small at SpatialConvolutionMM.c:45 error

Hey guys, I’m new at pytorch and cnn as well, so I dont know if I am being stupid. I’m getting this error:

RuntimeError: Given input size: (4 x 80 x 4). Calculated output size: (32 x 19 x 0). Output size is too small at /.../SpatialConvolutionMM.c:45

Here what I am doing in my code:
self.layer1 = nn.Sequential( nn.Conv2d(4, 32, kernel_size=8, stride=4), nn.BatchNorm2d(32), nn.ReLU())
self.layer2 = nn.Sequential( nn.Conv2d(32, 64, kernel_size=4, stride=2), nn.BatchNorm2d(64), nn.ReLU())
self.layer3 = nn.Sequential( nn.Conv2d(64, 64, kernel_size=3, stride=1), nn.BatchNorm2d(64), nn.ReLU())

And my input is a Tensor of shape [1 x 80 x 80 x 4]. What Am I doing wrong?


I think it literally means what it says.
Based on your input Tensor’s size, I would venture that you might double check that you are using pytorch’s dimension convention of Batch x Channels x Height x Width. Personally, I usually find the pytorch documentation helpful, it gives details about the dimensions in most layers, e.g. for Conv2d.

Best regards


First, thanks for your reply. I am following a pytorch implementation (https://github.com/transedward/pytorch-dqn/blob/master/dqn_model.py) and these Stanford lectures notes (http://cs231n.github.io/convolutional-networks/). I cant figure out what I am doing wrong.

It kinda looks like your input tensor might be the wrong size - Conv2d expects a tensor of size (n, k, h, w) where in your case that’s (1, 4, 80, 80) assuming your input is 80x80 and has 4 channels which is what it looks like. I would recommend checking your tensor shape and making sure it’s in the right format :slight_smile:

It turns out I was following other stuffs and by the end mix things up a little bit. My input Tensor didnt had size 1 x 4 x 80 x 80, but 1 x 80 x 80 x 4 instead. Thanks a lot :smiley:

Hey, how did you reshape this input to 1 x 4 x 80 x 80? I have the same problem. :frowning:

i am getting same error too.

Given input size: (512x1x5). Calculated output size: (512x0x2). Output size is too small at /pytorch/aten/src/THNN/generic/SpatialDilatedMaxPooling.c