Skip to main content
Course/Module 5/Topic 2 of 4Beginner

Convolutional Neural Networks (CNN)

Learn Convolutional Neural Networks for processing image data.

60 minBy Priygop TeamLast updated: Feb 2026

What are CNNs?

Convolutional Neural Networks (CNNs) are specialized neural networks designed for processing grid-like data, such as images. They use convolutional layers to automatically learn spatial hierarchies of features.

Key Components

  • Convolutional Layers: Extract features using filters
  • Pooling Layers: Reduce spatial dimensions
  • Fully Connected Layers: Final classification
  • Activation Functions: ReLU, Sigmoid, etc.

Implementation

Example
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

# Generate sample image data
np.random.seed(42)
X = np.random.randn(1000, 28, 28, 1)  # 1000 images, 28x28 pixels, 1 channel
y = np.random.randint(0, 10, 1000)  # 10 classes

# Convert to one-hot encoding
y = tf.keras.utils.to_categorical(y, 10)

# Split the data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create CNN model
model = keras.Sequential([
    keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Conv2D(64, (3, 3), activation='relu'),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Conv2D(64, (3, 3), activation='relu'),
    keras.layers.Flatten(),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
history = model.fit(X_train, y_train,
                    epochs=10,
                    batch_size=32,
                    validation_split=0.2,
                    verbose=1)

# Evaluate the model
test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test accuracy: {test_accuracy:.4f}")

Try It Yourself — Convolutional Neural Networks (CNN)

Try It Yourself — Convolutional Neural Networks (CNN)JavaScript
JavaScript Editor
✓ ValidTab = 2 spaces
JavaScript|33 lines|986 chars|✓ Valid syntax
UTF-8

Additional Resources

Recommended Reading

  • Deep Learning by Ian Goodfellow
  • Neural Networks and Deep Learning by Michael Nielsen
  • TensorFlow and Keras Documentation

Online Resources

  • TensorFlow Tutorials
  • Keras Documentation
  • CNN Architecture Guide
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep