/machine-learning:ml-deep-learning-and-trainingfrom machine-learning
Reference for building and training neural networks: autodiff, initialization, normalization, optimization, transformers, CNNs, diffusion, SSMs, PyTorch, JAX, distributed training (DDP, FSDP, tensor/pipeline parallelism), mixed precision, and fine-tuning (full, PEFT/LoRA).
Ships in machine-learning. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Part 2 of 4 of the Machine Learning reference (plugin
machine-learning), covering §4–§8. Sibling skills:ml-framing-data-and-classical(§0–§3),ml-evaluation-serving-mlops-and-safety(§9–§14),ml-reference(§15–§20). Section numbers are shared across the set; a reference written as §N →skillpoints into that sibling skill.Currency: Verified August 2026. See §17 →
ml-referencefor the currency snapshot and what goes stale first.
How to read this. Reference, not tutorial. Sections are independent. Three markers:
- [DURABLE] — statistics, optimization theory, or a lesson the field has relearned every few years. Does not expire.
- [VERSIONED] — framework versions, hardware, library APIs. Moving fast; verify.
- [CONTESTED] — practitioners genuinely disagree.
⚠️ GOTCHA boxes mark the mistakes that produce a model that looks great offline and fails in production — which is the characteristic failure of this field.
The three framings that organize everything below:
- Your data is the model. Architecture choices matter far less than data quality, quantity, and whether your evaluation measures the thing you care about. Most time on a real project goes to data, and most catastrophic failures originate there (§2 →
ml-framing-data-and-classical).- The default failure mode is a number that lies. Leakage, distribution shift, a metric that doesn't match the objective, a test set you tuned against. A model that looks too good almost always is — and finding out why is the core skill.
- Almost everything you'll build should not be a neural network. For tabular data — which is most business data — gradient-boosted trees remain the strongest default (§3.1 →
ml-framing-data-and-classical). Reach for deep learning when you have perceptual data, sequences, very large datasets, or a pretrained model to adapt.
forward pass → loss → BACKPROP (reverse-mode autodiff) → optimizer step
↑
the chain rule, applied over a computation graph
[DURABLE] Backpropagation is reverse-mode automatic differentiation, not a separate
algorithm. Understanding it as autodiff over a graph explains why memory scales with
activations (you must keep intermediates for the backward pass), why gradient checkpointing
works (recompute instead of store), and why detach()/stop_gradient does what it does.
| Optimizer | Notes |
|---|---|
| SGD + momentum | Still competitive for vision; often generalizes better than adaptive methods |
| Adam / AdamW | The default. ⚠️ Use AdamW, not Adam, whenever you use weight decay — Adam's L2 penalty is not equivalent to weight decay, and this measurably hurts |
| Adafactor / 8-bit Adam | Memory-efficient for large models — optimizer state is often the largest memory consumer (§7.3) |
| Muon, Shampoo, second-order | Active area; real wins reported at scale, less settled than the marketing |
Learning rate is the hyperparameter that matters most, by a wide margin. Practices that consistently pay: warmup (especially with transformers and large batches), cosine or linear decay, and an LR-range test to find the scale. [DURABLE] If you can tune only one thing, tune the learning rate.
Batch size interacts with LR (linear scaling rule as a starting heuristic), affects generalization, and is often set by memory rather than by choice. Gradient accumulation simulates a large batch on small hardware.
[DURABLE, but the classical story is incomplete.] The textbook U-curve — underfit, sweet spot, overfit — is real for classical models. Double descent complicates it: past the interpolation threshold, test error can decrease again as you keep adding capacity, which is part of why enormously overparameterized networks work at all. Practical implication: "the model is too big, it will overfit" is not a reliable argument for deep networks, and regularization plus data scale matters more than parameter count.
[DURABLE] The dominant architecture, and worth understanding mechanically rather than by analogy.
tokens → embeddings + POSITIONAL INFORMATION
→ N × [ self-attention → residual+norm → FFN → residual+norm ]
→ output head
attention(Q,K,V) = softmax(QKᵀ/√d_k) V
Key points: multi-head attention runs several attention patterns in parallel; attention is O(n²) in sequence length, which is the central scaling problem; the FFN holds most of the parameters; and causal masking is what makes a decoder autoregressive.
The modern variants you'll meet: RoPE (rotary position embeddings — now standard),
GQA / MQA (grouped/multi-query attention — fewer KV heads, dramatically smaller KV
cache, §11.2 → ml-evaluation-serving-mlops-and-safety), FlashAttention (IO-aware exact attention — not an approximation; it
avoids materializing the n² matrix in HBM), MoE (mixture of experts — many parameters,
few active per token), sliding-window and other sparse attention, and SSMs/Mamba
as the main non-attention contender.
CNNs — still the right choice for many vision tasks, especially with limited data and compute; ConvNeXt showed a modernized CNN matches ViTs. Convolution's inductive bias (locality, translation equivariance) is a feature when data is scarce. Vision Transformers — win at scale, need more data or heavy augmentation. Diffusion models — the generative image/video/audio default; iterative denoising, with flow matching as the cleaner modern formulation. GNNs — for genuinely graph-structured data. ⚠️ Often beaten by feature engineering plus a GBDT; verify the graph structure actually carries signal. Encoder-decoder vs. decoder-only — decoder-only won for general LLMs; encoder models (BERT-family) remain excellent and much cheaper for classification and retrieval, and are badly underused because attention moved elsewhere. Embedding models — the workhorse of retrieval, RAG, semantic search, and dedup, and often the highest-value-per-FLOP thing you can deploy.
[VERSIONED] PyTorch 2.13 is current (July 2026), with a roughly quarterly cadence. The parts that matter:
torch.compile — the headline feature since 2.0. Traces your model (TorchDynamo),
lowers it (TorchInductor), and generates fused Triton (or now CuTeDSL) kernels.
Typically a substantial speedup for free. ⚠️ Watch for graph breaks — Python
constructs Dynamo can't trace force it to fall back, silently costing you the speedup;
torch._dynamo.error_on_graph_break() makes that loud rather than silent.
Recompilation on changing shapes is the other tax — mark dynamic dimensions explicitly.
Other things worth knowing: torch.export is the unified capture path (legacy
torch.jit and old ONNX flows are deprecated or removed); weights_only=True is the
default for torch.load since 2.6 — a genuine security improvement, since pickle
deserialization is arbitrary code execution; FlexAttention for custom attention
patterns without writing kernels; AOTInductor for ahead-of-time compilation.
| Layer | Tools |
|---|---|
| Frameworks | PyTorch (dominant in research and increasingly production), JAX (functional, jit/grad/vmap/pmap, strong on TPU, favored for large-scale research), TensorFlow/Keras (legacy-heavy but alive), MLX (Apple Silicon) |
| Training loops | Lightning, HF Accelerate, torchtune, raw loops (fine, and often clearer) |
| Models and data | Hugging Face transformers, datasets, tokenizers, peft, trl — effectively the industry's default distribution channel |
| Classical | scikit-learn, XGBoost, LightGBM, CatBoost, statsmodels |
| Numerics | NumPy, pandas, Polars (faster, saner API, increasingly the default for new work), DuckDB, Arrow |
| Kernels | Triton (write GPU kernels in Python), CUDA, FlashAttention, FlashInfer |
| Experiment tracking | Weights & Biases, MLflow, TensorBoard, Aim |
| Serving | vLLM, SGLang, TensorRT-LLM, Triton Inference Server, Ray Serve, llama.cpp/Ollama (§11 → ml-evaluation-serving-mlops-and-safety) |
| Orchestration | Ray, Kubeflow, Airflow, Prefect, SLURM (still the HPC standard) |
[DURABLE] Pin your versions and record them. The ML stack breaks compatibility constantly, and "it worked last month" is a real and frequent failure.
| Strategy | Splits | Use when |
|---|---|---|
| Data parallel (DDP) | The batch | The default. Model fits on one GPU |
| FSDP / ZeRO | Parameters, gradients, optimizer state | Model doesn't fit. The mainstream large-model answer |
| Tensor parallel | Individual matrices, within a layer | Very large layers; needs fast interconnect (NVLink) |
| Pipeline parallel | Layers across devices | Very deep models; ⚠️ introduces bubbles |
| Expert parallel | MoE experts | MoE models |
| Context/sequence parallel | The sequence dimension | Very long context |
[DURABLE] Real large-scale training composes several of these ("3D parallelism" and beyond), and the composition is chosen against your interconnect topology, not in the abstract. Communication is usually the bottleneck, which is why NVLink/InfiniBand topology drives the design.
[VERSIONED] PyTorch's FSDP2 now supports overlapping reduce-scatter and all-gather via separate process groups (opt-in), which increases throughput; and torchcomms is a new communications backend for PyTorch Distributed aimed at fault tolerance, scalability, and debuggability on large clusters — with plans to make it the default, including breaking changes to how ProcessGroups operate (eager initialization, single backend device). If you run large distributed jobs, that migration is on your horizon.
[DURABLE] Train in low precision, keep a master copy and accumulations in higher precision.
[DURABLE] Know where the memory goes, because "CUDA out of memory" is the most common obstacle in practice:
parameters + gradients + OPTIMIZER STATE + activations + fragmentation
↑
Adam keeps 2 extra copies — often the largest single term
A rough anchor: fp32 Adam training costs roughly 16 bytes per parameter (4 param + 4 grad + 8 optimizer state), before activations. That's why a 7B model doesn't fit on an 80 GB card without help.
The levers: gradient checkpointing (recompute activations — trades ~30% compute for
large memory savings), gradient accumulation, 8-bit or fused optimizers, FSDP
sharding, nn.LinearCrossEntropyLoss (a 2026 PyTorch addition fusing the final
projection and loss — cuts peak memory by up to 4× for large-vocabulary LM training,
where the logits tensor is genuinely enormous), and simply reducing sequence length.
[DURABLE] The order to try things, cheapest first:
1. Prompting / few-shot ← no training. Try this first, seriously
2. Retrieval (RAG) ← when the problem is missing knowledge
3. PEFT / LoRA ← when the problem is behaviour or format
4. Full fine-tuning ← when you have real data and real budget
5. Continued pretraining ← new domain, large corpus
6. Training from scratch ← almost never the right answer
⚠️ The most common expensive mistake is fine-tuning to fix a knowledge problem. Fine-tuning teaches behaviour, style, and format reliably; it teaches facts poorly and expensively. If the model doesn't know something, retrieve it.
LoRA — freeze the base, train low-rank adapters (W + BA). Tiny memory footprint,
swappable adapters, near-full-fine-tuning quality on most tasks. QLoRA adds 4-bit
quantization of the frozen base, putting single-GPU fine-tuning of large models within
reach. Other PEFT: prefix tuning, prompt tuning, IA³, DoRA.
Post-training / alignment: SFT (supervised fine-tuning on demonstrations), then preference optimization — RLHF/PPO (powerful, complex, unstable), DPO (much simpler, no reward model, now the common default), and GRPO and relatives for reasoning work. [DURABLE] Data quality dominates method choice here — a small, carefully curated SFT set routinely beats a large noisy one.
⚠️ Catastrophic forgetting is real. Fine-tuning on a narrow task degrades general capability. Mix in general data, use lower learning rates, prefer PEFT, and evaluate on capabilities you didn't train on — not just the target task.
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
4 skills in this plugin
/ml-evaluation-serving-mlops-and-safetyUse when evaluating, debugging, deploying, or governing models. Covers metrics and doing evaluation honestly, evaluating generative models, debugging training runs (NaNs, divergence, overfitting, data bugs), inference and serving (vLLM, SGLang, batching, the KV cache, quantization, what actually determines inference cost and its economics), MLOps and reproducibility (the pipeline, monitoring, drift), hardware and cost, and interpretability, fairness, security, and safety./ml-framing-data-and-classicalUse when starting or scoping an ML project: when not to use ML, framing the problem properly (target, metric, baseline, cost of errors), data splitting and the leakage that follows from getting it wrong, the data problems you'll actually hit (labels, drift, imbalance, duplicates), and the classical toolkit — gradient boosting as the tabular default (XGBoost, LightGBM, CatBoost), linear models, clustering, and the rest of scikit-learn. Includes the router for the whole machine-learning reference./ml-referenceUse when reviewing ML work for known anti-patterns (leakage, test-set tuning, benchmark gaming), weighing contested questions (deep learning vs GBDTs on tabular data, scaling laws vs diminishing returns, PyTorch vs JAX, whether benchmark progress is real, bigger models vs better data, how much MLOps tooling, open-weight vs API models), checking whether a framework, model, or hardware claim is still current (snapshot verified August 2026), finding the books, papers, courses, and people worth reading, or needing the numbers, new-project checklist, and triage. Companion to the other machine-learning skills.npx claudepluginhub the-vibey-project/vibey --plugin machine-learning