0
0
TensorFlowml~5 mins

Pooling layers (MaxPool, AvgPool) in TensorFlow

Choose your learning style9 modes available
Introduction

Pooling layers help reduce the size of images or features in a model. They keep important information while making the model faster and simpler.

When you want to shrink image size but keep key details.
To reduce the number of calculations in a neural network.
When you want to make your model less sensitive to small changes in images.
To help the model focus on the most important parts of an image.
When building convolutional neural networks for image tasks.
Syntax
TensorFlow
tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid')
tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid')

pool_size sets the size of the window that moves over the image.

strides controls how far the window moves each step. If None, it equals pool_size.

Examples
This creates a max pooling layer that looks at 2x2 blocks and picks the biggest number in each block.
TensorFlow
tf.keras.layers.MaxPooling2D(pool_size=(2, 2))
This creates an average pooling layer that looks at 3x3 blocks, moves 2 steps each time, and averages the numbers in each block.
TensorFlow
tf.keras.layers.AveragePooling2D(pool_size=(3, 3), strides=2)
Sample Model

This code creates a simple 4x4 image with one channel. It applies max pooling and average pooling with a 2x2 window and stride 2. The output shows how the image shrinks and what values are kept.

TensorFlow
import tensorflow as tf
import numpy as np

# Create a sample 4x4 image with 1 channel
input_data = np.array([[[[1], [2], [3], [4]],
                        [[5], [6], [7], [8]],
                        [[9], [10], [11], [12]],
                        [[13], [14], [15], [16]]]], dtype=np.float32)

# Define MaxPooling layer
max_pool = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2)
# Define AveragePooling layer
avg_pool = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=2)

# Apply pooling
max_pooled_output = max_pool(input_data)
avg_pooled_output = avg_pool(input_data)

print("MaxPooling output:\n", max_pooled_output.numpy())
print("AveragePooling output:\n", avg_pooled_output.numpy())
OutputSuccess
Important Notes

MaxPooling picks the largest value in each window, which helps keep strong features.

AveragePooling calculates the average, which smooths the features.

Pooling reduces data size, which speeds up training and helps avoid overfitting.

Summary

Pooling layers shrink images or features while keeping important info.

MaxPooling keeps the strongest signals; AveragePooling smooths values.

They help make models faster and more focused on key details.