What is a convolutional neural network CNN

· Category: AI & Machine Learning

Short answer

A convolutional neural network is a deep learning architecture designed to automatically detect spatial hierarchies of features in images using convolution and pooling operations.

How it works

Convolution layers apply learnable filters that slide across the input image to detect local patterns such as edges, textures, and shapes. Pooling layers downsample feature maps to reduce dimensionality and provide translation invariance. After several convolutional and pooling blocks, fully connected layers map the learned features to class probabilities or regression outputs. Batch normalization and dropout are often inserted to stabilize and regularize training.

Example

In a CNN trained to classify cats and dogs, early layers detect simple edges and corners, middle layers assemble these into eyes and ears, and final layers combine parts into complete animal representations. The network learns this hierarchy without explicit feature engineering.

Why it matters

CNNs revolutionized computer vision by achieving superhuman performance on image classification, object detection, and segmentation tasks. Their weight-sharing design makes them parameter-efficient and scalable to high-resolution inputs, forming the foundation of modern visual AI applications.

Example

import torch
import torch.nn as nn

model = nn.Sequential(
    nn.Linear(784, 256),
    nn.ReLU(),
    nn.Dropout(0.2),
    nn.Linear(256, 10)
)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)

This snippet defines a simple neural network with dropout for regularization, a cross-entropy loss, and the Adam optimizer in PyTorch.