Convolutional Neural Networks Cheat Sheet
A cheat sheet for Convolutional Neural Networks covering PyTorch and Keras implementations, convolution and pooling operations, and transfer learning.
2 PagesIntermediateMar 8, 2026
CNN in PyTorch
A minimal convolution-pool-convolution-pool classifier.
python
import torchimport torch.nn as nnclass SimpleCNN(nn.Module): def __init__(self, num_classes=10): super().__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(2, 2) self.fc = nn.Linear(64 * 8 * 8, num_classes) def forward(self, x): x = self.pool(torch.relu(self.conv1(x))) # 32x32 -> 16x16 x = self.pool(torch.relu(self.conv2(x))) # 16x16 -> 8x8 x = x.flatten(1) return self.fc(x)
CNN in Keras
The same architecture using the Keras Sequential API.
python
from tensorflow.keras import layers, modelsmodel = models.Sequential([ layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Flatten(), layers.Dense(10, activation='softmax')])
Transfer Learning
Fine-tune a pretrained backbone on a new task.
python
import torch.nn as nnimport torchvision.models as modelsbackbone = models.resnet50(weights='IMAGENET1K_V2')for param in backbone.parameters(): param.requires_grad = False # freeze pretrained weightsbackbone.fc = nn.Linear(backbone.fc.in_features, num_classes) # replace the classifier head
Key Concepts
Core theory behind CNNs.
- Convolution- Slides learnable filters (kernels) across the input to detect local patterns like edges and textures
- Feature map- Output of applying one filter across the input; stacked feature maps form a layer's output
- Pooling- Downsamples feature maps (max or average) to shrink spatial size and add translation invariance
- Stride & padding- Stride sets the filter's step size; padding ('same'/'valid') controls the output spatial dimensions
- Receptive field- Region of the input that influences a given output unit; grows with network depth
- Transfer learning- Reuse a pretrained backbone (ResNet, EfficientNet, etc.) and fine-tune it on a new task with less data
Pro Tip
When fine-tuning a pretrained CNN on a small dataset, freeze the early convolutional layers, which learn generic edges and textures, and only unfreeze the later layers plus the classification head — this cuts overfitting risk and speeds up training.
Was this cheat sheet helpful?
Explore Topics
#ConvolutionalNeuralNetworks#ConvolutionalNeuralNetworksCheatSheet#DataScience#Intermediate#CNNInPyTorch#CNNInKeras#TransferLearning#KeyConcepts#MachineLearning#CheatSheet#SkillVeris