C.W.K.
Stream
Lesson 02 of 07 · published

Convolutional Layers

~10 min · layers

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

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.

LayerUse CaseKey Detail
Conv1DTemporal/text data1D sliding window
Conv2DImages2D sliding window
Conv3DVideo/volumetric3D sliding window
SeparableConv2DEfficient imagesDepthwise + pointwise (fewer params)
DepthwiseConv2DPer-channel convNo 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.

Code

Conv2D vs SeparableConv2D — same API, fewer params·python
# Standard Conv2D: 32 filters of 3×3
layers.Conv2D(32, kernel_size=3, activation="relu", padding="same")

# SeparableConv2D: same API, ~8x fewer params
layers.SeparableConv2D(32, kernel_size=3, activation="relu", padding="same")

External links

Exercise

Build a 4-layer CNN: Conv2D(32, 3, padding='same') → MaxPooling2D → Conv2D(64, 3, padding='same') → MaxPooling2D → Flatten → Dense(10). Call model.summary() and confirm the spatial dim halves at each MaxPooling2D while channels grow. Then swap both Conv2D for SeparableConv2D and compare the total param count.
Hint
Watch the 'Param #' column: the SeparableConv2D version should be dramatically lighter for the same output shapes.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.