mat1 and mat2 shapes cannot be multiplied — Fix
Fix the PyTorch error 'mat1 and mat2 shapes cannot be multiplied'. Paste your error message to see which dimensions don't match and get the exact fix.
Built by Michael Lip
Frequently Asked Questions
What does 'mat1 and mat2 shapes cannot be multiplied' mean?
This error means the inner dimensions of a matrix multiplication don't match. In a Linear layer, this means in_features doesn't match the actual input size. For example, if your input is [64, 512] but Linear expects in_features=256, you get 'mat1 and mat2 shapes cannot be multiplied (64x512 and 256x10)'.
How do I fix this error?
Read the error dimensions: 'mat1 (AxB) and mat2 (CxD)'. B must equal C. If they don't match, change in_features of your Linear layer to match B (the actual input size). Often B is C_out * H * W from the last Conv2d + pooling.
Why does this happen after changing Conv2d parameters?
Changing kernel_size, stride, or padding in Conv2d changes the output spatial dimensions. If you then Flatten and feed into a Linear layer, the flattened size changes. You must recalculate: in_features = out_channels * H_out * W_out.
About This Tool
This tool is part of HeyTensor, a free suite of PyTorch and deep learning utilities. All calculations run entirely in your browser — no data is sent to any server. The source code is open on GitHub.
Contact
HeyTensor is built and maintained by Michael Lip. For questions or feedback, email [email protected].