Loss Functions
where $d$ = distance between an embedding pair, $y=1$ if dissimilar, $m$ = margin.
- Training
- Optimization
- Deep Learning
- Evaluation
- Linear Models
Machine learning, from first principles to production. Browse the index or follow a thread through the atlas.
Select a node to read a note. Colors group fields; connections show shared keywords. Use the arrow keys to move between notes and Enter to read.
where $d$ = distance between an embedding pair, $y=1$ if dissimilar, $m$ = margin.
NDCG measures ranking quality by checking whether highly relevant items appear near the top of a ranked list. It is computed in three steps: DCG → IDCG → normalize.
Without activation functions, a neural network — no matter how deep — collapses into a single linear transformation. Stacking linear layers gives you another linear function:
Most confusion comes from treating these as three points on one line. They are not. There are two independent questions:
A model is calibrated if, among all cases where it says "70%", roughly 70% are actually positive. Many classifiers violate this:
A neural network is a parameterized function $f\theta: \mathbb{R}^d \rightarrow \mathbb{R}^k$ that maps inputs to outputs by composing many simple nonlinear transformations. The…
A graph $G = (V, E)$ is a set of nodes $V$ ($|V| = n$) and edges $E \subseteq V \times V$ ($|E| = m$). It is the natural data structure when the signal lives in the relationship…
Every node embedding method fits one template:
For node $v$ at layer $l$:
Predict $yv$ for each node. Example: flag fraudulent accounts in a payments graph; classify papers in Cora.
Per layer $l$, for every node $v$ with neighbors $\mathcal{N}(v)$:
Stacking L layers gives every node an L-hop receptive field — and that's exactly the problem. Each message-passing layer is (approximately) a step of feature averaging over neig…
Motivation in one sentence: GCN and mean-aggregating GraphSAGE weight all neighbors by structure alone (degree normalization or uniform), but not all neighbors are equally infor…
The GCN (Kipf & Welling, 2017) is the "linear regression of GNNs": the simplest, most-cited instance of the Message Passing Framework, and the default baseline you must be able…
Start cheap; each rung must justify itself against the previous:
Core idea: drop the edge-masked aggregation of the Message Passing Framework and let every node attend to every node — full self-attention over the node set, with graph structur…
Dijkstra: non-negative weights, greedy with a priority queue, $O(m \log n)$ — that one-liner suffices. Add Bellman-Ford ("handles negative weights, $O(nm)$") and A ("Dijkstra +…
The long tail rules AV ML: millions of miles are boring; value is in rare events. The loop:
Train on ~400M (image, caption) pairs from the web with a symmetric InfoNCE loss over the batch: each image must pick out its own caption among all captions in the batch (and vi…
The generic trust-&-safety architecture. Account matching is a specialization of this; payment fraud, spam, bot detection are siblings.
Real industrial graphs have multiple node types and edge types. The account-matching identity graph:
Some data's most important information is relational: who connects to whom. LinkedIn's member graph, Roblox's friend/trade graph, the account–device–IP identity graph, transacti…
Retrieval-Augmented Generation: ground an LLM's answers in retrieved enterprise documents. The interview is 20% LLM, 80% retrieval + evaluation + ops.
BM25 scores a document for a query by summing, over query terms, a TF-IDF-like quantity:
The workhorse of Stage-1 retrieval everywhere (and the template for account-matching candidate generation).
Every industrial recommender — LinkedIn feed, YouTube, Roblox discovery, ads — has the same skeleton, because of one arithmetic fact: you cannot run an expensive model on 10⁸ it…
Real rankers predict many outcomes at once: pClick, pLike, pComment, pShare, pDwell>30s, pHide, pReport. Why multi-task instead of seven separate models?
These are the heavy rankers: models that score a few hundred candidates with rich features. Their shared obsession: feature crosses.
The Transformer is the encoder you'll propose for any sequence: words, user click histories (Sequence Recommenders - DIN, SASRec, BERT4Rec), account event streams (AM-05 The Sia…
This note explains, from scratch, the three terms that appear constantly in retrieval papers: softmax over a huge catalog, sampled softmax / in-batch negatives, and the logQ cor…
In retrieval, metric learning, and link prediction you almost never have explicit negatives — you have positives (clicks, matched pairs, edges) and an ocean of "everything else.…
Goal: learn an encoder $f(\cdot)$ that maps raw objects (images, sentences, user behavior histories, accounts) to vectors such that semantically same things land close together…
The serving primitive of embedding systems. Once everything is an embedding, every retrieval problem reduces to: given query vector q, find the K vectors nearest to q among N =…
Neural networks consume vectors of real numbers. But most industrial data is categorical: user IDs, item IDs, words, device models, country codes. How do you feed "user84629173"…
Every architecture in this vault is "encoder + loss." Interviewers care that you pick the loss that matches the decision the system makes. This note covers each loss, its formul…
Three intertwined topics that dominate fraud/abuse interviews.
A user is not a bag of features; a user is a sequence of actions. Order and recency carry intent: someone who viewed {crib, stroller, bottle} yesterday is in a different state t…
Knowing these two papers cold lets you anchor almost any recsys interview, especially at Google.
Training data for rankers comes from logged impressions of the previous ranker. Click probability factors (to first order) as:
Every recsys interview ends with "what about new users/items?" Have a layered answer.
Why LLM inference is its own systems discipline, in five concepts.
Updating all weights of a 70B model needs the weights + gradients + optimizer states ≈ 16 bytes/param with Adam (fp16 weight 2 + grad 2 + fp32 master 4 + two moments 8) ≈ over a…
The single most reusable architectural dichotomy in this vault. It is the same tradeoff as retrieval-vs-ranking in recsys, and Layer-1-vs-Layer-2 in account matching.
Full-graph GNN training needs the whole adjacency + all activations in memory — impossible at 10⁹ nodes / 10¹⁰ edges. Know four scaling strategies plus the PinSage case study.
Task: given a graph, predict missing/future edges. Two flagship applications in your interviews: PYMK ("People You May Know" — predict future friendships, LinkedIn Prep) and sam…
The three names you must distinguish crisply. All are instances of message passing; they differ in how they aggregate and whether they generalize to unseen nodes.
When labels are scarce or the attack is novel, you detect deviation from normal instead of similarity to known-bad. Taxonomy by how much supervision you have:
Cut the image into 16×16 patches; linearly project each patch to a vector ("patch embedding"); add position embeddings; prepend a learnable [CLS] token; run a standard Transform…
Task: localize (boxes) + classify every object. Know the two-stage/one-stage/set-prediction trichotomy and the shared vocabulary: IoU (intersection-over-union of boxes), NMS (no…
A convolutional layer slides a small learned filter (e.g., 3×3×C) across the image, computing a dot product at each location → a feature map. Three built-in assumptions (inducti…
Task: given each agent's past track + the HD map + traffic context, predict each agent's next ~8 seconds. Sits between perception and planning; Waymo wrote the canonical papers,…
Lidar returns a point cloud: ~100–300k points per sweep, each (x, y, z, intensity). Two properties break standard nets: it's an unordered set (any permutation is the same scene…
A camera image is a perspective projection — depth is lost. To place camera evidence in the bird's-eye-view plane you must reason about depth. Two canonical mechanisms:
Self-attention treats its input as a set, not a sequence — by itself it cannot tell "dog bites man" from "man bites dog." Positional encodings fix this by adding or modifying to…
A modern LLM (GPT, Llama, Claude-style) is a decoder-only transformer: tokens are embedded, position information is added, and the sequence passes through $N$ identical decoder…
Standard multi-head attention (MHA) costs $O(n^2 d)$ compute and, at inference, a KV cache that grows with the number of heads. Multi-Query Attention (MQA) lets all query heads…
Modern LLMs stabilize training by normalizing activations inside the residual branch (pre-norm) and almost always use RMSNorm — a cheaper LayerNorm variant that drops mean-cente…
A transformer never sees characters or words — it sees a sequence of integer token IDs, each of which indexes a row in the embedding table. Tokenization is the algorithm that sp…
Mixture of Experts (MoE) swaps the dense feed-forward network (FFN) in a transformer block for $N$ parallel expert FFNs plus a lightweight router (gating network) that, per toke…
Supervised Fine-Tuning (SFT), also called instruction tuning, takes a pretrained base model — which only predicts likely next tokens — and trains it on human-written or curated…
Scaling laws say that an LLM's test loss decreases predictably — as a power law — as you grow the number of parameters $N$, training tokens $D$, and compute $C$. Kaplan et al. (…
RLHF (Reinforcement Learning from Human Feedback) is the alignment step that turns a capable-but-raw fine-tuned model into a helpful, harmless assistant. The classic InstructGPT…
Pretraining trains a model on a single, brutally simple objective: given the tokens seen so far, predict the next one. The loss is per-token cross-entropy (negative log-likeliho…
Parameter-Efficient Fine-Tuning (PEFT) freezes the pretrained model and trains only a small set of extra or selected parameters, getting most of the quality of full fine-tuning…
An autoregressive LLM only gives you a probability distribution over the next token; a decoding strategy decides which token to actually emit. Greedy picks the argmax (determini…
When an LLM generates text one token at a time, each new token must attend to every previous token. Naively you would re-run the whole prompt through attention at every step, wa…
Quantization maps high-precision floating-point numbers (FP16/BF16) to low-bit integers (INT8/INT4) or compact floats (FP8/NF4) using a learned scale and zero-point, shrinking a…
Speculative decoding runs a small, fast draft model to guess the next $k$ tokens, then has the big target model verify all $k$ in a single parallel forward pass; you accept the…
A model's context window is the maximum number of tokens it can process in one forward pass. It grew from ~2k (GPT-3) to 128k–1M+ in modern models. Long context is hard because…
Prompt engineering is the practice of designing the input text so a pretrained LLM performs a task well. Because large models exhibit in-context learning (ICL) — learning a task…
An LLM by itself is a frozen text predictor: it cannot do exact arithmetic, look up live or private data, or take actions in the world. Tool use (a.k.a. function calling) fixes…
Test-time (inference-time) compute is the idea that you can trade extra computation at query time for higher accuracy, instead of only making the model bigger at training time.…
Evaluating an LLM is hard because most useful outputs are open-ended — there is no single correct string to match. So we triangulate: intrinsic measures like perplexity (how wel…
LLMs are trained to produce likely, fluent continuations — not true ones — so they confidently invent facts, citations, and APIs that don't exist; this is hallucination. The mai…
RAG = retrieval + generation. Offline, you chunk a document corpus, embed each chunk with a bi-encoder text-embedding model, and store the vectors in a vector database. Online,…
RNNs were the standard for sequences but had fundamental problems:
MLPs and CNNs assume fixed-size inputs with no inherent ordering. But many problems involve variable-length sequences where order matters:
Consider a 224×224 RGB image fed into an MLP with 1000 hidden units:
Recall the seq2seq encoder-decoder for translation:
Before diving into K-Means, understand the paradigm shift:
Random Forest is an ensemble learning algorithm that combines many decision trees into a single, more robust model. Each tree is trained on a different random subset of the data…
Naive Bayes is a family of probabilistic classifiers based on Bayes' Theorem with a "naive" assumption: all features are conditionally independent given the class.
Logistic regression is a supervised learning algorithm used for binary classification (and extendable to multi-class). It predicts the probability that an input belongs to a par…
Linear regression is a supervised learning algorithm used for regression problems (predicting continuous values). It models the relationship between a dependent variable y and o…
K-Nearest Neighbors is a non-parametric, instance-based supervised learning algorithm used for both classification and regression. It makes predictions based on the K closest tr…
A Decision Tree is a supervised learning algorithm used for both classification and regression. It splits data into subsets based on feature values, forming a tree-like structur…
Three layers of metrics — a strong answer touches all three:
Backprop is just the chain rule applied systematically over a Computational Graph. Everything below builds from one neuron up to the vectorized, minibatched form.
A supervised algorithm for classification (and, via SVR, regression). Among all hyperplanes that separate two classes, the SVM picks the one that maximizes the margin — the dist…
An ensemble method that combines many weak learners (shallow decision trees) into a strong one. Unlike Random Forests which builds trees independently and in parallel (bagging),…
A cluster is a region of high point density. DBSCAN grows clusters from dense "core" points and leaves low-density points as noise. It finds arbitrarily shaped clusters and is r…
For logits $\mathbf{z} = (z1, \dots, zK)$:
Cross-entropy $H(p, q) = -\sumi pi \log qi$ is the expected number of bits to encode events from the true distribution $p$ using a code optimized for the predicted $q$. Minimizi…
Plain SGD struggles with ravines and saddle points. Improvements:
For a mini-batch $\mathcal{B} = \{x1, \dots, xm\}$ at some layer:
The gradient at layer $\ell$ is a product of terms across all later layers: