How to Fix "mat1 and mat2 shapes cannot be multiplied" in PyTorch

This error means your Linear layer's in_features doesn't match the input tensor's last dimension. Fix: add print(x.shape) before the layer, then set nn.Linear(actual_last_dim, out_features).

Understanding the Error

The full error looks like:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x2048 and 512x10)

This tells you: your input has last dimension 2048, but your Linear layer expects 512. The fix is to change nn.Linear(512, 10) to nn.Linear(2048, 10).

Common Causes

1. Wrong flatten size after CNN

# BUG: hardcoded wrong flatten size
self.fc = nn.Linear(512, 10)  # wrong!

# FIX: calculate the actual flatten size
# If Conv2d outputs (batch, 64, 7, 7):
# 64 * 7 * 7 = 3136
self.fc = nn.Linear(3136, 10)  # correct!

2. Changed input image size

# Model designed for 224x224 images
# But you're feeding 256x256 images
# This changes the flatten size

# FIX: Use AdaptiveAvgPool2d before flatten
self.pool = nn.AdaptiveAvgPool2d((7, 7))  # always outputs 7x7

3. Forgot to flatten

# BUG: passing 4D tensor to Linear
x = self.conv(x)       # (batch, 64, 7, 7)
x = self.fc(x)         # ERROR!

# FIX: flatten first
x = self.conv(x)       # (batch, 64, 7, 7)
x = x.flatten(1)       # (batch, 3136)
x = self.fc(x)         # (batch, 10) ✓

Quick Debug Method

def forward(self, x):
    x = self.features(x)
    print(f"Before FC: {x.shape}")  # Add this line
    x = x.flatten(1)
    print(f"After flatten: {x.shape}")  # And this
    x = self.fc(x)
    return x

Related Questions

Try the Shape Mismatch Solver