You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyTorch中RuntimeError: mat1 dim 1 must match mat2 dim 0错误求助——ResNet9模型验证阶段维度不匹配问题

Fixing "RuntimeError: mat1 dim 1 must match mat2 dim 0" in Your ResNet9 Implementation

Hey there, let's break down this error and fix it step by step!

What's Causing the Error?

The RuntimeError: mat1 dim 1 must match mat2 dim 0 happens when the input feature dimension of your linear layer doesn't match what it's expecting. Let's trace through your tensor shapes to see exactly where things go wrong:

Looking at your forward pass output:

torch.Size([32, 512, 50, 50])  # Shape right before entering self.classifier

When this tensor hits self.classifier:

  1. nn.MaxPool2d(4) takes the 50x50 spatial dimension and downsamples it to floor(50/4) = 12 (since default ceil_mode=False), resulting in a shape of (32, 512, 12, 12).
  2. nn.Flatten() then flattens all dimensions after the batch, giving you (32, 512*12*12) = (32, 73728).
  3. Your linear layer nn.Linear(512, num_classes) expects an input feature dimension of 512, but it's getting 73728 instead—hence the dimension mismatch error.

Two Simple Fixes

1. Use Adaptive Pooling (Recommended)

Adaptive pooling layers automatically adjust their output to a fixed spatial size, regardless of input dimensions. This makes your model flexible if you ever change input image sizes:

self.classifier = nn.Sequential(
    nn.AdaptiveAvgPool2d((1, 1)),  # Forces output to (512, 1, 1) no matter input size
    nn.Flatten(),
    nn.Dropout(0.2),
    nn.Linear(512, num_classes)
)

After adaptive pooling, flattening gives you exactly 512 features per sample, which matches your linear layer's input requirement.

2. Manually Adjust Max Pool Kernel Size

If you want to stick with MaxPool2d, calculate a kernel size that reduces your 50x50 spatial dimension to 1x1:

self.classifier = nn.Sequential(
    nn.MaxPool2d(50),  # 50x50 input → 1x1 output
    nn.Flatten(),
    nn.Dropout(0.2),
    nn.Linear(512, num_classes)
)

Note: This only works for 400x400 input images (since your conv layers downsample to 50x50). If you change your input size later, you'll need to adjust the kernel size again.

Verify the Fix

To confirm things work, you can add print statements for each step in the classifier:

def forward(self, xb):
    # ... existing forward pass code ...
    print("Before classifier:", out.shape)
    out = self.classifier[0](out)
    print("After pooling:", out.shape)
    out = self.classifier[1](out)
    print("After flatten:", out.shape)
    out = self.classifier[2](out)
    out = self.classifier[3](out)
    print("Final output:", out.shape)
    return out

You should see the flattened shape become (32, 512) before hitting the linear layer, which will resolve the error.

内容的提问来源于stack exchange,提问作者chuky pedro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:49:06