Why convolution instead of Dense
A Dense layer connects every input to every output — fine for a feature vector, ruinous for an image, where a modest 256×256 RGB input is already ~200K numbers. Convolutional layers exploit a simple fact about spatial data: the pattern that detects an edge in the top-left corner is the same pattern that detects an edge in the bottom-right. So a conv layer learns one small kernel and slides it across the whole input, sharing those weights everywhere. Far fewer parameters, and built-in translation invariance for free.
| Layer | Use Case | Key Detail |
|---|---|---|
Conv1D | Temporal/text data | 1D sliding window |
Conv2D | Images | 2D sliding window |
Conv3D | Video/volumetric | 3D sliding window |
SeparableConv2D | Efficient images | Depthwise + pointwise (fewer params) |
DepthwiseConv2D | Per-channel conv | No cross-channel mixing |
The two args that control output shape
Padding: 'valid' means no padding, so the output shrinks by (kernel - 1) each layer; 'same' pads the border so the output keeps the input's spatial size. Strides: the step size of the sliding window — strides=2 halves each spatial dimension and is a cheap alternative to a pooling layer for downsampling. Get these two right and the rest of a CNN's shape math falls out automatically.
SeparableConv2D when params matter
SeparableConv2D is a drop-in efficiency swap: it factors a standard convolution into a depthwise step (one filter per channel) followed by a pointwise 1×1 step (mixing channels). Same call signature, roughly an order of magnitude fewer parameters — which is exactly why mobile-class architectures lean on it.