Skip to content
Advertisement

mat1 and mat2 shapes cannot be multiplied (128×4 and 128×64)

Could not find out why the mat1 from the convolutional network is 128×4 and not 4×128. The following is the convolutional network used:

JavaScript

The model training code is as follows:

JavaScript

The error log shown is:

JavaScript

mat1 should be the output of the convolutional network after it is flattened, and mat2 is the linear network following it. Appreciate any help. Thanks!

Advertisement

Answer

Here are the output shapes for each layer

JavaScript

You’ll need to change the kernel parameters and padding sizes if you wish to obtain an output of a given shape. This link might help in calculating the output shapes after each layer.

Another approach is that you could take a transpose of the flattened array and pass it into the Linear layers. You’ll need to add the line in your forward function like below

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement