Mastering RNNs, CNNs, and Transformers: The Ultimate Exam Preparation Guide

ANTERA Admin
Mastering RNNs, CNNs, and Transformers: The Ultimate Exam Prep Guide for College Students
Welcome, future AI engineer. If you're a college student staring down an exam on deep learning architectures: Recurrent Neural Networks (RNNs), Convolutional Neural Networks (CNNs), and Transformers, you're in the right place.
Table of Contents
Introduction: Why This Matters for Your Exam and Career
Part 1: Recurrent Neural Networks (RNNs) - The Memory Machines
Part 2: Convolutional Neural Networks (CNNs) - The Spatial Wizards
Part 3: Transformers - The Attention Revolution
Part 4: Head-to-Head Comparison: RNN vs. CNN vs. Transformer
Part 5: Your 7-Day Exam Preparation Roadmap
Part 6: Coding Tips for the Exam (PyTorch Focus)
Conclusion: From Student to Master
Introduction: Why This Matters for Your Exam and Career
Deep learning isn't just a buzzword. It's the engine behind everything from Google Translate to self-driving cars. In your exam, you're not just tested on memorizing facts, you're tested on understanding how these architectures "think." RNNs, CNNs, and Transformers process information in three different ways: step-by-step (RNNs), by looking at nearby patterns (CNNs), and by looking at everything at once (Transformers).
Your exam will likely ask you to:
Explain the idea behind each architecture.
Do simple gradient/math derivations.
Compare architectures for a given task.
Read or fix a short PyTorch code snippet.
This guide is your weapon. Let's go through each one, step by step.
Part 1: Recurrent Neural Networks (RNNs) - The Memory Machines
1.1 Core Theory: Why RNNs?
Normal feedforward networks have no memory. If you give them a sequence of words, they treat each word as separate. RNNs fix this by keeping a hidden state that carries information from one step to the next. Think of it as a loop: the output at time t depends on the input at time t AND the hidden state from time t-1.
Key idea: RNNs are made for sequential data: time series, text, audio, DNA. They learn patterns over time.
Simple formula (Vanilla RNN):
h_t = tanh(W_hh · h_(t-1) + W_xh · x_t + b_h)
y_t = W_hy · h_t + b_y
W = weight matrices, b = biases. tanh keeps values between -1 and 1, so numbers don't blow up, but they can still shrink to zero.
1.2 The Math: Backpropagation Through Time (BPTT)
This is the most exam-important part. BPTT is just normal backpropagation, but applied to the "unrolled" RNN. You lay the network flat across T time steps, then compute gradients like normal.
The Vanishing Gradient Problem: To get the gradient at step 1 from the loss at step T, you multiply many small matrices together (the chain rule). If these numbers are less than 1, the gradient shrinks to almost zero. This is why plain RNNs struggle to remember things from far back.
Exam tip: Be ready to write out the chain rule for one RNN step, and explain why repeated multiplication causes vanishing (or exploding) gradients.
1.3 Variants: LSTM & GRU
LSTM (Long Short-Term Memory): Adds a cell state and three gates, forget, input, output.
Forget gate: what to throw away.
Input gate: what new info to add.
Output gate: what to actually output. The cell state lets gradients flow more easily, so LSTMs remember longer.
GRU (Gated Recurrent Unit): A simpler LSTM with two gates, reset and update.
Reset gate: how much old info to forget.
Update gate: how much new info to add. GRUs use fewer parameters and often work about as well as LSTMs.
Quick comparison:
Feature | Vanilla RNN | LSTM | GRU |
|---|---|---|---|
Gates | None | 3 | 2 |
Cell State | No | Yes | No |
Parameters | Fewest | Most | Medium |
Vanishing Gradient | Bad | Fixed (mostly) | Fixed (mostly) |
Long-range memory | Poor | Great | Very good |
1.4 Exam Tactics & Common Pitfalls
Pitfall 1: Forgetting that RNNs reuse the same weights at every time step. This matters for questions about parameter count.
Pitfall 2: Mixing up BPTT with normal backprop. Remember: you unroll the network first.
Pitfall 3: Thinking LSTMs fix everything. They help, but still struggle on very long sequences (1000+ steps).
Tactic: If asked to compare RNNs vs. Transformers, always mention: RNNs process one step at a time, so they can't be parallelized. This makes them slow to train.
Part 2: Convolutional Neural Networks (CNNs) - The Spatial Wizards
2.1 Core Theory: Why CNNs for Images?
Images have local patterns, edges, corners, textures. A normal fully-connected layer looks at every pixel with no sense of "nearby." CNNs fix this using a filter (kernel), a small grid of numbers that slides over the image and picks out patterns like edges or shapes.
Key idea: CNNs assume nearby pixels are related. This is called the locality assumption. CNNs also reuse the same filter across the whole image (weight sharing), which makes them efficient and helps them recognize a pattern no matter where it appears in the image (translation invariance).
2.2 The Math: Convolution, Pooling, and Backprop
Convolution: A filter of size k×k slides across the image, multiplying and summing values at each position. This produces a feature map highlighting where a pattern (like an edge) appears.
Output size formula:
Output = (N - K + 2P) / S + 1
Where N = input size, K = filter size, P = padding, S = stride.
Pooling: Shrinks the feature map by keeping only the important values.
Max pooling: keeps the biggest value in each region.
Average pooling: keeps the average value. Pooling reduces size and makes the network less sensitive to small shifts in the image.
Backprop in CNNs: Works the same as normal backprop, but gradients also flow back through the shared filter weights. Since one filter is used many times across the image, its gradient is the sum of gradients from every position it touched.
Exam tip: Know how to compute output size after a conv or pooling layer. This is a very common exam question.
2.3 Key Architectures: LeNet, AlexNet, ResNet
LeNet (1998): One of the first CNNs. Simple: a few conv + pooling layers, used for digit recognition.
AlexNet (2012): Made CNNs famous by winning ImageNet. Used ReLU activation, dropout, and GPUs for training. Much deeper than LeNet.
ResNet (2015): Solved a big problem, very deep networks were hard to train because gradients vanished. ResNet added skip connections (shortcuts that let the input skip a few layers and add directly to the output). This lets gradients flow more easily, so networks could go much deeper (100+ layers).
Exam tip: If asked "why do skip connections help," say: they give gradients a direct path backward, which reduces vanishing gradients in deep networks.
2.4 Exam Tactics & Common Pitfalls
Pitfall 1: Forgetting padding when computing output size.
Pitfall 2: Confusing stride (how far the filter moves) with kernel size (how big the filter is).
Pitfall 3: Thinking pooling has learnable weights, it doesn't. It's a fixed operation.
Tactic: If comparing CNNs to RNNs, mention: CNNs process all inputs at once (parallel), unlike RNNs, but CNNs need many layers to see far-apart parts of the image (limited long-range view).
Part 3: Transformers - The Attention Revolution
3.1 Core Theory: The Attention Mechanism
Instead of processing step-by-step (RNN) or only looking at nearby patches (CNN), Transformers let every part of the input look directly at every other part. This is called self-attention.
Key idea: For each word (or token), the model asks: "which other words matter most for understanding me?" It then builds a weighted mix of information from all other words, based on how relevant they are.
3.2 The Math: Scaled Dot-Product Attention & Multi-Head Attention
Every input token creates three vectors: Query (Q), Key (K), and Value (V).
Formula:
Attention(Q, K, V) = softmax(QK^T / √d_k) · V
Steps:
Multiply Q and K to get a score, how relevant each token is to each other token.
Divide by √d_k to keep numbers stable (this is the "scaled" part).
Apply softmax to turn scores into probabilities (they add up to 1).
Multiply by V to get the final weighted output.
Multi-head attention: Instead of doing this once, the model does it several times in parallel (multiple "heads"), each learning to focus on a different kind of relationship (e.g., grammar, meaning). The results are combined at the end.
Exam tip: Know why we scale by √d_k, without it, large dot products push softmax into a region with tiny gradients, making learning harder.
3.3 The Full Transformer: Encoder-Decoder
Encoder: Reads the whole input at once, using self-attention so every token can see every other token.
Decoder: Generates output one token at a time. It uses masked self-attention so it can't "cheat" by seeing future tokens. It also uses cross-attention to look at the encoder's output.
Positional encoding: Since attention has no built-in sense of order, position information is added to each token's embedding so the model knows word order.
Residual connections + layer normalization: Used around each attention and feed-forward block to keep training stable in deep stacks.
Exam tip: Many modern models (like GPT) only use the decoder part, with masked self-attention, and skip the separate encoder.
3.4 Exam Tactics & Common Pitfalls
Pitfall 1: Forgetting the scaling factor √d_k in the attention formula.
Pitfall 2: Confusing self-attention (Q, K, V from the same sequence) with cross-attention (Q from one sequence, K/V from another).
Pitfall 3: Forgetting that Transformers need positional encoding, since attention alone has no sense of order.
Tactic: If asked why Transformers beat RNNs, say: attention lets every token connect to every other token in one step (better long-range memory), and all tokens are processed at once (parallel, faster to train).
Part 4: Head-to-Head Comparison: RNN vs. CNN vs. Transformer
Feature | RNN | CNN | Transformer |
|---|---|---|---|
Best for | Sequences (text, time series) | Images, grid data | Sequences, images, almost everything |
Processes input | One step at a time | Small local patches | All at once |
Parallel training | No | Yes | Yes |
Long-range memory | Weak | Weak (needs many layers) | Strong |
Needs position info | Built-in (order is natural) | Built-in (grid layout) | Must be added manually |
Compute cost | Grows with sequence length (linear) | Grows with image size | Grows with sequence length squared |
Main weakness | Vanishing gradients, slow training | Limited local view | Expensive for very long sequences |
Exam tip: If a question asks "which architecture would you pick for X," match the data type: grid-like data → CNN, ordered data with strong long-range links → Transformer, simple/short sequences with low compute budget → RNN or LSTM.
Part 5: Your 7-Day Exam Preparation Roadmap
Day 1: RNN theory + hidden state math. Practice writing the RNN formula from memory.
Day 2: BPTT and vanishing gradients. Practice deriving the gradient chain by hand.
Day 3: LSTM and GRU gates. Draw the diagrams from memory and explain each gate's job.
Day 4: CNN theory, convolution, and output-size formula. Do 5 practice problems computing output sizes.
Day 5: CNN architectures (LeNet, AlexNet, ResNet) and why skip connections help.
Day 6: Transformer attention math. Practice writing the attention formula and explaining each step.
Day 7: Full review. Do the comparison table from memory. Practice past exam questions and time yourself.
Tip: Spend the last 2 hours before the exam only reviewing formulas and the comparison table, not learning new material.
Part 6: Coding Tips for the Exam (PyTorch Focus)
If your exam includes code, focus on these common patterns:
RNN layer:
rnn = nn.RNN(input_size, hidden_size, batch_first=True)
output, hidden = rnn(x)
LSTM layer:
lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
output, (hidden, cell) = lstm(x)
CNN layer:
conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
pool = nn.MaxPool2d(kernel_size=2, stride=2)
Multi-head attention (built-in):
attn = nn.MultiheadAttention(embed_dim, num_heads, batch_first=True)
output, weights = attn(query, key, value)
Common exam code tasks:
Fix a shape mismatch error (check
batch_first, input dimensions).Compute output size after a conv/pool layer by hand.
Explain what a given line of code does (e.g., "what does
hiddencontain after an LSTM call?").Spot a missing component (e.g., no positional encoding before a Transformer layer).
Tip: Know the shapes of tensors going in and out of each layer. This is where most code-based exam questions focus.
Conclusion: From Student to Master
RNNs, CNNs, and Transformers each solve the same basic problem, understanding structured data, in different ways. RNNs think in steps, CNNs think in local patches, and Transformers think in full connections. Know the core math for each, know their weaknesses, and know when to use which. Walk into your exam with the comparison table and the key formulas in your head, and you're ready. Good luck.
Shadrackovsky, Founder , Antera Group.
