ML coding
Machine learning algorithms and model parts written from scratch in NumPy and PyTorch, as ML coding rounds ask.
Machine learning algorithms and model parts written from scratch in NumPy and PyTorch, as ML coding rounds ask.
16 write-ups
| Title |
|---|
| ML Coding Interviews: What to Expect What the ML coding round is, who gets it, NumPy versus PyTorch, what interviewers grade, a 45-minute plan, and how to prepare. |
| PyTorch Essentials The PyTorch you need for an ML coding round: tensors versus arrays, autograd and accumulating gradients, nn.Module and parameters, devices, no_grad and eval, and the four lines of a training step. |
| Getting Shapes Right Habits that keep shapes right in ML code: name every dimension, assert at boundaries, spot broadcasts that hide bugs, reshape versus transpose, and test on tiny inputs with sizes that all differ. |
| Debugging ML Code The find-the-bugs variant of the ML coding round: how to work it, and the bugs it plants: silent broadcasts, wrong axes, off-by-one masks, leaking state, and a loss that won't fall. |
| Optimizers: SGD, Momentum, Adam SGD, momentum, Adam and AdamW in NumPy, with bias correction, L2 against decoupled weight decay, warmup and cosine schedules, checked against torch.optim. |
| Dropout and Regularization Inverted dropout as a NumPy layer with train and eval modes and a backward pass, then L2 regularization and early stopping on a model that overfits, with tests and PyTorch checks. |
| Writing a Training Loop A PyTorch training loop from full-batch steps to shuffled mini-batches, a clean eval, checkpoints that resume exactly, and the bugs that make the loss not fall. |
| Self-Attention from Scratch Scaled dot-product self-attention in NumPy and then PyTorch, with causal and padding masks, a stable softmax, tests, and the why-scale and why-norm questions. |
| Multi-Head and Grouped-Query Attention Multi-head causal self-attention in PyTorch, from a loop over heads to one fused projection and reshapes, then grouped-query and multi-query attention, tested against PyTorch's own. |
| Positional Encoding and RoPE Why attention needs positions, then learned and sinusoidal embeddings and rotary embeddings (RoPE) on queries and keys, with the relative-position property tested and the long-context follow-ups. |
| A Transformer Block A transformer encoder layer in PyTorch, from a block without residuals to post-norm and pre-norm, matched against nn.TransformerEncoderLayer, plus a one-layer NumPy transformer and an attention-free variant. |
| KV Cache and Incremental Decoding A KV cache for a small decoder in PyTorch, from recomputing the prefix to a growing cache to a preallocated one, with prefill vs decode, offset masks, an exactness test and the memory math. |
| Decoding: Greedy, Sampling and Beam Search Turning next-token logits into text in PyTorch: greedy, temperature, top-k and top-p sampling as masks over a batch, beam search checked against exhaustive search, and repetition penalties. |
| LoRA Low-rank adapters in PyTorch, from full fine-tuning to a LoRA linear layer to injecting, merging and saving adapters, with a rank experiment, exactness tests and the memory math. |
| Train a Tiny GPT A character-level GPT trained end to end in PyTorch: data and batches, a bigram baseline, the transformer, a training recipe with warmup, clipping and weight decay, sampling, and the checks that prove it learns. |
| Policy Gradients and GRPO Policy-gradient training for sequence models in PyTorch, from raw REINFORCE to a group-relative baseline to GRPO with ratio clipping and a KL penalty, plus the NaN and ratio bugs of the debugging variant. |