PyTorch CNN Intro
A Convolutional Neural Network (CNN) is a type of neural network designed specifically to process grid-like data — most commonly images. While a standard fully connected network treats each pixel as an independent input, a CNN understands that neighboring pixels are related and uses that structure to recognize patterns like edges, textures, and shapes.
Why Not Use a Fully Connected Network for Images
A 224×224 color image contains 224 × 224 × 3 = 150,528 pixel values. A fully connected first layer with 1,000 hidden units would need 150,528 × 1,000 = 150 million parameters — just for the first layer. CNNs solve this problem by sharing weights across spatial positions.
Fully connected on 224×224 image: Every pixel connects to every neuron 150 million weights in layer 1 alone ← impractical CNN on 224×224 image: A small 3×3 filter slides across the image Only 3×3×3 = 27 weights for the entire first conv layer ← efficient Same filter detects the same edge pattern everywhere in the image
The Core Idea: Filters Slide Across the Image
A CNN learns small filters (also called kernels). Each filter slides across the image and computes a dot product at every position. This produces a feature map — a grid showing where in the image the filter's pattern was detected.
Image (5×5): Filter (3×3):
┌─────────────┐ ┌─────────┐
│1 0 1 0 1│ │ 1 0 -1 │
│0 1 0 1 0│ × │ 1 0 -1 │
│1 0 1 0 1│ │ 1 0 -1 │
│0 1 0 1 0│ └─────────┘
│1 0 1 0 1│
└─────────────┘
↓
Feature map (3×3): highlights vertical edges
A CNN's Layer Structure
Input Image
│
▼ Convolutional Layer ← learns filters to detect features
│
▼ Activation (ReLU) ← adds non-linearity
│
▼ Pooling Layer ← shrinks spatial size, keeps important features
│
▼ (repeat conv → relu → pool)
│
▼ Flatten ← convert 2D feature maps to 1D vector
│
▼ Fully Connected ← final classification
│
▼ Output (class scores)
A Simple CNN in PyTorch
import torch
import torch.nn as nn
class SimpleCNN(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
# Feature extraction
self.conv_block = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=3, padding=1), # 1 channel in, 16 out
nn.ReLU(),
nn.MaxPool2d(2, 2), # halve spatial size
nn.Conv2d(16, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2)
)
# Classification head
self.fc = nn.Sequential(
nn.Linear(32 * 7 * 7, 128),
nn.ReLU(),
nn.Linear(128, num_classes)
)
def forward(self, x):
x = self.conv_block(x)
x = x.flatten(start_dim=1) # flatten all dims except batch
x = self.fc(x)
return x
model = SimpleCNN(num_classes=10)
sample = torch.rand(8, 1, 28, 28) # batch of 8 grayscale 28×28 images
output = model(sample)
print(output.shape) # torch.Size([8, 10])Spatial Size Through the Network
Input: [8, 1, 28, 28] ← 8 images, 1 channel, 28×28 pixels After Conv2d(1,16,3,padding=1): [8, 16, 28, 28] ← 16 feature maps, same size After MaxPool2d(2): [8, 16, 14, 14] ← halved After Conv2d(16,32,3,padding=1): [8, 32, 14, 14] ← 32 feature maps After MaxPool2d(2): [8, 32, 7, 7] ← halved again After flatten: [8, 32*7*7] = [8, 1568] After Linear(1568 → 128): [8, 128] After Linear(128 → 10): [8, 10] ← 10 class scores per sample
What Each Part Learns
Early layers (first conv): → Simple features: edges, lines, color gradients Middle layers: → Combinations: corners, textures, circles Late layers (deeper conv): → Complex patterns: eyes, wheels, fur, faces
Three Properties That Make CNNs Powerful
Local Connectivity
Each filter looks at a small neighborhood (e.g. 3×3 pixels) instead of the whole image. This drastically reduces parameters and focuses on spatial structure.
Weight Sharing
The same filter slides across the entire image. An edge detector learned in the top-left corner works equally well in the bottom-right corner. This is why CNNs are translation-invariant.
Hierarchical Feature Learning
Each layer builds on the previous layer's features. The network learns simple features early and complex combinations in later layers automatically — no hand-engineering required.
Summary
CNNs use convolutional layers that slide learned filters across input images, producing feature maps. This approach shares weights spatially, making CNNs far more parameter-efficient than fully connected networks for image data. A typical CNN alternates convolutional layers, ReLU activations, and pooling layers to progressively extract features and reduce spatial dimensions. The output is then flattened and passed to fully connected layers for classification. CNNs form the backbone of nearly all modern image recognition systems.
