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.

Is this tool free?

Yes. All HeyTensor tools are free, run in your browser, and require no signup.

Does this work offline?

Once loaded, the tool runs entirely in your browser. No internet needed after the initial page load.

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 calculator inputs or results are 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].

TENSOR PREFLIGHT · DOWNLOADABLE PYTORCH TOOLKIT

Keep the fix. Catch the mismatch next time.

Trace a local forward pass, inspect tensor shapes in an HTML report, and turn a repair into a regression check. Includes eight broken-and-fixed workflows, source code and setup instructions.

Is this toolkit right for my problem?

Use the free calculator for a layer formula or a quick shape check.

Consider Tensor Preflight when you have a local PyTorch model and want a forward-pass report plus repeatable checks. Python 3.9+ and PyTorch 2.8+ are required. It does not run your model in this website or automatically repair it.

Open the sample report before buying to check the output format.

Python 3.9+ · PyTorch 2.8+ · Runs locally

Get Tensor Preflight — $29 One-time payment · ZIP download See a sample report
📊 Based on real data from our PyTorch Error Database — 52 errors analyzed from Stack Overflow
By the same builder: GitHub, theluckystrike BeLikeNative, Grammar AI EarlyThunder, Dev Blog Bug Bounty Reality Zovo, AI Dev Tools