Pooling layers help reduce the size of images or features in a model. They keep important information while making the model faster and simpler.
Pooling layers (MaxPool, AvgPool) in 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.
tf.keras.layers.MaxPooling2D(pool_size=(2, 2))
tf.keras.layers.AveragePooling2D(pool_size=(3, 3), strides=2)
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.
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())
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.
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.