Deep Learning: CNNs, RNNs, and Transformer Architectures
Deep learning extends neural networks with many layers, enabling models to learn hierarchical representations directly from raw data. Unlike traditional machine learning, which requires manual feature engineering, deep learning architectures automatically discover the features needed for detection, classification, or generation. This guide covers the three major deep learning architectures — convolutional neural networks (CNNs), recurrent neural networks (RNNs), and Transformers — with practical guidance on when to use each.
Yann LeCun, Geoffrey Hinton, and Yoshua Bengio — often called the godfathers of deep learning — laid the groundwork for these architectures over decades of research. Their 2015 Nature survey (“Deep Learning”) codified the principles that now power everything from self-driving cars to language models. Understanding how CNNs, RNNs, and Transformers work gives you the tools to solve a vast range of problems across computer vision, natural language processing, time series forecasting, and multi-modal AI.
Convolutional Neural Networks
CNNs are purpose-built for grid-like data, most notably images. They exploit the spatial structure of data by using convolutional filters that slide across the input, detecting local patterns such as edges, textures, and shapes at progressively higher levels of abstraction. LeCun’s work on LeNet-5 in the 1990s demonstrated that backpropagation could train convolutional networks for handwritten digit recognition — a breakthrough that later evolved into architectures like AlexNet (Krizhevsky et al., 2012), which won the ImageNet competition by a wide margin.
Convolution Layers
A convolution layer applies learnable filters across the input tensor. Each filter detects a specific spatial pattern — horizontal edges in early layers, wheels or eyes in deeper layers. The operation is mathematically a cross-correlation:
# Conceptual operation: sliding dot product
# Input shape: (batch, height, width, channels)
# Filter shape: (filter_height, filter_width, in_channels, out_channels)Key hyperparameters include filter size (typically 3×3 or 5×5), stride (step size), and padding (adding zeros at borders to preserve spatial dimensions). Stacking multiple convolution layers allows the network to compose low-level features into high-level concepts. The depth of the network often correlates with performance, as demonstrated by the VGG and ResNet families.
Pooling Layers
Pooling layers down-sample feature maps, reducing spatial dimensions and computational cost. Max pooling selects the maximum value within each window, providing translational invariance — the exact position of a feature matters less than its presence. Average pooling computes the mean, offering a smoother aggregation. Research by Goodfellow et al. (Deep Learning, MIT Press, 2016) notes that pooling also helps the model generalize by focusing on the strongest activations.
Typical CNN Architecture
A modern CNN follows a pattern of repeated convolution-pooling blocks followed by fully connected layers:
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
])Batch normalization after each convolution layer stabilizes training and accelerates convergence (Ioffe & Szegedy, 2015).
Transfer Learning with CNNs
Training a CNN from scratch requires substantial data and compute. Transfer learning offers a practical alternative: take a pre-trained model such as VGG16, ResNet-50, or EfficientNet, remove the classification head, and fine-tune the remaining layers on your dataset. The early layers encode generic features (edges, colors, textures) that transfer across domains. Only the later task-specific layers need retraining. This approach works well with as few as a few hundred images per class and is the standard practice in industry.
base = tf.keras.applications.EfficientNetB0(weights='imagenet', include_top=False)
base.trainable = False # Freeze base layers
model = tf.keras.Sequential([
base,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(num_classes, activation='softmax')
])Recurrent Neural Networks
RNNs process sequential data by maintaining a hidden state that captures information from previous time steps. This makes them suitable for time series, audio, text, and any data where order matters. The core idea — a neural network with a loop that allows information to persist — was formalized by Rumelhart, Hinton, and Williams in 1986, but practical training remained challenging due to the vanishing gradient problem.
Vanilla RNNs
A simple RNN applies the same weight matrix at each time step, taking both the current input and the previous hidden state. The mathematical formulation is:
h_t = tanh(W_h * h_{t-1} + W_x * x_t + b)Vanilla RNNs struggle with long-range dependencies because gradients either explode or vanish during backpropagation through time (BPTT). Bengio et al. (1994) showed that the error signal decays exponentially with sequence length, making it nearly impossible for simple RNNs to learn relationships spanning more than about ten time steps.
LSTMs and GRUs
Long Short-Term Memory networks (Hochreiter & Schmidhuber, 1997) solve the vanishing gradient problem through gating mechanisms. An LSTM cell maintains a cell state that runs across time steps, with three gates controlling information flow:
- Forget gate: decides what information to discard from the cell state
- Input gate: decides what new information to store
- Output gate: decides what to output based on the cell state
Gated Recurrent Units (Cho et al., 2014) simplify the LSTM by merging the forget and input gates into a single update gate. GRUs have fewer parameters and train faster, often matching LSTM performance on smaller datasets.
model = tf.keras.Sequential([
tf.keras.layers.LSTM(128, input_shape=(sequence_length, n_features), return_sequences=True),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(1, activation='sigmoid')
])Bidirectional RNNs
Bidirectional RNNs process sequences in both forward and backward directions, allowing each position to access context from both past and future time steps. This is particularly useful for tasks like named entity recognition and part-of-speech tagging, where the meaning of a word depends on both preceding and following words.
Transformers
The Transformer architecture, introduced by Vaswani et al. in “Attention Is All You Need” (2017), replaced recurrence entirely with attention mechanisms. Transformers process all positions simultaneously, enabling massive parallelization during training and capturing long-range dependencies more effectively than RNNs.
Attention Mechanism
Attention computes a weighted sum of values, where the weights depend on the compatibility between queries and keys. Scaled dot-product attention is defined as:
Attention(Q, K, V) = softmax(Q × K^T / sqrt(d_k)) × VThe scaling factor sqrt(d_k) prevents the dot products from growing too large, which would push the softmax into regions with extremely small gradients. Multi-head attention runs this operation multiple times in parallel with different learned projections, allowing the model to attend to information from different representation subspaces.
Positional Encoding
Since self-attention is permutation-invariant, the Transformer needs a way to encode token position. The original paper used sinusoidal positional encodings with different frequencies:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))These encodings allow the model to learn relative positions because any offset can be represented as a linear combination of the original encoding.
Encoder-Decoder and Decoder-Only Variants
The original Transformer architecture has an encoder (bidirectional self-attention for understanding input) and a decoder (masked self-attention for generating output). BERT uses only the encoder stack for understanding tasks, while GPT uses only the decoder stack for generative tasks. This family of models has achieved state-of-the-art results across NLP benchmarks, and vision transformers (Dosovitskiy et al., 2021) have shown that pure attention architectures can match or exceed CNNs on image classification.
Pre-Trained Transformers in Practice
The Hugging Face Transformers library provides a unified interface for hundreds of pre-trained models:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-uncased", num_labels=4)
inputs = tokenizer("Deep learning continues to transform industries.", return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logitsFine-tuning these models on domain-specific data typically requires only a few hours on a single GPU.
Choosing an Architecture
- Images and video: start with CNNs or vision transformers. EfficientNet offers a strong accuracy-to-compute ratio. For real-time applications, consider MobileNet or YOLO.
- Sequences with moderate length: LSTMs and GRUs remain effective for time series forecasting and audio processing. They are more memory-efficient than Transformers for long sequences on limited hardware.
- Text and language: use pre-trained Transformers (BERT for understanding, GPT for generation). Fine-tuning on domain data is standard practice.
- Multi-modal data: CLIP (Radford et al., 2021) and similar models bridge vision and language, enabling zero-shot classification and cross-modal retrieval.
FAQ
Why did Transformers replace RNNs for NLP? Transformers process all tokens in parallel, enabling much faster training on modern hardware. They also capture long-range dependencies without the vanishing gradient problem that limits RNNs.
Do I need a GPU to train deep learning models? For small datasets and simple architectures, a CPU suffices. For anything beyond MNIST-level toy problems, a GPU (or TPU) drastically reduces training time from days to hours.
How many layers do I need? Deeper networks can learn more complex patterns but require more data and regularization. Start with 2-4 hidden layers and increase only if validation performance plateaus. ResNet-style skip connections help train very deep networks.
What is the difference between a vision transformer and a CNN? Vision transformers divide an image into patch tokens and apply standard Transformer attention, while CNNs use localized convolution filters. ViTs excel with large datasets; CNNs still perform better with limited data due to their built-in inductive biases.
Can I use deep learning for tabular data? Tree-based methods like XGBoost remain state-of-the-art for most tabular benchmarks. Deep learning excels with unstructured data — images, text, audio, and video — where manual feature engineering is impractical.
Internal Links
- For a refresher on the building blocks, see Neural Networks: Perceptrons, Activation, and Backpropagation.
- To understand how Transformers revolutionized NLP specifically, read NLP with Transformers: BERT, GPT, and Attention.
- If you are deciding between frameworks for implementing these architectures, see PyTorch vs TensorFlow.