What is tf.nn.max_pool's ksize parameter used for?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In TensorFlow max pooling, ksize defines the size of the pooling window. That window is the region over which TensorFlow takes the maximum value at each pooling step. If you understand ksize, strides, and padding together, max pooling becomes much easier to reason about.
What ksize Means
In tf.nn.max_pool, ksize tells TensorFlow how large the pooling window should be along each dimension.
For a 4D tensor in NHWC format, the shape is typically:
- batch
- height
- width
- channels
So a common pooling call looks like this:
Here, ksize=[1, 2, 2, 1] means:
- do not pool across the batch dimension
- pool over height windows of size
2 - pool over width windows of size
2 - do not pool across channels
Why Batch and Channel Entries Are Usually 1
In standard CNN usage, pooling happens independently inside each example and inside each channel map. That is why the batch and channel entries are usually 1.
A normal image pooling window is really about spatial dimensions, so ksize is often read mentally as "pool over a 2-by-2 patch" while the leading and trailing 1s just preserve the untouched dimensions.
ksize Is Not the Same as strides
ksize says how large the window is. strides says how far the window moves between pooling operations.
Example:
This still uses a 2x2 pooling window, but now it slides one step at a time instead of two. That creates overlapping pooling regions.
So the practical distinction is:
- '
ksizecontrols region size' - '
stridescontrols movement step'
padding Changes the Output Shape Too
padding works with ksize and strides to determine the result dimensions.
- '
VALIDmeans no extra padding outside the original tensor' - '
SAMEpads as needed so output sizing follows the "same"-style rule'
That means the same ksize can produce different output shapes depending on padding and stride choices.
Data Format Affects How You Read ksize
The examples above assume NHWC. If the tensor uses NCHW format, the dimension order changes, and the meaning of each ksize position changes with it.
That is why it is important not to memorize only one list shape blindly. Always relate ksize to the data format being used.
Modern Convenience APIs Can Be Clearer
In many codebases, higher-level layers such as tf.keras.layers.MaxPooling2D are easier to read than tf.nn.max_pool directly.
This expresses the same idea more directly for common 2D image models.
Common Pitfalls
- Confusing
ksizewithstrides. - Forgetting that
ksizeincludes entries for dimensions that are usually left untouched. - Using the wrong interpretation of
ksizebecause the tensor data format changed. - Expecting
ksizealone to determine output shape without consideringstridesandpadding. - Reading low-level
tf.nn.max_poolcode when a higher-level pooling layer would be clearer.
Summary
- In TensorFlow max pooling,
ksizedefines the size of the pooling window. - For NHWC tensors, a common value such as
[1, 2, 2, 1]means a2x2spatial window. - Batch and channel entries are usually
1because pooling typically does not span those dimensions. - '
ksize,strides, andpaddingwork together to define the pooling behavior.' - For many models,
tf.keras.layers.MaxPooling2Dis a clearer high-level alternative.

