How to Fix "Expected 4D Input, Got 3D" in PyTorch

In PyTorch, add x = x.unsqueeze(0) to give Conv2d the batch dimension it needs, turning your (C, H, W) tensor into (1, C, H, W).

Conv2d expects (batch, channels, H, W) — 4 dimensions. Your input is missing the batch dimension. Fix: add x = x.unsqueeze(0) or check your DataLoader.

The Error

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 3, 3, 3],
but got 3-dimensional input of size [3, 224, 224] instead

Your tensor has shape (3, 224, 224) but Conv2d needs (batch, 3, 224, 224).

Fix 1, Add Batch Dimension

# Single image inference
image = torch.randn(3, 224, 224)    # (C, H, W) — 3D
image = image.unsqueeze(0)       # (1, C, H, W) — 4D ✓
output = model(image)

Fix 2, Check Your DataLoader

# Make sure batch_size is set
loader = DataLoader(dataset, batch_size=32) # returns 4D tensors

# NOT this:
loader = DataLoader(dataset, batch_size=1)
for x, y in loader:
  x = x.squeeze(0) # DON'T squeeze the batch dim!
  output = model(x) # ERROR

Fix 3, Check Your Transform

from torchvision import transforms

# Make sure you convert PIL to tensor
transform = transforms.Compose([
  transforms.Resize((224, 224)),
  transforms.ToTensor(),     # PIL -> (C, H, W) tensor
])

# Then add batch dim for single inference
image = transform(pil_image)    # (3, 224, 224)
image = image.unsqueeze(0)     # (1, 3, 224, 224) ✓

Also Applies To

Try the Shape Mismatch Solver
By the same builder: GitHub, theluckystrike BeLikeNative, Grammar AI EarlyThunder, Dev Blog Bug Bounty Reality Zovo, AI Dev Tools