DL - Intermediate Notes by Rishi Raj
DL - Intermediate Notes by Rishi Raj
This document covers 8 intermediate Deep Learning topics — each with a definition, full explanation,
mathematical formula, real interview questions, common mistakes, use/do-not-use guidance, visual description,
Python code, quick summary, and algorithm comparison.
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
1 Batch Normalization
📖 DEFINITION
Batch Normalization (BatchNorm) is a technique that normalizes the output of each layer during
training so that the inputs to the next layer always have a consistent mean of 0 and standard
deviation of 1. This stabilizes and dramatically speeds up training.
💡 EXPLANATION
Imagine you are a teacher grading 30 students across wildly different subjects — some score 2/100, others
98/100. If you normalize all scores to the same scale first, comparing and improving becomes much easier.
BatchNorm does the same for neuron outputs.
Without BatchNorm, as data passes through many layers, the distribution of values can shift wildly — called
Internal Covariate Shift. This forces the network to constantly readjust, slowing learning.
BatchNorm fixes this by: (1) computing mean and variance of the current mini-batch, (2) normalizing the output,
(3) then applying learnable scale (gamma) and shift (beta) parameters so the network can undo the normalization
if needed.
Example: Training a deep ResNet without BatchNorm is like running a relay race where each runner uses a
completely different speed scale. With BatchNorm, every runner uses the same scale — the team runs smoothly
and fast.
📐 MATHEMATICAL FORMULA
Normalize:
x_hat = (x - mean_batch) / sqrt(variance_batch + epsilon)
Scale and Shift (learnable):
y = gamma * x_hat + beta
Where:
gamma = learned scale parameter
beta = learned shift parameter
epsilon = small constant (~1e-5) to avoid division by zero
✗ Using BatchNorm with very small batch sizes (batch size < 8) — statistics become unreliable
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
✗ Forgetting that BatchNorm behaves differently during training vs inference (uses running mean/var at test time)
✗ Placing it wrong — generally use Conv → BatchNorm → ReLU order
✗ Using BatchNorm with RNNs — Layer Normalization works better for sequences
Draw a pipeline: [Layer Output] → [Compute Mean & Variance of batch] → [Normalize to mean=0, var=1]
→ [Scale by gamma, Shift by beta] → [Next Layer Input]. Add a note: during training, use batch stats;
during inference, use running averages stored from training.
import tensorflow as tf
from [Link] import layers
layers.GlobalAveragePooling2D(),
[Link](10, activation='softmax')
])
# In PyTorch
import [Link] as nn
layer = [Link](
nn.Conv2d(64, 128, 3, padding=1, bias=False),
nn.BatchNorm2d(128),
[Link]()
)
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Batch Normalization Normalizes across the batch — best for CNNs with large batches
Layer Normalization Normalizes across features — best for RNNs and Transformers
Group Normalization Normalizes within groups of channels — good for small batches
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
2 Recurrent Neural Networks (RNN)
📖 DEFINITION
An RNN is a neural network designed for sequential data — where the order of inputs matters.
Unlike regular networks, RNNs have a memory (hidden state) that carries information from
previous time steps to influence future predictions.
💡 EXPLANATION
Imagine reading a book. When you read the word "bank," you need context — are we talking about a river bank or
a financial bank? Previous words (context) determine the meaning. RNNs work exactly like this.
In a regular neural network, each input is processed independently. In an RNN, each time step produces a hidden
state that is passed to the next time step — creating a chain of memory.
The hidden state acts like a running summary: "here is everything I have seen so far." At each step, the network
reads the new input AND the previous hidden state together to produce the next output and hidden state.
Example: Autocomplete on your phone. When you type "I want to eat," the RNN reads each word sequentially,
building context, and predicts "pizza" based on the full sequence — not just the last word.
📐 MATHEMATICAL FORMULA
✗ Using vanilla RNN for long sequences — it forgets early context (use LSTM or GRU instead)
✗ Not truncating backpropagation through time (BPTT) for very long sequences
✗ Forgetting to reset hidden states between independent sequences
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Draw a chain of boxes labeled h0 → h1 → h2 → h3. Each box receives an input x_t from below and
passes its hidden state to the right. Above each box is the output y_t. An arrow loops back from each
hidden state to the next — this loop is the memory.
import tensorflow as tf
from [Link] import layers
# In PyTorch
import [Link] as nn
rnn = [Link](input_size=64, hidden_size=128,
num_layers=2, batch_first=True)
output, hidden = rnn(x) # x shape: (batch, seq_len, features)
Vanilla RNN Has memory but forgets long sequences (vanishing gradient)
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
3 LSTM & GRU (Long Short-Term Memory)
📖 DEFINITION
LSTM is an advanced RNN that uses special gates (Forget Gate, Input Gate, Output Gate) to
selectively remember or forget information over long sequences. GRU is a simplified version with
just two gates but similar performance.
💡 EXPLANATION
Think of your brain before an important exam. You selectively remember important concepts (KEEP), forget
irrelevant details (FORGET), and use the knowledge at the right moment (OUTPUT). LSTMs work exactly this
way.
Vanilla RNNs have one problem — they forget things from far back in the sequence (vanishing gradient). LSTMs
solve this with a Cell State — like a conveyor belt that carries information across many time steps with minimal
modification.
Forget Gate: Decides what to throw away from cell state ("forget this old info")
Input Gate: Decides what new information to store ("learn this new info")
Output Gate: Decides what to output at this time step ("use this relevant info")
GRU (Gated Recurrent Unit) simplifies this to just two gates (Reset and Update) — nearly same performance but
faster to train.
📐 MATHEMATICAL FORMULA
LSTM Gates:
f_t = sigmoid(W_f * [h_(t-1), x_t] + b_f) Forget gate
i_t = sigmoid(W_i * [h_(t-1), x_t] + b_i) Input gate
o_t = sigmoid(W_o * [h_(t-1), x_t] + b_o) Output gate
Cell State Update:
C_t = f_t * C_(t-1) + i_t * tanh(W_c * [h_(t-1), x_t] + b_c)
Hidden State:
h_t = o_t * tanh(C_t)
Q2 What are the three gates in an LSTM and what does each do?
Q3 What is the difference between the cell state and hidden state in LSTM?
✗ Using LSTM when Transformers would be better for very long sequences
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Draw one LSTM cell. Show three sigmoid gates (Forget, Input, Output) as separate boxes. Show the Cell
State as a horizontal line running through the top (the conveyor belt). Show h_(t-1) and x_t as inputs on
the left. Show h_t as output on the right. Label each gate with its purpose.
import tensorflow as tf
from [Link] import layers
[Link]([Link](64)),
[Link](0.3),
[Link](64, activation='relu'),
[Link](1, activation='sigmoid') # binary sentiment
])
→ LSTMs fix vanishing gradients using three gates: Forget, Input, Output
→ The Cell State is the long-term memory (conveyor belt across time steps)
→ GRU is a simpler LSTM variant with two gates — faster but similar accuracy
→ Bidirectional LSTM reads sequence both forward and backward for richer context
→ LSTM was the go-to for NLP before Transformers took over in 2017
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
GRU 2 gates, no cell state — faster, slightly less capacity than LSTM
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
4 Transfer Learning
📖 DEFINITION
Transfer Learning is the technique of taking a model pre-trained on a large dataset (like ImageNet
with 14 million images) and reusing its learned knowledge for a new, related task — saving
massive amounts of time and data.
💡 EXPLANATION
You already know how to drive a car. Learning to drive a truck is much easier because most skills transfer —
steering, braking, traffic rules. You only need to learn the new parts (bigger vehicle, different gear system).
Transfer Learning does the same with neural networks. A model trained on ImageNet has already learned to
detect edges, textures, shapes, and objects. When you apply it to a new task (say, detecting tumors in X-rays), it
already knows the basics — you just fine-tune it for the specific task.
Two common approaches:
(1) Feature Extraction: Freeze all pre-trained layers. Only train the new output head. Fast but less specialized.
(2) Fine-Tuning: Unfreeze some or all layers and train with a very small learning rate. Slower but more powerful.
Example: Google's MobileNet trained on ImageNet can detect your custom object (e.g., ripe vs. unripe mangoes)
with just 500 photos instead of millions.
📐 MATHEMATICAL FORMULA
Q4 What pre-trained models do you know and when would you use each?
✗ Using too large a learning rate when fine-tuning — destroys pre-trained knowledge
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
✗ Not freezing any layers when data is very small — model will overfit
✗ Using an unrelated pre-trained model (e.g., NLP model for images)
✗ Forgetting to change the output layer to match your number of classes
Draw two networks side by side. LEFT: Big pre-trained model (VGG/ResNet) trained on ImageNet —
many layers, fully trained. RIGHT: Your model — same base layers (FROZEN, shown in grey) + new
small head layers (TRAINABLE, shown in orange). Arrow pointing from left to right = transfer.
import tensorflow as tf
model = [Link]([
base_model,
[Link].GlobalAveragePooling2D(),
[Link](256, activation='relu'),
[Link](0.3),
[Link](5, activation='softmax') # your classes
])
[Link](optimizer=[Link](1e-5),
loss='categorical_crossentropy', metrics=['accuracy'])
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Training from Scratch Needs millions of samples and weeks of GPU compute
Fine-Tuning (Transfer) More accurate, needs slightly more data and time
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
5 Data Augmentation
📖 DEFINITION
Data Augmentation is the technique of artificially creating new training examples from existing
ones by applying transformations like flipping, rotating, cropping, or adding noise — without
collecting new data.
💡 EXPLANATION
Suppose you want to teach a child to recognize a cat. You show them one photo. Now you flip it upside down,
change the brightness, zoom in — the child still recognizes it as a cat. You have created multiple learning
experiences from one photo.
Deep learning models need lots of data to generalize well. But collecting and labeling data is expensive. Data
Augmentation solves this by transforming existing images during training so the model sees many "versions" of
the same sample.
For images: flip, rotate, crop, zoom, brightness/contrast changes, adding Gaussian noise, cutout (randomly
removing patches).
For text: synonym replacement, back-translation, random insertion/deletion.
For time series: time warping, window slicing, adding noise.
Example: MedMNIST — a medical image dataset where augmentation is critical because getting 10,000 real X-
ray scans is impossible, but augmenting 1,000 into 50,000 is easy.
📐 MATHEMATICAL FORMULA
Augmentation probability:
Apply each transform with probability p (typically 0.5)
Mixup Augmentation:
x_mix = lambda * x1 + (1 - lambda) * x2
y_mix = lambda * y1 + (1 - lambda) * y2
lambda ~ Beta(alpha, alpha) (typically alpha = 0.2)
CutMix:
Replace rectangular region of image with patch from another image
Q2 What augmentations would you apply to medical images vs. natural images?
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Show one original cat image in the center. Surround it with 6 versions: flipped horizontally, rotated 30
degrees, zoomed in, brightness increased, random crop, and Gaussian noise added. All arrows point
outward from the original = one sample becomes many training examples.
import tensorflow as tf
from [Link] import layers
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
CutMix Pastes patches from one image into another — very effective
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
6 Hyperparameter Tuning
📖 DEFINITION
Hyperparameters are settings you choose BEFORE training — like learning rate, batch size,
number of layers, dropout rate. Hyperparameter Tuning is the process of finding the optimal
combination of these settings to maximize model performance.
💡 EXPLANATION
Think of baking a cake. The recipe's baking temperature, time, and ingredient ratios are hyperparameters. The
exact amounts of each ingredient the dough "learns" = model parameters. You tune the recipe settings
(hyperparameters) to get the perfect cake.
Model parameters (weights) are learned automatically during training. Hyperparameters must be set manually or
searched automatically.
Key hyperparameters to tune:
Learning rate — the most important hyperparameter. Too high = diverges. Too low = too slow.
Batch size — affects gradient stability and memory.
Number of layers/neurons — model capacity.
Dropout rate — regularization strength.
Optimizer type — Adam vs SGD vs RMSprop.
Search strategies:
Grid Search: Try every combination (exhaustive but slow).
Random Search: Try random combinations (faster, often better than grid).
Bayesian Optimization: Intelligent search based on past results (most efficient).
📐 MATHEMATICAL FORMULA
Q2 How would you tune the learning rate for a new problem?
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Q5 What is a Learning Rate Scheduler and when would you use one?
Draw a 2D grid with Learning Rate on X-axis and Dropout on Y-axis. Grid Search = every cell tested
(shown as dots in every cell). Random Search = dots scattered randomly (covers more of the space
efficiently). Bayesian = dots concentrated near the best regions found so far.
def build_model(hp):
model = [Link]()
[Link]([Link](
units=[Link]('units', min_value=64, max_value=512, step=64),
activation='relu'
))
[Link]([Link](
[Link]('dropout', min_value=0.1, max_value=0.5, step=0.1)
))
[Link]([Link](10, activation='softmax'))
[Link](
optimizer=[Link](
[Link]('learning_rate', [1e-2, 1e-3, 1e-4])
),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
return model
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
→ Hyperparameters are set before training; model parameters are learned during training
→ Learning rate is the most important hyperparameter to tune first
→ Grid Search = exhaustive but slow; Random Search = faster and surprisingly effective
→ Bayesian Optimization = smartest search, uses past results to guide next attempt
→ Always tune on validation set, NEVER on test set
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
7 Attention Mechanism
📖 DEFINITION
Attention is a technique that allows a neural network to focus on the most relevant parts of the
input when making a prediction — instead of treating all parts equally. It is the core idea behind
Transformers and modern NLP models like BERT and GPT.
💡 EXPLANATION
When you read the sentence "The trophy didn't fit in the suitcase because it was too big," what does "it" refer to?
Your brain focuses on (attends to) "trophy" because context makes it relevant. Attention mechanisms teach neural
networks to do the same.
In traditional RNNs, all past information is compressed into a single hidden state — a bottleneck. Attention solves
this by letting the model look at ALL previous positions and decide which ones matter most for the current
prediction.
How it works:
Query (Q): What am I looking for?
Key (K): What does each position offer?
Value (V): What information does each position contain?
Attention Score = how relevant each position is = softmax(Q * K^T / sqrt(d_k))
Final output = weighted sum of Values based on attention scores.
Example: In translation, when predicting the French word for "ate," the model attends strongly to "ate" in the
English source sentence, not "quickly" or "the."
📐 MATHEMATICAL FORMULA
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
✗ Confusing self-attention (query from same sequence) with cross-attention (query from different sequence)
✗ Forgetting the scaling factor 1/sqrt(d_k) — causes vanishing gradients with large dot products
✗ Thinking attention is only used in NLP — it is used in vision (ViT), audio, and more
✗ Not using masking in decoder attention to prevent looking at future tokens
Draw a sentence: "The cat sat on the mat." Show attention scores as a heatmap grid (sentence x
sentence). When predicting the meaning of "sat," the model highlights "cat" (subject) and "mat" (location)
with darker colors — showing which words it attends to most.
import tensorflow as tf
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
→ Attention lets the model focus on the most relevant parts of the input
→ Uses three matrices: Query (what I want), Key (what I offer), Value (what I contain)
→ Attention score = softmax(Q * K^T / sqrt(d_k)) — tells how much to focus on each position
→ Multi-Head Attention runs multiple attention operations in parallel for richer representation
→ Self-attention is the core of Transformers, BERT, GPT — the backbone of modern NLP
RNN (no attention) Fixed-size hidden state bottleneck — forgets distant context
RNN + Attention Looks at all encoder hidden states — solved NMT before Transformers
Self-Attention (Transformer) Every position attends to every other — parallelizable and powerful
Multi-Head Attention Runs attention in parallel across different subspaces — richer features
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
INTERMEDIATE
8 Transformer Architecture (Basics)
📖 DEFINITION
The Transformer is a neural network architecture built entirely on Attention (no RNNs, no
convolutions). It processes the entire sequence in parallel and has become the foundation of all
modern AI models: BERT, GPT, T5, ChatGPT, and more.
💡 EXPLANATION
In 2017, the paper "Attention Is All You Need" introduced the Transformer. Before this, RNNs dominated NLP.
Transformers made everything faster and better.
Key idea: Instead of processing one token at a time (like RNN), process the ENTIRE sequence at once using self-
attention. This enables massive parallelization — train on thousands of GPUs simultaneously.
Encoder: Reads the full input and creates rich contextual representations. Each token "looks at" all other tokens
via self-attention.
Decoder: Generates the output one token at a time, attending to encoder output and its own previous outputs.
Positional Encoding: Since there is no recurrence, we must inject position information into the input embeddings.
Example: When you type a prompt into ChatGPT, your entire prompt is processed in parallel by the Transformer
encoder/decoder — not word by word like older systems. This is why it responds so fast.
📐 MATHEMATICAL FORMULA
✗ Thinking Transformers replaced all RNNs — RNNs still have edge cases where they are better
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
✗ Forgetting that Transformers have O(n^2) memory complexity with sequence length
✗ Confusing BERT (encoder-only) with GPT (decoder-only) with T5 (encoder-decoder)
✗ Not using positional encoding — the model has no way to know word order without it
Draw the Transformer encoder stack: Input → Embedding + Positional Encoding → [Multi-Head Self-
Attention → Add & Norm → Feed Forward → Add & Norm] x N layers → Output Representation. Draw
decoder similarly but with an extra cross-attention layer attending to encoder output. Show that all
positions process in parallel (arrows go both ways).
import tensorflow as tf
from [Link] import layers
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer
Deep Learning Notes — Part 2: Intermediate For Data Science Placements
Transformer Encoder (BERT) Bidirectional, understands context both ways, great for classification/QA
Encoder-Decoder (T5, BART) Full Transformer, great for translation and summarization
Part 2: Intermediate Deep Learning | Topics: BatchNorm, RNN, LSTM, Transfer Learning, Augmentation, Hyperparameter Tuning, Attention, Transformer