Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to create a convolutional base using Python?
A convolutional neural network generally consists of a combination of Convolutional layers, Pooling layers, and Dense layers. TensorFlow with Keras provides an easy way to create these networks using the Sequential API.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
Creating a Convolutional Base
The convolutional base is the feature extraction part of a CNN. It uses Conv2D and MaxPooling2D layers to progressively reduce spatial dimensions while increasing feature depth ?
from tensorflow.keras import models, layers
print("Creating the convolutional base")
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
print("Description of architecture is")
model.summary()
Creating the convolutional base Description of architecture is Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 30, 30, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 13, 13, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 4, 4, 64) 36928 ================================================================= Total params: 56,320 Trainable params: 56,320 Non-trainable params: 0
Architecture Breakdown
The convolutional base follows a common pattern of stacking Conv2D and MaxPooling2D layers:
Input Shape: (32, 32, 3) ? designed for CIFAR-10 images with 32×32 pixels and 3 color channels
Conv2D Layers: Extract features using 3×3 filters with ReLU activation
MaxPooling2D: Reduces spatial dimensions by taking maximum values in 2×2 regions
Feature Maps: Start with 32 filters, then increase to 64 for deeper feature extraction
How It Works
| Layer Type | Purpose | Output Effect |
|---|---|---|
| Conv2D | Feature extraction | Maintains spatial size, increases depth |
| MaxPooling2D | Dimensionality reduction | Reduces spatial size, keeps depth |
As the network gets deeper, spatial dimensions (height, width) shrink from 32×32 to 4×4, while the number of feature channels increases from 3 to 64.
Conclusion
This convolutional base creates a feature extraction pipeline that progressively reduces spatial dimensions while increasing feature depth. The pattern of Conv2D followed by MaxPooling2D is fundamental to CNN architecture design.
