Last Updated : 02 Apr, 2025
Pooling layer is used in CNNs to reduce the spatial dimensions (width and height) of the input feature maps while retaining the most important information. It involves sliding a two-dimensional filter over each channel of a feature map and summarizing the features within the region covered by the filter.
For a feature map with dimensions n_h \times n_w \times n_c , the dimensions of the output after a pooling layer are:
\left( \frac{n_h - f + 1}{s} \right) \times \left( \frac{n_w - f + 1}{s} \right) \times n_c
where:
A typical CNN model architecture consists of multiple convolution and pooling layers stacked together.
Why are Pooling Layers Important?Max pooling selects the maximum element from the region of the feature map covered by the filter. Thus, the output after max-pooling layer would be a feature map containing the most prominent features of the previous feature map.
Max pooling layer preserves the most important features (edges, textures, etc.) and provides better performance in most cases.
Max Pooling in Keras:
Python
from tensorflow.keras.layers import MaxPooling2D
import numpy as np
# Example input feature map
feature_map = np.array([
[1, 3, 2, 9],
[5, 6, 1, 7],
[4, 2, 8, 6],
[3, 5, 7, 2]
]).reshape(1, 4, 4, 1)
# Applying max pooling
max_pool = MaxPooling2D(pool_size=(2, 2), strides=2)
output = max_pool(feature_map)
print(output.numpy().reshape(2, 2))
Output:
2. Average Pooling[[6 9]
[5 8]]
Average pooling computes the average of the elements present in the region of feature map covered by the filter. Thus, while max pooling gives the most prominent feature in a particular patch of the feature map, average pooling gives the average of features present in a patch.
Average pooling provides a more generalized representation of the input. It is useful in the cases where preserving the overall context is important.
Average Pooling using Keras:
Python
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import AveragePooling2D
feature_map = np.array([
[1, 3, 2, 9],
[5, 6, 1, 7],
[4, 2, 8, 6],
[3, 5, 7, 2]
], dtype=np.float32).reshape(1, 4, 4, 1) # Convert to float32
# Applying average pooling
avg_pool = AveragePooling2D(pool_size=(2, 2), strides=2)
output = avg_pool(feature_map)
print(output.numpy().reshape(2, 2))
Output:
3. Global Pooling[[3.75 4.75]
[3.5 5.75]]
Global pooling reduces each channel in the feature map to a single value, producing a 1 \times 1 \times n_c output. This is equivalent to applying a filter of size n_h × n_w .
There are two types of global pooling:
Global Pooling using Keras:
Python
from tensorflow.keras.layers import GlobalMaxPooling2D, GlobalAveragePooling2D
feature_map = np.array([
[1, 3, 2, 9],
[5, 6, 1, 7],
[4, 2, 8, 6],
[3, 5, 7, 2]
], dtype=np.float32).reshape(1, 4, 4, 1)
# Applying global max pooling
gm_pool = GlobalMaxPooling2D()
gm_output = gm_pool(feature_map)
# Applying global average pooling
ga_pool = GlobalAveragePooling2D()
ga_output = ga_pool(feature_map)
print("Global Max Pooling Output:", gm_output.numpy())
print("Global Average Pooling Output:", ga_output.numpy())
Output:
How Pooling Layers Work?Global Max Pooling Output: [[9]]
Global Average Pooling Output: [[4.4375]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4