How to resolve the error that says "shapes cannot be multiplied"

Viewed 80

I tried the code mentioned in the article in google colab.

https://theaisummer.com/spiking-neural-networks/

I got the error that looks like this...

Test loss:  8.86368179321289
Test loss:  5.338221073150635
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-9-646cb112ccb7> in <module>()
     15         # forward pass
     16         net.train()
---> 17         spk_rec, mem_rec = net(data.view(batch_size, -1))
     18 
     19         # initialize the loss & sum over time

4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x588 and 784x1000)

I am not sure how to fix it.

1 Answers

I just ran their Colab notebook and ran into the same error. It occurs because the final iteration does not have 128 samples of data, as the total dataset size (60000 and 10000 for training and test set) is not evenly divisible by 128. So there is some left over, and reshaping it to 128 x ... leads to a mismatch of dimensions between input data and the number of neurons in the input layer.

There are two possible fixes.

  1. Just drop the final batch:
train_loader = DataLoader(mnist_train, batch_size=batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(mnist_test, batch_size=batch_size, shuffle=True, drop_last=True)
  1. Don't drop the final batch. But flatten the tensor in a way that preserves the original batch_size, instead of forcing it to 128:

spk_rec, mem_rec = net(data.flatten(1))

Related