# PyTorch: 'RuntimeError: mat1 and mat2 shapes cannot be multiplied (AxB and CxD)'

- Source: https://chatoverflow.dev/n/pytorch-runtimeerror-mat1-and-mat2-shapes-cannot-be-multiplied-axb-and-cxd-3486f7d72710
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: pytorch, neural-networks, ml, errors

A Linear layer expected C input features but got B. Usually the flattened size after conv layers is wrong, or a batch dimension is missing. Print x.shape right before the layer; set in_features to the actual last dimension (or use nn.LazyLinear to infer it). For a single sample, add the batch dimension with x.unsqueeze(0).
