skip to content
Victor Guerra

NotesRSS feed

Self-contained concept references I keep while studying machine learning, deep learning, and systems. Each note stands on its own.

PyTorch — Tensors & Mechanics

  • PyTorch Basics — dtype conversion, `.item()`, clamp, round, NLL loss, sigmoid, sqrt, in-place updates & `no_grad`, `requires_grad`, `torch.autograd.grad`, one-hot, (n,)↔(n,1), logical reductions (`any`/`all` + `dim`, axis-that-disappears, `keepdim`, dead-neuron fraction)
  • PyTorch nn Modules — `nn.Linear`, `nn.Dropout`, custom `nn.Module`, manual weight init, `nn.Parameter` (vs `register_buffer`), `kaiming_uniform_` (symmetry breaking, variance-propagation formula, Kaiming vs Xavier exact formulas + uniform/normal variants & the factor-6), `state_dict`/`load_state_dict`
  • PyTorch Tensor Indexing: Slicing, Masking, Fancy Indexing — slicing (view) vs boolean masking (flattens) vs integer/fancy indexing; `gather`, `index_select`, `where`, `masked_fill`
  • Tensor Memory Layout: Storage, Strides, Contiguity, view/reshape/permute — storage/strides/`data_ptr`, contiguity, `view` vs `reshape`, `permute`/`transpose`, `.contiguous()`
  • Joining & Splitting Tensors: cat, stack, split, chunk — `cat` (existing dim) vs `stack` (new dim), `chunk` (count) vs `split` (size), `unbind`
  • Broadcasting in PyTorch — right-to-left alignment rules, stride-0 mechanism, (n,) vs (n,1) silent bug, `expand` vs `repeat`, `@` (matmul contracts) vs `*` (element-wise broadcasts)
  • Tensor Dtypes, Casting, and Type Promotion — dtype landscape, fp16 vs bf16, casting & truncation, type promotion, NumPy float64 trap
  • Tensor Devices & Device Management (CPU / GPU) — CPU/GPU/MPS, create-on-device, `model.to` (in-place) vs `tensor.to` (copy), device consistency, transfer overhead, `pin_memory`/`non_blocking`
  • Autograd & Automatic Differentiation — dynamic (define-by-run) vs static graphs, `grad_fn`; reverse-mode autodiff; forward-vs-reverse choice rule (n≫m → reverse, Jacobian column/row framing); the memory tradeoff (must cache activations → `no_grad` frees, gradient checkpointing recomputes); backward ≈ 2× forward → `C≈6ND`
  • Anatomy of a PyTorch Training Step — anatomy of a training step (zero_grad → forward → loss → backward → step); two accumulations (within-backward chain-rule sum vs across-step design choice), `set_to_none` + zero-vs-None table (memory/speed/momentum·weight-decay behavior), mean-vs-sum reduction ↔ effective LR, why step-after-backward (stale-grad bug), `model.eval()` vs `torch.no_grad()` (orthogonal switches for validation)
  • PyTorch Hooks & Iterating a Module's Layers — iterating layers (`named_modules`/`children`/`named_parameters`, leaf filter), forward/backward/tensor hooks, `handle.remove()` lifecycle, `p.grad` (param) vs `grad_output` (activation) grad, why to `detach().std().item()` instead of stashing outputs
  • The PyTorch `Optimizer` Class — `Optimizer` anatomy: constructor & `param_groups` (per-group LR/weight-decay overrides, scheduler writes `lr` into group dict), lazy `self.state`, index-based `state_dict` (resume-order gotcha), `zero_grad`, `step` (closure/L-BFGS, skip None-grad, `no_grad` + in-place `mul_`/`add_`)
  • Tensor Shape Typing — jaxtyping (and torchtyping) — annotating tensor shape+dtype with jaxtyping (`Float[Tensor, "batch seq dim"]`, axis grammar `*`/`#`/`...`, the `@jaxtyped` binding-scope gotcha, enforce-at-boundaries) and legacy torchtyping (`TensorType`, `patch_typeguard`)

NumPy & Python

  • NumPy Basics — one-hot, argmax, rounding, type conversion, random matrices, `default_rng`, transpose, broadcasting, reshape, reductions, logical reductions (`any`/`all` + axis-that-disappears + `keepdims`), norms, ReLU, stable sigmoid
  • Python Basics — `match`, walrus, reshape/transpose tricks, string ops, dict max, char↔int, `assert` best practices
  • Python Generators — `yield`, `yield from`, and typing — `yield` (lazy produce-and-suspend, generator objects, infinite sequences) & `yield from` (delegate to sub-iterable, forward send/throw, capture inner `return` value), flatten-nested-strings gotcha, typing generators (`Iterator[T]` vs `Generator[Y,S,R]`)
  • Type Annotations in Python — what to annotate (signatures, not locals), modern syntax (`list[int]`, `X | None`), accept-broadly/return-specifically, dataclass/TypedDict/Literal/Protocol/Final, PEP 695 generics, pyright vs mypy (hints inert without a checker), `Any` vs `object`

Training Dynamics & Optimization

  • Training Diagnostics — Debugging Why a Model Won't Learn — debugging why a model won't learn: reading loss-curve shapes (flat/plateau/spiky/NaN, `log C` random baseline), the 3 silent failures (dead ReLU, vanishing/exploding gradients + causes/fixes), residuals + norm as gradient highways, per-layer health check & log-scale gradient-norm plot, parameter-update-ratio ≈1e-3 heuristic, debugging by `.grad` state (None/zero/NaN/params-not-changing symptom→cause table), Karpathy's recipe (overfit one batch first)
  • Data Loading & Batching (PyTorch) — Dataset/Sampler/DataLoader architecture, map vs iterable style, eager/lazy/mmap loading, dtypes, feature/label `(N,)` vs `(N,1)` collate gotcha, DataLoader params (statistics vs throughput knobs), collate function, sampler hierarchy, `pin_memory` two-step transfer, batching strategies (bucket/dynamic/grad-accum), performance tuning, pitfalls
  • Memory Management During Training — where GPU memory goes (params/gradients/optimizer-state Adam×4/activations/graph), 16 bytes-per-param rule, the accumulate-loss-tensor OOM pitfall (`.item()`/`.detach()`), levers to reduce memory
  • Optimization — Hessian eigenvalues, saddle points, condition number, adaptive optimizers, Newton's method, convergence, 2nd-order at LLM scale
  • Adaptive Optimizers — AdaGrad, RMSProp, Adam — why adaptive (sparse gradients), AdaGrad (sum-of-squares → monotonic decay flaw), RMSProp (EMA fix), Adam (momentum + RMSProp + bias correction), Adam vs SGD table, AdamW pointer
  • Momentum & Nesterov — why momentum (narrow-valley oscillation / ill-conditioning, SGD noise cancels while signal accumulates); velocity EMA `v=μv+g`, steady-state `g/(1−μ)` (10× at μ=0.9), `1/(1−μ)` window, noise reduction; dampening τ; Nesterov lookahead (conceptual + PyTorch reformulated forms, anticipatory braking); Adam connection
  • Learning Rate: Effect on Convergence & How to Tune It — LR effect on convergence/stability, tuning (log-scale, loss-curve, LR range test); SGD vs SGD+momentum vs Adam update rules + when-preferred; batch-size-1 SGD; warmup + decay schedules
  • Learning-Rate Schedulers — why constant LR is suboptimal (condition-number oscillation, `η·σ²` noise ball, Robbins-Monro two-sum conditions); schedulers (Step/MultiStep/Exponential/Cosine/SGDR/CLR/OneCycle/ReduceLROnPlateau); warmup & why transformers need it (Adam 2nd-moment init + softmax saturation); LR finder, linear-vs-√k batch scaling; PyTorch mechanics & common mistakes (per-epoch vs per-batch, ordering, constructor implicit step, save state); practical guidelines by model type

Generalization & Model Fitting

  • Diagnosing Overfitting vs Underfitting — diagnosing via train/val curves, bias-variance, fixes, val<train edge case, quick triage
  • Double Descent — test error down-up-down past interpolation, three regimes (under/critical/over-parameterized), why second descent (smooth interpolation = inductive bias), curse-of-dimensionality connection
  • Regularization: L2 vs Dropout vs Early Stopping — L2 vs dropout vs early stopping mechanics, L1 vs L2 (+ geometry & Elastic Net), L2-penalty vs decoupled weight decay (AdamW), inverted dropout, early-stopping↔L2, regularization-as-prior (MAP), implicit regularization (GD large-step / SGD stable-gradients), noise injection (input/weight/label smoothing), ensembling-as-regularization, reg term & validation loss
  • Regression: OLS and R² — OLS (normal equations), R² definition/interpretation, negative R², adjusted R², polynomial fitting (`np.polyfit` / `Polynomial.fit`)
  • Feature Selection: Removing Low-Variance / Uncorrelated Features — low-variance / uncorrelated feature removal, interpreting `np.var`, univariate caveats, mutual info, model-based selection
  • Preprocessing: Fit on Train, Apply Everywhere — fit on train / apply everywhere, fit vs transform, data leakage, pipelines, CV
  • Classification vs Regression — output/goal/loss/metric differences, measuring confidence (softmax/entropy/MC-dropout vs prediction intervals), converting between (threshold vs binning + risks)
  • Cross-Validation & Data Splits — train/val/test roles, methods (holdout/k-fold/stratified/LOOCV/time-series/group), why CV rare in DL (early stopping + ensembling), split hygiene & leakage, test<val red flag
  • Evaluation Metrics — precision/recall/F1, accuracy-under-imbalance, macro/micro/weighted F1, confusion-matrix worked example + raising recall, κ/MCC, regression (MAE/RMSE/MAPE/R²), distribution divergences (KL/JS/TV/Wasserstein/NLL)
  • Class Imbalance & Weighted Sampling — why accuracy misleads + metrics (precision/recall/F1/AUROC/AUPRC/MCC/κ), sampling strategies (over/under/SMOTE), `WeightedRandomSampler` math (`w_c=1/n_c` derivation, construction pattern, replacement/0.632), class-weighted & focal loss, sampling↔loss-weighting equivalence, stratified vs balanced, calibration caveat, BN interaction

Learning Paradigms & Workflow

  • Types of Learning & Sampling — supervised/unsupervised/weakly/semi (self-training, consistency reg, pseudo-labeling)/active learning (uncertainty/margin/entropy/QBC/diversity), LM supervised-vs-unsupervised, sampling (with/without replacement, bootstrapping, MCMC)
  • Deep Learning Theory — Wide vs Deep, UAT — why DL took off (data/GPU/ReLU/dropout/BN/Adam/residuals), wide vs deep (depth → compositionality/exponential expressivity), linear regions (piecewise-linear, folding, regions-per-parameter), Universal Approximation Theorem (+ caveats: existence ≠ learnability)
  • Hyperparameters & Tuning — parameters vs hyperparameters, key HPs table, tuning methods (grid/random/Bayesian/Hyperband/evolutionary), why random beats grid

Classical ML

  • Logistic Regression, Odds & Odds Ratios — probability vs odds vs odds-ratio, log-odds/`e^β` interpretation, interaction terms, why log-loss not MSE (sigmoid saturation), assumptions & feature scaling
  • Classical ML Models — KNN, K-Means, GMM, Trees, SVM — parametric vs non-parametric, KNN (choosing k, bias-variance), K-Means (init sensitivity, choosing k) vs GMM (soft/overlapping), decision trees (high variance), kernel methods / SVM (kernel trick, hinge loss)
  • Ensembles — Bagging, Boosting, Stacking — bagging (variance ↓, Random Forest = bagging + feature randomness), boosting (bias ↓, sequential, Gradient Boosting/XGBoost), stacking, averaging/voting, bagging-vs-boosting contrast

Transformers & Sequence Models

  • Word Embeddings — embedding table `(vocab, d)`, differentiable lookup via one-hot-matmul view + sparse per-row gradient, why similar words cluster (distributional hypothesis, emergent not designed), static/context-free nature, weight tying, `padding_idx`; one-hot limits & count-based (LSA/GloVe) vs prediction-based (Word2Vec/BERT) vs contextual embeddings, Word2Vec (CBOW/skip-gram/negative-sampling); Bag-of-Words mean-pooling + sentiment pipeline, permutation-invariance limitation (negation/order-blindness), masked-mean padding fix
  • Self-Attention — scaled dot-product attention, QKV, softmax gotchas
  • RNNs — Elman Cell, BPTT, and Gating — Elman cell (`h_t = tanh(W_ih x_t + W_hh h_{t-1} + b)`, shapes, why tanh), BPTT vanishing/exploding (two factors: `W_hh` spectral norm *and* `tanh'≤1`), weight sharing, how gating fixes it (additive cell state `c_t = f_t⊙c_{t-1}+i_t⊙g_t`, `∂c_t/∂c_{t-1}=f_t≈1` = gated residual highway / constant error carousel), LSTM vs GRU
  • Attention-Free / Sub-Quadratic Architectures — sub-quadratic landscape (linear attention, SSM/Mamba, Hyena, AFT), train/infer duality; linear attention deep-dive (kernel factorization, `(QKᵀ)V→Q(KᵀV)`, `d×d` recurrent state, feature maps, decay→RetNet/RWKV)
  • Positional Encoding — sinusoidal PE, even/odd indices, frequency intuition, RoPE vs learned vs sinusoidal
  • Transformer Architecture — FFN role, attention vs FFN, memory view of FFN
  • Normalization — LayerNorm formula, variance, LayerNorm vs BatchNorm, why γ/β exist, ICS caveat (loss-smoothing), 3 BN problems LN fixes, Pre-LN vs Post-LN, RMSNorm, BN-in-residual-nets (variance control) + benefits (stable fwd/higher LR/regularization), norm variants table (Instance/Ghost/Batch-Renorm), weight norm, group norm
  • Tokenization — token granularity levels (word/subword/char/byte table), vocabulary & special tokens, subword algorithms (BPE/WordPiece/Unigram), how BPE/WordPiece handles rare words, numbers, code
  • Classical NLP — TF-IDF, n-grams, BLEU, Softmax Approximations — TF-IDF (TF·IDF, limitations), n-gram LMs + smoothing (Laplace/add-k/Good-Turing/Kneser-Ney), large-vocab softmax approximations (hierarchical/sampled/adaptive/tied), BLEU (+ METEOR/ROUGE/BERTScore/COMET)
  • Perplexity — definition, stable log-prob implementation, why not to multiply probs
  • Beam Search Decoding — breadth-limited decoding: beam/width/log-prob score, algorithm (top-k of all extensions then split completed/active), greedy vs beam, not globally optimal, length bias & normalization (`length^α`, the raw-score short-sequence bias), beam vs sampling (likelihood trap)
  • Scaling Laws & Chinchilla — Chinchilla (C≈6ND, 20 tokens/param), Kaplan-era under-training, fitted loss model, inference-cost correction (overtraining), optimal-operating-point via local-quadratic fit

Math Foundations

  • Linear Algebra Basics — vectors (dot/outer product, L0–L∞ norms, independence), matrix fundamentals (linear transforms, inverse, determinant, derivative/gradient/Jacobian/Hessian hierarchy); singular matrices (equivalent characterizations, why they break OLS), near-singular / condition number, detecting rank-deficiency
  • PCA, SVD & Eigendecomposition — dimensionality reduction (curse of dimensionality), covariance matrix, PCA (standardize → eigendecompose covariance → top-k), eigendecomposition vs SVD, matrix as sum of rank-1 outer products, applications (LSA, compression, LoRA)
  • Probability Distributions — Gaussian/Bernoulli/Categorical/Binomial/Multinomial, the generalization ladder (single-vs-n-trial × binary-vs-K-way), links to CE/BCE/softmax
  • Taylor Series — definition, common expansions, computation, relevance to ML

Reinforcement Learning

  • RLHF — Reinforcement Learning from Human Feedback — RLHF pipeline (pretraining → SFT → reward model + PPO), SFT model & its 3 roles, reward model from pairwise preferences (Bradley-Terry, architecture, margin, shift-invariance), PPO + KL penalty, reward hacking, DPO/RLAIF
  • RL Fundamentals — on-policy vs off-policy (behavior/target policies, Q-learning vs SARSA, deadly triad, importance sampling, PPO clipped ratio, RLHF connection); GAE & the bias/variance tradeoff (advantage/baseline, TD residual, λ dial, γ vs λ)

GPU — Hardware & Execution Model

ML Systems / Production

  • ML Models in Production — Drift, Degradation & Adaptation — why models degrade (covariate/concept/label drift, training-serving skew, feedback loops, staleness), drift detection (PSI/KS/KL/Wasserstein/chi-sq/classifier-based), breaking feedback loops (explore-exploit, counterfactual eval), domain adaptation (feature alignment, MMD, pseudo-labeling, fine-tuning), "great on test poor in prod" diagnosis
  • Distributed Training (Multi-GPU) — data parallelism & effective batch size (linear/√k LR scaling), sync vs async SGD (straggler vs staleness), three parallelisms (data/pipeline/tensor + 3-D), gradient checkpointing & micro-batching, DDP vs DataParallel, DistributedSampler sharding, SyncBatchNorm, optimizer-state consistency
  • Model Compression — Pruning, Distillation, Quantization — pruning (magnitude/structured/gradient, structured→real speedup, Lottery Ticket), knowledge distillation (soft targets/dark knowledge, why student beats training-small-directly), quantization/mixed-precision (fp16 vanish/overflow, master fp32 + loss scaling, Kahan sum)
  • Model Serving & Deployment — deployment challenges→fixes (latency/memory/scalability/cold-start/versioning/monitoring/cost), batched inference, Triton dynamic batching, throughput-vs-latency tradeoff

GPU / Kernels

  • Triton: Vector Add — Tile/Mask, Coalescing, Roofline — Triton tile-and-mask model via vector add: author-vs-compiler contract (+ warp latency hiding), 1-D grid / `program_id` / `offs`, `constexpr` BLOCK_SIZE, block-size↔program-count & launch-overhead (~5–10 μs fixed), tail mask (`offs < N`, correctness not perf), memory coalescing (128 B transactions, stride penalty, free in Triton), L2 incidental (runs at HBM speed), roofline & arithmetic intensity (vector add ~0.083 FLOP/byte memory-bound, ridge point ~10, matmul `O(BLOCK_K)` reuse → compute-bound), pitfalls (mask, constexpr, power-of-two, fusion removes HBM round-trips)

Misc ML Concepts

  • Loss Functions — choosing-the-right-loss table (MSE/BCE/CCE/Hinge + output activations), MSE (Gaussian MLE, outlier-sensitivity), cross-entropy from logits (softmax → −log p_true, log-sum-exp stability, `torch.max` placement), CE↔KL divergence, clean gradient forms (`ŷ−y`, `p−1_true`) & exponential-family reason, why CE not MSE (sigmoid saturation), Hinge loss (SVM/margin vs calibrated probs), Huber (δ, smooth L1), **maximum-likelihood recipe** (choose distribution → predict its params → minimize NLL; Gaussian→MSE, Bernoulli→BCE, categorical→CE), heteroscedastic regression (predict μ & σ²), robust/quantile/focal/ranking losses, loss = MLE-under-noise unifying frame
  • ML Concepts — Conv2d parameters, sigmoid, log
  • Latent Variables & Generative Models — latent variables (data lower-D than observed), generative models (simple latent prior → complex data via deep net), skeleton behind VAE/GAN/flows/diffusion
  • Activation Functions — why non-linearity, ReLU/LeakyReLU/PReLU (dying ReLU), sigmoid/tanh (derivatives 0.25/1, zero-centering, `tanh=2σ(2x)−1`), GELU (`x·Φ(x)`, GPT/BERT), Swish/SiLU (self-gated, GELU≈SiLU cousins), SwiGLU/GeGLU gated FFN (LLaMA), practice table, interview Q&A
  • Residual Networks — degradation problem, residual blocks (`x+f(x)`, additive change), shattered gradients & shorter gradient paths, order of ops (activation-first), exploding-variance (why BN needed), why ResNets work (ensembles of shallow nets, smoother surface, wider>deeper), architectures (ResNet/bottleneck/DenseNet/U-Net)
  • Convolution (Conv2d) — Conv2d: weight sharing & translation equivariance, cross-correlation vs true convolution (framework gotcha), multi-channel formula & shapes `(C_out,C_in,k,k)`, output-size formula, im2col (conv as GEMM), receptive field `1+L(k−1)` (why 2×3×3 beats 5×5), CNN building blocks (filter sizes/1×1/FC↔conv, padding, max-vs-avg pooling, strided-conv, upsampling, CNN-for-text), equivariance-vs-invariance, stride/dilation, spatial-dropout/cutout, applications (YOLO/segmentation), params & MACs-vs-FLOPs