Machine Learning

CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels

Convolutional layers scan shared local filters over grid data; padding, stride, channels, filters, and pooling decide the output tensor shape.

status: reviewimportance: criticaldifficulty 3/5math: graduateread: 18mlive demo

Concept Structure

CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

2prerequisites
2next concepts
3related links

Learner Contract

What this page should let you do.

You are here becauseConvolutional layers scan shared local filters over grid data; padding, stride, channels, filters, and pooling decide the output tensor shape.

This Machine Learning concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.

By the end4/4 sections ready | runnable code expected | live demo

Explain the mechanism, trace the main notation, and test one prediction in the live demo.

Do this firstIntuition

Read the intuition before the notation; the math should name a mechanism you already felt.

Test the linkManipulate one control and predict the visible change.Then continue to CNN Architecture Families (review)

Claim/source review status

Substantive review recorded

1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.
Claims1/1 reviewed
Sources6 cited
Codeattached
Demolive
Reviewed2026-07-01
Updatedpage 2026-07-01

Learning item flow

4/4 sections readyAsk about thisResearch room
ConceptCNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and ChannelsMachine Learning
6 sources attachedLocal snapshot ready
concept:machine-learning/cnn-convolution-padding-stride-channels
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

You are here because CNNs are often taught as pictures with arrows, but the learner still leaves unsure where the output shape came from. The useful mental model is simpler: a small filter looks at one local patch, writes one output value, then the same filter moves to the next allowed patch.

Before this, know linear transformations and matrices. By the end, you should be able to predict the output tensor shape of a convolutional layer, explain why channels are mixed by each filter, and tell convolution apart from pooling.

Convolution uses locality. Instead of connecting every input pixel to every output unit, a filter sees a small receptive field such as 3×33\times 3. The same filter weights are reused at many spatial positions. That is weight sharing: one learned detector can look for the same local pattern in different places.

Padding changes the border canvas. If you add zeros around the image, the kernel can be centered closer to the edge and the output can stay larger. Stride changes how far the kernel jumps. A stride of 22 skips every other starting position, so the output gets smaller.

Channels are the common hidden confusion. An RGB image is not three independent convolutions unless you choose to view the calculation that way. A single output filter has weights for all input channels; it multiplies a K×K×CinK\times K\times C_{\mathrm{in}} patch and sums across space and channels to produce one output channel. Multiple filters produce multiple output channels.

Pooling is separate. A pooling window aggregates nearby activations, often to downsample a feature map. It has a shape formula like a sliding window, but it usually has no learned weights.

One caveat that keeps implementation intuition honest: many deep-learning libraries compute cross-correlation while calling the layer convolution. The kernel is not flipped in the usual forward operation. For learning CNN mechanics, the important object is the shared local filter scan.

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Let the input tensor have shape

XRH×W×Cin.X\in\mathbb R^{H\times W\times C_{\mathrm{in}}}.

Let a convolutional layer use square kernels of size KK, symmetric padding PP, stride SS, and CoutC_{\mathrm{out}} filters. Each filter has weights

W(c)RK×K×Cin,W^{(c)}\in\mathbb R^{K\times K\times C_{\mathrm{in}}},

plus one optional bias b(c)b^{(c)}, where c{1,,Cout}c\in\{1,\dots,C_{\mathrm{out}}\} indexes the output channel.

For an output location (u,v)(u,v) and output channel cc, the cross-correlation-style forward value is

Yu,v,c=b(c)+a=0K1b=0K1r=1CinWa,b,r(c)X~uS+a,  vS+b,  r,Y_{u,v,c} = b^{(c)} + \sum_{a=0}^{K-1}\sum_{b=0}^{K-1}\sum_{r=1}^{C_{\mathrm{in}}} W^{(c)}_{a,b,r}\, \tilde X_{uS+a,\;vS+b,\;r},

where X~\tilde X is the padded input. This one equation says the whole mechanism: local patch, shared weights, sum over input channels, one output channel per filter.

The spatial output size is

Hout=H+2PKS+1,H_{\mathrm{out}}=\left\lfloor\frac{H+2P-K}{S}\right\rfloor+1,

and

Wout=W+2PKS+1,W_{\mathrm{out}}=\left\lfloor\frac{W+2P-K}{S}\right\rfloor+1,

provided the kernel can fit at least once. If H+2P<KH+2P<K or W+2P<KW+2P<K, there is no valid spatial position. The floor means some border pixels may be left unused when the numerator is not divisible by the stride.

The learned parameter count, with one bias per output channel, is

(K2Cin+1)Cout.(K^2 C_{\mathrm{in}}+1)C_{\mathrm{out}}.

Notice what does not appear in that parameter count: HH and WW. The same filter is reused across positions, so larger images create more output values, not more filter weights.

For a pooling layer with window KpK_p and stride SpS_p applied after convolution, the same spatial-size pattern appears:

Hpool=HoutKpSp+1.H_{\mathrm{pool}}=\left\lfloor\frac{H_{\mathrm{out}}-K_p}{S_p}\right\rfloor+1.

Pooling usually keeps the channel count fixed and adds no learned parameters.

03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt
import numpy as np

def conv2d_shape(H, W, K, P, S, Cin, Cout, bias=True):
    if H + 2 * P < K or W + 2 * P < K:
        return None
    Hout = (H + 2 * P - K) // S + 1
    Wout = (W + 2 * P - K) // S + 1
    params = (K * K * Cin + int(bias)) * Cout
    leftover_h = (H + 2 * P - K) % S
    leftover_w = (W + 2 * P - K) % S
    return Hout, Wout, Cout, params, leftover_h, leftover_w

X = np.arange(5 * 5 * 3).reshape(5, 5, 3)
kernel = np.ones((3, 3, 3))
patch = np.pad(X, ((1, 1), (1, 1), (0, 0)))[0:3, 0:3, :]
one_cell = np.sum(patch * kernel)

print("output shape/params:", conv2d_shape(5, 5, 3, 1, 1, 3, 4))
print("one output cell:", one_cell)

The first function is the shape witness. The second part shows the mechanism: after padding, one 3×3×33\times 3\times 3 patch is multiplied by one filter and summed into one scalar output cell.

04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

Choose a setup and predict both the spatial behavior and the parameter-count scale before reveal:

  • Same spatial size: padding and stride preserve H×WH\times W.
  • Downsampled: the output is smaller, usually because stride or pooling jumps over positions.
  • Leftover border: the floor in the formula drops a partial final step.
  • Invalid: the kernel cannot fit at least once.

Before reveal, the exact output shape, parameter count, sliding positions, and one-cell calculation stay locked. After reveal, inspect the highlighted receptive field, the top-left output cell, the output tensor shape, and the caveat strip. The demo is not a full CNN architecture. It is the tensor-shape and locality grammar that later CNN architectures rely on.

Live Concept Demo

Explore CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 3/5graduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

Convolutional layers scan shared local filters over grid data; padding, stride, channels, filters, and pooling decide the output tensor shape.

Prediction open01 / Intuition
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels should make visible.

Visual Inquiry

Make the image answer a mathematical question

Convolutional layers scan shared local filters over grid data; padding, stride, channels, filters, and pooling decide the output tensor shape.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

course-notes · 2026CS231n: Convolutional Neural NetworksStanford CS231n

Primary course-note source for local receptive fields, depth/filters, stride, zero padding, output volume sizing, and parameter sharing.

Open source
book · 2026Dive into Deep Learning: Padding and StrideZhang, Lipton, Li, and Smola

Source for padding and stride mechanics in 2D convolutional layers.

Open source
book · 2026Dive into Deep Learning: Multiple Input and Multiple Output ChannelsZhang, Lipton, Li, and Smola

Source for multi-channel convolution: filters span all input channels and output channels correspond to learned filters.

Open source
book · 2026Dive into Deep Learning: PoolingZhang, Lipton, Li, and Smola

Source for max/average pooling as a separate local aggregation/downsampling operation.

Open source
book · 2016Deep Learning, Chapter 9: Convolutional NetworksGoodfellow, Bengio, and Courville

Frames convolutional networks through sparse interactions, parameter sharing, equivariance, and grid-structured data.

Open source
course-notes · 2026CS230: Convolutional Neural Networks CheatsheetStanford CS230

Compact reference for convolution/pooling output dimensions and parameter counts.

Open source

Claim Review

Convolutional layers scan shared local filters over grid data; padding, stride, channels, filters, and pooling decide the output tensor shape.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources6 references

cs231n-convnets, d2l-padding-stride, d2l-channels, d2l-pooling, goodfellow-2016-convnets, cs230-cnn-cheatsheet

Local checks4 local checks

Use equations, runnable code, and demos to check whether the source support is operational.

Substantively reviewedA CNN convolutional layer scans a shared local kernel over a grid, uses padding and stride to determine spatial output positions, and uses a filter bank that mixes all input channels to produce output channels.Claim metadata: source checked

CS231n and Deep Learning Book support local receptive fields, full-depth shared filters, and parameter sharing; D2L supports padding/stride formulas, channel summation, output-channel filters, and pooling; CS230 supports compact shape and parameter-count formulas.

Sources: CS231n: Convolutional Neural Networks, Dive into Deep Learning: Padding and Stride, Dive into Deep Learning: Multiple Input and Multiple Output Channels, Dive into Deep Learning: Pooling, Deep Learning, Chapter 9: Convolutional Networks, CS230: Convolutional Neural Networks CheatsheetToy shape witness only; not full CNN architecture history, performance, optimized kernels, dilation/groups/depthwise variants, exact framework conventions, complete equivariance guarantees, or GPT Pro publication approval.A bounded review summary is present; still check caveats and exact reference scope.

Checked CS231n for local receptive fields, full-depth filters, padding/stride sizing, parameter sharing, and parameter counts; D2L for padding/stride formulas, multi-channel summation, output-channel filters, and pooling; Deep Learning Book for sparse interactions, parameter sharing, grid data, and cross-correlation framing; and CS230 for compact output-size/parameter formulas. GPT Pro publish critique remains pending.

Reviewer: codex-local-primary-source-audit; reviewed 2026-07-01

Practice Loop

Try the idea before it explains itself

Convolutional layers scan shared local filters over grid data; padding, stride, channels, filters, and pooling decide the output tensor shape.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Grounded research drawerClose
ConceptCNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and ChannelsMachine Learning

Research Room

Attach the question to a claim, equation, code, or demo

Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptMachine Learning

CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels

Attached question

What is the smallest example that makes CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays in this browser, attached to the selected learning item.

No local draft saved.
Evidence to inspect
  • References to inspect: attached references on this page.
  • Definition, prerequisite, and contrast concept links
  • The equation or runnable code that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels Selected item key: recorded for copy. Context: Machine Learning Page anchor: recorded for copy. Open question: What is the smallest example that makes CNN Shape Mechanics: Convolution, Padding, Stride, Pooling, and Channels click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

View it in context
concept/concept-notebook/machine-learning/cnn-convolution-padding-stride-channels concept:machine-learning/cnn-convolution-padding-stride-channels