A source-bounded machine-learning guide built around the professor’s recurring exam move: give a small artifact, change one condition, and ask you to justify what follows.
Boundary: lecture weeks 1–10, both supplied notebooks, and the transferable question style of four older CS exams. Old Naive Bayes and modified-ID3 specifics are not treated as current syllabus facts.
Start closed-book
Find the weak link first
Each item tests a decision or calculation, not a definition. Your result points to the lesson to study first.
Question 1 of 10Evaluation
Read once · refer back often
The notation bridge
You do not need a mathematics degree. You do need to know what each symbol is doing.
Example and features
is one input vector; is feature . stacks examples as rows. is the true target; is the prediction.
Parameters and hyperparameters
means parameters learned from training data, such as weights and biases. Hyperparameters—depth, , , or —are selected with validation data.
Loss and gradient
scores error. asks: “if weight increases slightly, which way and how much does loss move?”
Shape
means an matrix. Multiplication works when the inner dimensions match.
Recurring operators
means “add the indexed terms”; is a transpose; is a dot product; is vector length.
Tensor
A tensor is a multidimensional array. An image batch often has shape : batch, height, width, channels.
01
Weeks 1–2
ML reasoning & evaluation
Choose the task, protect the test set, match the metric to the cost, and diagnose generalization from train/validation behavior.
1 · DefineName the concept and its job.
2 · MechanismShow the formula, flow, or calculation.
3 · PerturbState what changes when one condition moves.
4 · ConcludeChoose for the scenario and name the trade-off.
When ML is appropriate
Use ML when the mapping is too complex to hand-code, data provides a usable learning signal and evaluation criterion, and future cases resemble what can be learned. Prefer rules when requirements are exact, stable, and fully specified.
Representation
Representation means the numbers shown to the model. A house may become ; a CNN instead learns useful features from pixels.
Inductive bias (built-in preference)
Every learner prefers some solutions: KNN assumes nearby points behave alike; trees prefer axis-aligned rules; CNNs assume local patterns recur across position.
Best-possible boundary
The Bayes rule chooses the most probable class. Even the best possible rule must err when classes overlap or labels are noisy.
Fitdata → features → training → learned model
Predictnew input → same feature transform → model → prediction
Learning setup
Signal available
Typical task
Recognition cue
Supervised
Desired outputs
Regression or classification
Examples include target values or labels.
Unsupervised
No desired output
Clustering or association
Discover structure or co-occurrence.
Reinforcement
Reward from actions
Sequential decision policy
Actions change later states and rewards.
Self-supervised
Targets constructed from raw data
Representation pretraining
Mask, predict, or contrast parts of the input.
The lecture frames AI problem solving as a choice among exact algorithms, fast approximate solutions, hand-written if–then knowledge, and patterns learned from data. Use ML when the last option matches the evidence and cost—not merely because data exists.
Split by purpose, not habit
Training setFit weights and transformations.
Validation setChoose depth, threshold, epoch, and other hyperparameters.
Freeze choicesNo more tuning after this point.
Test setEstimate final performance once.
DeploymentMonitor shift and real costs.
Notebook trap: the Week 3 notebook selects tree depth using X_test; Week 6 early-stops and compares models on X_test. In both cases, that set has become validation data. A new untouched test set is required for an honest final estimate.
Threshold laboratory
Thirty fixed cases: 5 positive, 25 negative. Predict positive when score ≥ threshold. Your operational target is at least 90% recall.
Calculate + justify
Actual ↓
Pred +
Pred −
Positive
TP 4
FN 1
Negative
FP 4
TN 21
Accuracy83.3%
Precision50.0%
Recall80.0%
F161.5%
The same 30 cases, seen as curves
A single threshold gives one confusion matrix. Sweeping every threshold traces a curve, and the slider below marks where you are standing on it. ROC plots ; the PR curve plots precision against recall and is the more honest view when positives are rare.
curve over all thresholdsyour current thresholdrandom-guess baseline
TPR (= recall)—
FPR (= 1 − specificity)—
ROC AUC—
Positive prevalence5 / 30 ≈ 16.7%
Read the axes before the shape. ROC AUC is threshold-free and ignores prevalence, so a rare-positive model can look excellent on ROC and poor on PR. The PR baseline is not the diagonal — it is the flat line at prevalence, here . Moving the threshold slides you along a curve; retraining is what moves the curve itself.
Formula sheet and zero-denominator rule
If a denominator is zero, report the metric as undefined (—), not as a confident zero. Trees and KNN may emit a label directly; logistic regression, SVMs, and neural classifiers often emit a score whose threshold determines the confusion matrix.
Multiclass: reduce to one-vs-rest
For each class, treat that class as positive and every other class as negative. Then aggregate.
Average
Calculation
What it answers
Exam use
Macro
Mean of class metrics
Are all classes treated equally?
Prefer when rare classes matter.
Weighted
Mean weighted by class support
How is performance across actual prevalence?
Can hide weak rare-class performance.
Micro
Pool all TP/FP/FN first
How are individual decisions doing overall?
Dominated by common classes.
Worked 3-class matrix: one-vs-rest, macro, weighted, and micro
Rows are actual classes and columns are predictions:
For class A: , so precision = recall = F1 = 0.80. The class F1 scores are A = 0.80, B = 0.60, C ≈ 0.833.
Why: macro gives every class equal weight; weighted follows class support. In ordinary single-label multiclass classification, micro-F1 equals accuracy: here .
Curve clinic
Predict the diagnosis before moving the capacity marker.
Read curves, not labels
Biasmedium
Variance riskmedium
Diagnosisbalanced region
Schematic seven-point example for reading curves; the exact shape is not a universal law. Choose capacity using the validation minimum, not the lowest training error.
This decomposition is for expected squared-error regression. Use “bias/variance” qualitatively for classification unless the question specifies such a loss.
The four train/validation cases
High train, high validation error: underfitting / high bias.
Low train, much higher validation error: overfitting / high variance.
High train, much higher validation error: high bias and high variance; improve representation/capacity and reduce overfitting.
Low train, low validation error: useful low-bias, low-variance fit.
Separate deployment warning: if development scores are strong but deployment is poor, suspect leakage or distribution shift.
Feature engineering and feature selection — compact exam recap
Preprocessing changes existing feature values by a defined transformation: thresholding, binning, scaling, or one-hot encoding. Extraction creates new features from raw or multiple inputs. Selection keeps a subset. A threshold learned using labels is supervised; one chosen from feature distribution alone is unsupervised.
With features there are subsets. Exhaustive search becomes expensive. Forward selection adds the best next feature; backward elimination removes the least useful; wrappers score subsets with a model; filters score features without repeatedly fitting the final model.
Interaction trap: a univariate filter can discard features that are weak alone but decisive together. In XOR, neither nor predicts the class by itself, while the pair predicts it perfectly. Multivariate selection evaluates the joint subset.
Fold strip
Twelve development examples plus a sealed test set. Watch which rows train and which row validates, fold by fold.
Where does each example go?
trains the modelvalidates this foldsealed test set — untouched
Model fits per setting4
Validation cases per fold3
Reported scoremean of 4 folds
The step students lose marks on: every preprocessing step that learns something — a scaler’s mean and standard deviation, an imputer’s median, a feature selector’s ranking — must be re-fitted inside each fold’s training portion only. Fitting it once on all development data leaks validation information into training and inflates every fold.
Holdout, k-fold, and leave-one-out
Strategy
Fits
Strength
Cost / risk
Holdout
1 per setting
Fast; simple on large data
Estimate depends on one split.
k-fold CV
k per setting
Every example validates once; average the folds
More compute; keep preprocessing inside each training fold.
LOOCV
n per setting
Uses n−1 training cases per fold
Very expensive; each validation fold has one case.
Cross-validation helps select hyperparameters. It does not license repeated inspection of the final test set.
Exit ticket: choose the metric
A cancer screener must catch at least 95% of true cases, while a human reviews flagged cases. Which constraint comes first, and how do you tune it?
Model answer: recall is the primary constraint because false negatives are costly. Lower the threshold until validation recall reaches at least 95%, then compare precision/workload among qualifying settings. Keep the test set untouched until the rule is frozen.
02
Week 3
Features & decision trees
Compute a weighted split, explain why the best feature wins, and trace how depth and pruning change bias and variance.
Entropy
Impurity is zero in a pure node and largest at a balanced binary node.
Use the convention .
Information gain
ID3 greedily chooses the split with maximum information gain: parent impurity minus size-weighted child impurity.
Stopping
Stop at purity, no useful features, minimum samples, or maximum depth. Pruning removes weak branches after growth.
The impurity curve
Every entropy number in this lesson is one point on this curve. Drag the mix and watch impurity rise toward the balanced middle and collapse at the pure ends.
Why balanced = 1 bit
Entropy 1.000
Gini 0.500
Majority-class error0.500
Three facts the curve gives you for free. Entropy is 0 only at a pure node; it is maximal at the balanced node and equals exactly 1 bit for two classes; and it is concave, which is precisely why any split into children can never increase the size-weighted average impurity — information gain is never negative.
Weighted split laboratory
Play-Tennis target: 9 Yes, 5 No. The parent entropy is calculated below; then every candidate is compared with the same parent.
ID3 arithmetic
Weighted entropy = …
Weighted child entropy
Information gain
Best root among fourOutlook
Grow the tree one level at a time
The same 14 Play-Tennis examples. Each level is ID3 re-running the gain calculation inside every impure child.
Depth → error → variance
Leaves5
Training errors0 / 14
Training accuracy100%
Bias / variance pressurelow bias
The trap in “depth 2 is best”. Training error reaching zero is not evidence of a good tree — it is exactly what an unrestricted tree is guaranteed to do on clean, non-contradictory data. Depth must be chosen on validation data. Pre-pruning stops growth early (maximum depth, minimum samples, minimum gain); post-pruning grows fully and then removes branches that do not pay for themselves on held-out data.
From tree to rules
Each root-to-leaf path becomes an AND rule; alternative leaves become OR rules.
IF Outlook=Overcast → Play=Yes
Trees are interpretable because the prediction follows explicit feature tests, but small data changes can produce a different tree.
Perturbation checklist
Increase maximum depth → training error cannot increase; variance usually rises.
Add a noisy feature → a greedy split may overfit unless regularized/pruned.
Duplicate a case → counts and weighted gains change; recompute.
Do not skip the weights. A small perfectly pure child can still be less useful than a feature that improves many examples. Information gain weights each child by its fraction of the parent.
Continuous feature: how to find the split threshold on paper
Sort the distinct values, test midpoints between adjacent values, compute the weighted entropy for each threshold, then choose the largest gain.
Example: values 2, 4, 7, 9 create candidate thresholds 3, 5.5, and 8. A rule such as makes left/right children; their class counts—not the numeric distance—determine entropy.
Exit ticket: why is “purest child wins” wrong?
Because a split creates several children and affects different numbers of examples. ID3 minimizes the weighted average child entropy, then subtracts it from parent entropy. One tiny pure child does not determine the whole split.
03
Weeks 3–4
KNN & SVM
Reason geometrically: who is a neighbor, which points define the margin, and what changes when one point or hyperparameter moves.
KNN: lazy local voting
Store training examples; at prediction time, measure distance, select , and vote. Small gives flexible boundaries; large smooths them.
SVM: widest-gap separator
In 2D, SVM chooses a line with the widest gap between classes. The closest points are support vectors; distant points do not affect the line unless they become support vectors.
These margin distances use the canonical scaling .
KNN board
Move the query. The plot and distance calculation use the same selected coordinate space. The default case deliberately flips after standardization.
Distance → neighbors → vote
Click or drag anywhere on the board to move the query.
Predicted class
Vote
Other coordinate space
For these ten training points, using population SD (divide by ): Feature 1 ; Feature 2 . Both statistics come from training examples only.
KNN exam facts
Scale features before distance if units differ; fit the scaler on training data only.
Choose using validation or cross-validation. LOOCV holds out each training point once.
Euclidean uses straight-line distance; Manhattan sums absolute differences; Chebyshev uses the largest coordinate difference; cosine compares direction. Choose a metric whose geometry matches the features.
For weighted voting, a common rule is weight . If an exact zero-distance match exists, predict from zero-distance matches instead of dividing by zero.
A naive query compares all cases. A kd-tree partitions by median coordinates and backtracks when another branch can beat the current neighbor radius; it helps mainly in lower dimensions.
Condensation tries to retain boundary cases and remove redundant interior points.
With standard assumptions, asymptotic 1-NN error is bounded by about twice Bayes error. Zero training error additionally assumes each point can vote for itself and there are no conflicting duplicates.
Weighted-vote example: two A neighbors at distances 1 and 2 give . One B neighbor at distance 1.5 gives , so weighted KNN predicts A.
Margin laboratory
A real soft-margin SVM is solved on these points every time you move one. Drag a point and watch what the separator does — or refuses to do.
Drag it. Prove it to yourself.
class +1class −1decision boundary margins support vector
Drag any point. Support vectors are ringed; try dragging one, then try dragging a far point.
Margin width —
—
Support vectors—
Total slack —
Three things to verify by hand before the exam. (1) Drag a point that sits far inside its own side — the boundary does not move, because its dual weight is . (2) Drag a ringed support vector — the boundary moves immediately. (3) Push toward zero with the outlier preset — the separator stops contorting to rescue that one point and the margin widens, which is exactly what “low is more regularized” means geometrically.
Perturbation table
The same three cases stated as exam-ready sentences. Use the laboratory above to confirm each row, then learn the wording here.
Which points define the boundary?
Moved case
Dual/slack state
Boundary consequence
Reason
Distant non-support point
Normally unchanged
It contributes nothing to .
Support vector
Can move
The point directly helps define the optimum separator.
Overlapping outlier
if it violates the margin
Depends strongly on
Low accepts more slack; high penalizes the violation more.
C and kernels without slogans
High C penalizes violations more, so the fit tends to contort more around training cases and may sacrifice margin. Low C tolerates more slack for a wider, more regularized separator. These are tendencies, not guarantees of perfect classification.
A kernel replaces an explicit dot product with similarity in an implicit feature space. Polynomial: . RBF: . High is more local; low is smoother. Scaling changes margins, dot products, and RBF distances, so fit scaling on training data and tune on validation data.
Multiclass SVM counts
A binary SVM needs a decomposition for classes. One-vs-rest trains classifiers. One-vs-one trains . For 5 classes, that is 5 versus 10 classifiers.
Exit ticket: add a point far from the SVM margin
If it remains correctly classified and non-supporting, its dual weight is zero and the optimum boundary does not change. Moving a support vector can change the margin because support vectors define .
04
Week 5
Neural networks by hand
Carry one example through affine layers, activations, loss, the chain rule, and a parameter update—without hiding behind a framework.
Input
Weighted sum + bias (affine)
ReLU
Output
Lossbinary cross-entropy against
One complete numeric update
Fixed example and weights keep the arithmetic inspectable. Change only the learning rate and target.
Forward → loss → backward → update
Forward pass
Backward pass and new weights
Prediction
BCE loss
The same update as a graph
Backpropagation is not a separate algorithm to memorize — it is the chain rule walked backwards along this graph. Step through it: the forward sweep fills the green values, then each backward step multiplies the incoming gradient by one local derivative.
Every backward arrow carries one multiplication. Sigmoid + binary cross-entropy is the special case where the two derivatives collapse into — that cancellation is why this pair is always chosen together, and it is a frequent “explain why” question.
Activation and its derivative
The derivative is the multiplier backpropagation uses at this unit. Where the derivative is near zero, learning through this unit stops.
Saturation, seen directly
activation derivative your
—
—
Signal after 10 such layers—
Status—
Connect the two facts. Sigmoid’s derivative peaks at and decays toward zero in both tails. Stack ten sigmoid layers and the best possible product of derivatives is — that is the vanishing-gradient problem, and it is the reason ReLU (derivative exactly 1 on the positive side) became the default.
Binary cross-entropy
One sigmoid output for a binary target.
Categorical cross-entropy
Softmax probabilities across mutually exclusive classes.
Three logits in, three probabilities out. The loss shown is the categorical cross-entropy paid for the true class you select.
Logits → probabilities → loss
Probabilities—
Sum1.000
Loss —
Gradient —
Shift invariance is the exam question. Press “add +5” and watch every probability stay identical. Adding a constant to every logit multiplies numerator and denominator by , so it cancels: softmax depends only on logit differences. This is also why implementations subtract before exponentiating — identical result, no numeric overflow.
Activation choice and saturation
Sigmoid is useful for a binary probability but saturates near 0 and 1, where its derivative becomes small. Tanh is zero-centered but also saturates. ReLU has derivative 1 for positive inputs and 0 for negative inputs; persistent negative pre-activations can create “dead” units.
Exit ticket: why does backprop run backward?
The loss depends on the output, which depends on hidden activations, which depend on earlier weights. The chain rule therefore starts with the loss derivative and multiplies local derivatives backward through that dependency graph.
05
Weeks 5–6
Optimization & regularization
Separate “can we minimize training loss?” from “will the learned solution generalize?” and diagnose the correct intervention.
SGD
Estimate the gradient from one example or a small batch. Each update is cheaper but noisier than using the full dataset.
Momentum
Accumulates a velocity so consistent directions accelerate and alternating directions cancel.
Adam-like methods use moment estimates to scale coordinates. Convenient, but learning rate and validation behavior still matter.
Optimizer diagnosis: oscillation → reduce learning rate or use momentum. Training loss down while validation rises → restore the best checkpoint and regularize. Plateau → inspect learning rate, gradients, and capacity before choosing an intervention.
Descent playground
A genuine gradient descent run on an elongated quadratic bowl — the classic hard case. Every dot is one update; the contours are level sets of the loss.
Learning rate, seen
Loss after shown steps—
Distance to optimum—
Stability limit —
Behaviour—
The one inequality worth memorizing. For a quadratic loss with largest curvature , plain gradient descent converges only while . Below it you converge; above it you diverge; near it you oscillate across the valley. The curvature ratio is the second story: a long thin valley forces a small step in the steep direction, so progress along the flat direction crawls — and that is exactly the gap momentum closes.
Batch, mini-batch, and stochastic — the same picture with noise
Batch gradient descent uses all examples per update: the smoothest path, the most expensive step. Stochastic uses one example: very cheap, very noisy, and the trajectory rattles around the optimum rather than settling. Mini-batch (typically 32–512) is the practical middle and is what “SGD” means in almost every framework.
The noise is not purely a cost. It lets the iterate escape shallow local structure and acts as a mild regularizer, which is why the noisy path often generalizes at least as well as the exact one.
Early-stopping clinic
The validation minimum chooses the checkpoint. The test curve is deliberately absent.
Choose without test leakage
Best validation epoch5
Current decision
If you inspect a “test” curve to choose the checkpoint, that split has become validation data. Final testing happens once after the checkpoint rule and all other choices are frozen.
Method
Mechanism
Likely effect
Important nuance
L2 / weight decay
Penalize squared weights
Smoothly shrink weights
Usually not exact zeros.
L1
Penalize absolute weights
Can create sparse weights
The corner at zero helps produce exact zeros.
Elastic net
Combine L1 and L2
L1 sparsity plus L2 shrinkage
Two strengths to tune.
Dropout
Randomly mask units during training
Discourage co-adaptation
Disabled at inference; all units active under the framework’s scaling convention.
Early stopping
Keep best validation checkpoint
Limits over-training
Validation is not the final test.
More data / augmentation
Increase useful variation
Often lowers variance
Augmentations must preserve the label.
Batch normalization
Normalize intermediate mini-batch statistics, then learn scale/shift
Can stabilize optimization
Training and inference statistics differ.
Regularization equations
Why L1 produces exact zeros and L2 does not
Two weights, one loss. The shaded region is the budget the penalty allows; the ellipses are loss contours. The solution sits where the smallest reachable contour first touches the budget.
Say it with the corner. The L1 budget is a diamond whose corners lie on the axes, and a corner is where a contour is most likely to make first contact — contact on an axis means a coordinate is exactly zero. The L2 budget is a smooth circle with no corners, so contact almost never lands exactly on an axis: weights shrink toward zero without reaching it. Equivalently, L1 has a non-differentiable kink at zero whose subgradient can hold a weight pinned there; L2’s gradient vanishes as and stops pushing.
Learning-rate schedules and warmup
A fixed rate is simplest. Step or exponential decay reduces it over time; cosine decay lowers it smoothly; a plateau scheduler reacts when validation improvement stalls. Warmup begins with small updates and raises the rate over early steps, which can stabilize large or sensitive models. The schedule changes step size, not the gradient direction itself.
Neural hyperparameter checklist
Architecture depth/width, activation, initialization, optimizer, learning rate and schedule, batch size, number of epochs, weight decay, dropout, normalization, augmentation, and checkpoint rule. Tune on validation evidence; do not list knobs without linking each to an observed failure.
Exit ticket: train loss falls, validation loss rises after epoch 5
The model is fitting training-specific variation after epoch 5. Restore the epoch-5 checkpoint and consider stronger regularization, augmentation, less capacity, or more data. Do not choose the epoch using the test set.
06
Week 7
CNN: pixels to tensor shapes
Perform one convolution, compute each output shape and parameter count, and explain why weight sharing matters.
Local connectivity
A kernel is a small learned weight grid; its receptive field is the input patch currently read.
Weight sharing
The same kernel slides across positions, reducing parameters and detecting a pattern wherever it occurs.
Channels
Each filter spans all input channels and produces one output channel.
Convolution under the microscope
Slide a 3×3 edge kernel across a 5×5 input. The selected nine products sum to one feature-map cell.
Multiply → sum → move
Selected output value
Feature-map shape3 × 3
Top-left 2×2 max pool
Max pooling returns the largest value in each window and has no learned weights.
Bright cells in the feature map are strong vertical edges; this kernel answers “is the left side brighter than the right?”.
Single-layer shape ledger
must be positive integers; padding must be a nonnegative integer.
Most common calculation
The assumes one enabled bias per output filter.
Output tensor32 × 32 × 16
Parameters448
Layer validityvalid
Three cases to recognize instantly.“Same” padding with stride 1 needs , which is why odd kernels (3, 5, 7) are the convention. “Valid” padding is and shrinks the map by each layer. Stride s divides the size by roughly , and the floor silently discards any incomplete final window — set above to see one input column go unused.
Multi-layer ledger
Layer
Input
Output
Parameters
Reason
Conv 3×3, same, 16
32×32×3
32×32×16
Padding 1 preserves spatial size.
MaxPool 2×2, stride 2
32×32×16
16×16×16
0
Halves height and width.
Conv 3×3, same, 32
16×16×16
16×16×32
One bias per output filter.
Flatten
16×16×32
8,192
0
Reshape only.
Dense 10
8,192
10
Dense layers often dominate parameters.
Exit ticket: double the number of filters
The output channel count doubles. Parameters in this layer double because each additional filter has its own weights and bias. The next convolution’s parameter count can also rise because its has doubled.
07
Week 7
RNN & LSTM
Trace information through time, explain vanishing gradients, and inspect how LSTM gates preserve or replace memory.
Tokenrepresented numerically; an embedding is a learned dense vector
Recurrent update
Per-token outputslot or tag at time
Shared weightssame update at every position
BPTTunroll and propagate loss through time
Why gradients vanish: backpropagation through time repeatedly multiplies derivatives through time. Values mostly below 1 shrink distant influence; values above 1 can make it explode.
A sigmoid gate acts like a valve: 0 blocks, 1 passes, and values in between pass a fraction.
Gradient decay through time
Backpropagation through time multiplies one factor per step. This plots how much of the gradient at step still reaches step .
Why long dependencies die
your factorreference: 0.5, 0.9, 1.0, 1.1numerical floor
Survives 10 steps—
Survives 50 steps—
Steps until 1% remains—
Regime—
Two different diseases, two different cures. gives vanishing gradients: distant steps contribute nothing, training silently fails to learn long dependencies, and no amount of extra epochs fixes it — the architecture must change (gating, skip connections, attention). gives exploding gradients: loud, visible NaNs, and it is cheaply fixed by gradient clipping. Notice the asymmetry — the quiet failure is the dangerous one.
LSTM state inspector
Use scalar gates to see the exact roles. Real LSTMs apply the same equations elementwise to vectors.
Keep + write + reveal
Retained
Written
New cell
Hidden
Read the cell state as a conveyor belt. is additive, so the gradient along the belt is multiplied by rather than by a weight matrix and a squashing derivative. When the network learns the factor is ≈ 1 and gradients survive hundreds of steps. That single design choice — replacing a multiply-and-squash with an additive gated highway — is the whole answer to “how does LSTM address vanishing gradients?”.
Mechanism
Information path
Strength
Bottleneck
RNN
Compress past into , step by step
Natural online processing
Long gradient path; sequential computation
LSTM
Gated cell state plus hidden state
Better controlled long memory
Still sequential and compressed
Self-attention
Direct weighted links between positions
Short dependency path; parallel over a known sequence
Needs position information; full attention cost grows quadratically with length (standard clarification)
GRU, bidirectional RNN, and seq2seq
GRU: a lighter recurrent unit with an update gate that mixes old/new state and a reset gate that controls how much past state helps form the candidate; fewer gates/parameters than LSTM, but still sequential.
Bidirectional RNN/LSTM: combines left-to-right and right-to-left states. Useful when the full input sequence is known; invalid for a strict online prediction that cannot see the future.
Sequence-to-sequence: the encoder hands a representation of the input to a decoder that emits an output sequence. Attention removes the fixed-vector bottleneck by letting each decoder step consult all encoder states.
Exit ticket: set f=1 and i=0
The cell state is copied exactly: . The output gate can still hide part of it from . This near-linear memory path helps preserve gradients across time.
08
Week 8
Transformers, exactly
Calculate scaled dot-product attention, place the causal mask correctly, and distinguish encoder, decoder, and cross-attention information flow.
Query
What the current position is looking for.
Key
What each candidate position offers for matching.
Value
The content blended after match weights are normalized.
Queries, keys, and values are learned projections of token vectors. is the key-vector length; division by keeps large dot products from making softmax excessively sharp.
when allowed; when forbidden.
Exact attention stepper
Three fixed tokens and 2D vectors. Every displayed weight is calculated, not illustrative.
Scores → mask → softmax → values
Key token
q·k
÷ √2
After mask
Softmax weight
Weighted V
Attention output
Weights sum
The attention matrix
Six tokens, real fixed embeddings, two real head projections. Every cell is : how much row attends to column .
Rows are queries
weight ≈ 0mediumweight ≈ 1masked, exactly 0
Every row sums to exactly 1 — always check this first. Softmax is applied along each row, so a row is a probability distribution over the positions that query is allowed to see. Switch on the causal mask and the matrix becomes lower-triangular: the mask deletes the upper triangle before softmax, so the remaining entries in each row renormalize to 1 rather than simply being zeroed afterwards. Zeroing after softmax would leave rows summing to less than 1 and is a classic wrong answer.
Why the matrix is why attention costs
The matrix has one cell per (query, key) pair, so a sequence of tokens builds an object: doubling the context quadruples the attention compute and memory. Against that cost you buy a constant path length — position 1 reaches position 1000 in one hop, where an RNN needs 999 sequential multiplications — and full parallelism over positions during training, since no cell depends on another cell.
That trade is the entire RNN-versus-Transformer comparison in one sentence: quadratic cost bought a constant-length gradient path and parallel training.
Mask placement: add to forbidden logits before softmax. Their probability becomes zero. Teacher forcing trace: decoder input [BOS, A, B] targets [A, B, C]; the causal mask prevents each position from seeing later target tokens.
Inside each block: residual paths preserve the block input; normalization controls scale; the feed-forward network applies the same small network independently to each token.
Positional encoding
.
Self-attention alone has no inherent sensitivity to token order. Positional information is added or learned so permutations are distinguishable.
Positional encoding, drawn
Rows are positions, columns are embedding dimensions. Low dimensions oscillate fast; high dimensions oscillate slowly — together they give every position a unique fingerprint.
Order, injected
Fastest wavelength (dim 0)≈ 6.3 positions
Slowest wavelength—
Similarity to position 0—
Vector norm—
The property the sinusoids were chosen for. Because and are fixed linear combinations of and , the encoding of a position steps ahead is a fixed rotation of the current one — the same rotation regardless of where you are in the sequence. A linear projection can therefore learn to read relative offsets, and the scheme extends to positions longer than any seen in training. Learned positional embeddings are simpler but have neither property.
What multi-head adds
Each head learns separate Q/K/V projections, performs attention in its own subspace, then the head outputs are concatenated and projected. This gives the block multiple similarity patterns at once; it does not guarantee that any head has a human-named linguistic role.
BERT and self-supervised contextual learning
Tokenization produces discrete IDs; initial embeddings plus position information enter a bidirectional encoder. Masked-language modeling predicts hidden tokens from surrounding context, creating contextual representations. The lecture also presents the original next-sentence objective. Fine-tuning adds a task head and updates some or all parameters.
Exit ticket: remove positional information
The model can still compare token content, but identical token multisets in different orders become difficult or impossible to distinguish from attention alone. It is more precise to say “no inherent order sensitivity” than “the Transformer is an unordered set model.”
09
Weeks 9–10
LLMs in practice
Select prompting, retrieval, or parameter adaptation from constraints; locate RAG failures; and choose a verification method.
Chunk
A passage cut from a source document.
Embedding
A vector used to compare semantic similarity.
Index
A searchable store of chunks and their vectors.
Rerank
Rescore a retrieved shortlist more precisely.
Offlinedocuments → chunks
Offlinechunks → embeddings → index
Onlinequery → embedding → retrieve
Onlineoptional rerank → selected evidence
Onlineprompt + evidence → answer + citation
Standard inference-time RAG changes the model’s context, not its weights. A retriever or generator can be trained separately, but retrieval at answer time is not itself fine-tuning.
Adaptation decision lab
Choose a scenario, commit to one or more methods, then check the rationale.
Constraint → intervention
Parameter-efficient adaptation
Method
What changes
Best fit
Key limit
Prompting
No weights; instructions/examples in context
Fast behavior steering
Context cost and sensitivity; no new durable knowledge guarantee
RAG
Context receives retrieved evidence
Fresh/private facts and citations
Retrieval and evidence-use failures
Prompt tuning
Learn continuous embeddings added at the model input
Very small task-specific state
May be weaker for large behavior shifts
Prefix tuning
Learn K/V prefix vectors at every Transformer layer
Deeper parameter-efficient steering
Consumes attention context at each layer
Adapters
Insert small trainable modules
Modular task/domain adaptation
Adds modules at inference
LoRA
Learn a low-rank update
Strong PEFT with limited memory
Rank and target modules matter
Partial fine-tuning
Update selected pretrained layers
Middle ground
More memory and drift than PEFT
Full fine-tuning
Update all pretrained weights
Large stable dataset + compute + deep change
Cost, forgetting, version proliferation
PEFT is motivated not only by memory and time, but also by the economic and environmental cost of repeatedly adapting very large models.
LoRA: count the parameters yourself
One frozen weight matrix, one low-rank detour. The bars are drawn to scale, so the saving is visible rather than asserted.
The most calculable PEFT question
Full per matrix—
LoRA per matrix—
Trainable, whole model—
Fraction of full tuning—
Say why it works, not just what it does. The claim is that the update needed to adapt a pretrained matrix has low intrinsic rank, even though the pretrained matrix itself does not. So freeze and learn only with . Two consequences worth a mark each: is initialized to zero so the adapted model starts exactly equal to the base model, and at deployment can be merged into one matrix, so unlike adapters or prefix tuning LoRA adds zero inference latency.
Six fact-checking strategies
Rule-based
Deterministic checks for format, ranges, or known constraints. Precise but narrow.
Retrieval-based
Compare claims with trusted external evidence. Good for factual grounding; retrieval quality is the gate.
LLM as judge
A second evaluation prompt scores support or consistency. Flexible but can share model biases.
Self-reflection
The model critiques and revises its answer. Cheap, but cannot invent missing evidence reliably.
Self-consistency
Sample multiple reasoning paths and aggregate. Helps when independent paths converge; costs more.
Multi-agent verification
Separate roles propose, retrieve, challenge, and adjudicate. More checks, more orchestration and correlated-failure risk.
Agreement is not truth. Rules and retrieval can compare a claim with external evidence. Self-reflection, self-consistency, and extra agents test internal agreement unless they also consult trusted evidence.
Domain-adversarial training
Inputsource + target examples
Feature extractorshared representation, then branch
Label branch
Feature → label predictor. Minimize source-label loss so features remain task-predictive.
Domain branch
Feature → gradient reversal () → domain classifier. The classifier learns source vs target while the extractor receives the reversed domain gradient.
The feature extractor helps the label task while confusing the domain classifier. Example: source = labeled hospital A; target = unlabeled hospital B. Preserve disease evidence while suppressing hospital/scanner cues. This assumes both domains share a useful labeling structure.
Locate the RAG failure
A wrong answer is not “RAG failed”. Six different things can break, each with a different symptom, a different test, and a different fix. Pick the symptom you observe.
Symptom → stage → fix
The diagnostic move the professor is testing. Before proposing a fix, name the test that isolates the stage. Put the gold passage into the prompt by hand: if the answer becomes correct, retrieval is at fault; if it stays wrong, generation/grounding is at fault. That single experiment splits the pipeline in two and is worth stating explicitly in an answer.
RAG failure diagnosis
Evidence absent from the corpus: acquisition/coverage failure.
Evidence exists but is split badly: chunking/indexing failure.
Relevant chunk not retrieved: embedding/query/recall failure.
Retrieved but ranked too low: ranking failure; consider a cross-encoder reranker.
Evidence reaches prompt but answer contradicts it: generation/grounding failure.
Answer is supported but citation is wrong: attribution failure.
Chunking, bi-encoder retrieval, and cross-encoder reranking
Fixed-size chunking is simple but may split a semantic unit. Semantic chunking follows topic or sentence boundaries, improving coherence at extra preprocessing cost. A bi-encoder embeds query and chunks separately for fast large-scale retrieval; a cross-encoder jointly reads each query–chunk pair for slower but more precise reranking of a small candidate set.
Exit ticket: changing facts, strict citations, no training budget
Use RAG: update the external index without retraining, retrieve trusted chunks, and require evidence-linked output. Prompting alone cannot supply unavailable private facts; fine-tuning is costly and a poor mechanism for weekly factual updates.
Active recall · answer, then read the reason
Drill bank
Forty-eight one-move questions. Each is the smallest decision an exam question can hinge on, and each answer explains not only why the right option is right but why the tempting one is wrong.
Drill 1—
0 correct · 0 attempted
Wrong answers are the useful ones. If a drill catches you, open the lesson it names and redo that lab before moving on.
Retrieval practice · the night before
Flashcards
Seventy-five cards graded the way spaced repetition works: anything you mark Again or Hard comes back inside this session, and Got it retires the card. Answer aloud before flipping — recognizing an answer is not the same as producing one.
——
Retired cards stay retired until you reset the deck.
Transfer · 100 marks
Exam arena
Older papers point to a stable question grammar. They do not define the current syllabus; they show how the professor asks.
calculate from a small artifactdraw or tracechange one conditionchoose under constraintscompare a precise pairchallenge a claim
Method marks matter. If the final number is wrong, show the formula, substitution, intermediate value, direction of change, and why your conclusion follows. Do not erase a defensible method.
Self-marking total— / 100Mark yourself after each answer
Write each attempt on paper first; open a model answer only after you have committed to yours, then enter your honest self-mark.
10 marks
Q1 · Threshold and metric
Predict positive when score ≥ threshold. At threshold 0.50: TP=40, FP=10, FN=20, TN=930. Calculate accuracy, precision, recall, and F1. A safety rule requires recall ≥ 80%. In which direction should the threshold usually move, and what is the cost?
Model answer, rationale, and marking
Rationale: lower the threshold. Some former false negatives cross into the positive region and become true positives, so recall can rise. The usual cost is more false positives and lower precision.
Marks: 1 formula + 1 result for each metric (8), 1 direction, 1 trade-off.
8 marks
Q2 · Find the leakage
A student standardizes the whole dataset, splits it, trains depths 1–10, selects the depth with the highest test F1, and reports that same F1. Identify two leaks and repair the workflow.
Model answer, rationale, and marking
Leak 1: the scaler learned test statistics. Put scaling and the model in one pipeline; inside each CV fold, fit the scaler only on that fold’s training portion and transform its validation portion. Leak 2: the test set selected depth, so it became validation data. Select depth by validation/CV, refit the frozen pipeline on the allowed development data, then use an untouched test set once. Why: any data that changes a model choice is development data, not an unbiased final test. Marks: 2 per leak, 2 per repair.
12 marks
Q3 · Weighted information gain
A node has 6 Yes, 4 No. Split A gives left (4Y,0N), right (2Y,4N). Split B gives left (3Y,1N), right (3Y,3N). Compute both gains and choose the root. Use .
Model answer, rationale, and marking
Rationale: choose A because it produces the larger reduction in weighted impurity.
Marks: 2 parent entropy, 4 split A (weighted entropy + gain), 4 split B, 2 choice and reason.
10 marks
Q4 · KNN and SVM perturbation
(a) One feature ranges 0–1 and another 0–10000. What fails in Euclidean KNN and how do you repair it? (b) Move a correctly classified non-support SVM point farther from the margin. What changes?
Model answer, rationale, and marking
(a) The 0–10000 feature dominates Euclidean distance because numeric units, not relevance, determine the sum. Fit a scaler on training data and apply the same transform to validation, test, and queries. (b) If the moved point remains non-supporting, , so it contributes nothing to the SVM weight vector and the optimum boundary/margin do not change. Marks: 3 diagnosis, 2 repair, 2 conclusion, 3 dual-weight mechanism.
12 marks
Q5 · Neural forward and backward
Use ReLU in the hidden unit, sigmoid at the output, and binary cross-entropy:
Compute , then update by SGD with .
Model answer, rationale, and marking
Rationale: since , subtracting the negative gradients raises the logit toward target 1.
Training loss falls every epoch; validation loss reaches its minimum at epoch 5 then rises. State the failure, checkpoint rule, two suitable interventions, and when the final test set is used.
Model answer and marking
Overfitting/high variance after epoch 5. Restore the epoch-5 validation checkpoint. Suitable interventions include more data/valid augmentation, stronger weight decay/dropout, reduced capacity, or early stopping. Use the final test set once, after the epoch and every other model choice are frozen. Marks: 2 diagnosis, 2 checkpoint, 1 each intervention, 2 test-set principle.
10 marks
Q7 · CNN ledger
Input 28×28×3 enters a convolution with K=5, stride 1, padding 0, dilation 1, 8 filters, and one bias per filter; then a 2×2 max pool uses stride 2 and padding 0. Give both output shapes and the convolution parameter count.
Model answer, rationale, and marking
Convolution output = 24×24×8. Pool output = 12×12×8 and pooling has no learned parameters.
Rationale: each of the 8 filters spans all 3 input channels and produces one output channel; pooling changes spatial size but not channel count.
Explain why a plain RNN can lose a dependency across many steps. Then set LSTM f=1, i=0 and explain the result.
Model answer, rationale, and marking
Backpropagation through time repeatedly multiplies derivatives; factors mostly below 1 shrink the influence of distant steps. With , , creating a direct memory path; the output gate still controls exposure in . Marks: 3 product mechanism, 1 consequence, 2 equation, 2 gate interpretation.
12 marks
Q9 · Masked attention
For query , keys , values , and : compute the dot products, scaled scores, softmax weights, and output. Then place A at decoder position 1: B is a future token. Apply the causal mask and recompute the weights and output.
Model answer, rationale, and marking
Rationale: the future token receives before softmax, so its probability is exactly zero.
A hospital assistant must answer from policies updated weekly, cite the exact passage, and run with limited training budget. Choose the primary adaptation method, outline the pipeline, name two independent failure points, then specify what the verifier checks and what counts as success.
Model answer, rationale, and marking
Choose RAG because the missing capability is fresh, citable external knowledge—not a permanent behavior change. Offline: chunk trusted policies, embed, and index them. Online: embed query, retrieve, rerank, place selected passages in the prompt, then generate an answer with passage identifiers. Independent failures include missing/poor chunks, retrieval miss, ranking miss, the generator contradicting evidence, or wrong citation attribution. Verification target: every factual claim and citation span. Success: each claim is entailed by the cited trusted passage, the passage identifier is correct, and unsupported claims are rejected or flagged. Marks: 2 choice/rationale, 3 pipeline, 1 each failure, 3 verification target and success rule.
One page · print this
Formula sheet
Everything you might have to substitute numbers into, grouped by the question that asks for it. Reconstructing it from memory is a strong check that the calculation half of the exam is secure.
Confusion matrix
Precision’s denominator is a column of the matrix; recall’s is a row. Zero denominator means undefined, not zero.
Quadratic stability: . L1 can zero a weight exactly; L2 shrinks without zeroing.
Convolution shapes
Pooling has zero parameters. “Same” padding at stride 1 needs .
Recurrence and gating
Gates use σ (a valve in ); the candidate uses tanh (content in ).
Attention
Cross-attention: queries from the decoder, keys and values from the encoder. LoRA trainable count .
Provenance
Source boundary
This guide is synthesized from the files in this folder. Standard clarifications are labeled where they go beyond a slide’s wording. Notebook mistakes are taught as leakage/debugging exercises, not repeated as best practice.
Week 1 — Introduction to core concepts, part 1
Week 2 — Introduction to core concepts, part 2
Week 3 — Traditional ML algorithms 1 + sample notebook
Week 4 — Traditional ML algorithms 2: SVM
Week 5 — DL fundamentals, structure, optimization
Week 6 — DL regularization + sample notebook
Week 7 — CNN, RNN, LSTM architectures
Week 8 — Transformers
Weeks 9–10 — Adapting foundation models
Four older exam sets — question style only where material differs
Deliberately excluded as current requirements: old exact datasets, obsolete Naive Bayes calculations, and modified-ID3 coefficients from past exams.
What the labs actually compute
No number in any lab is a hand-written illustration. Each one is calculated in the page from the stated inputs, so you can check the arithmetic against your own work and trust it when it disagrees with you.
Lab
What is genuinely computed
What is schematic
Threshold, ROC and PR
The full confusion matrix, every metric, both curves and the ROC AUC, from 30 fixed scores.
The 30 scores themselves are invented data.
Curve clinic
—
Seven-point train/validation curves are a schematic for reading shapes, not a fitted model.
Impurity curve, split laboratory, tree growth
Entropy, Gini, weighted child entropy, information gain, leaf counts and training errors on the real 14-row Play-Tennis table.
Nothing.
KNN board
Distances in the chosen metric and coordinate space, the neighbour vote, and the shaded decision region — recomputed per screen cell.
The ten training points are invented data.
Margin laboratory
A soft-margin SVM solved by sequential minimal optimization on every drag: dual weights, , , margin width, slack.
Point positions are yours to set.
Neural update, graph, activations, softmax
Full forward pass, every chain-rule gradient, the SGD step, activation derivatives, softmax and its loss.
Nothing.
Descent playground
Real gradient descent with momentum on a quadratic, including the divergence check and the limit.
The loss surface is a chosen quadratic.
L1 versus L2
Closed-form solutions: soft-thresholding for L1, proportional shrinkage for L2, exact for the isotropic loss drawn.
The loss is isotropic so the solutions stay checkable by hand.
Convolution, shape ledger
Every product and sum, the whole feature map, output shapes, parameter counts and discarded windows.
Nothing.
Gradient decay, LSTM cell
Exact powers of the per-step factor; exact gate arithmetic for and .
Gates are scalars here; real LSTMs apply the same equations elementwise to vectors.
Scaled dot products, masking, row-wise softmax and the weighted value mixture; the sinusoidal encoding formula itself.
Token embeddings and the two head projections are fixed, chosen to make content and positional behaviour visible.
LoRA budget
Exact parameter counts and ratios.
Nothing.
This page is a single file with no network requests: the mathematical typeface is embedded, so every equation renders identically offline, on any machine, and in print. Progress ticks and your theme choice are stored locally in this browser; drill answers and flashcard grades reset on reload by design, so a second attempt is genuinely a second attempt.