ml-training-recipes
它能做什么
这项技能汇集了多个领域中训练神经网络的实用方法,包括语言、视觉和生物医学模型。适合希望获得可靠训练结果的机器学习从业者。
适用场景
- ✓当你要根据数据的类型和数量选择模型和训练方法的时候。
- ✓当训练出现损失骤升或内存不足,你需要排查问题的时候。
- ✓当你要为新的训练任务选择优化器或学习率调度方案的时候。
技能链接
https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/10-optimization/ml-training-recipes/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的训练损失突然飙升,然后运行因内存不足而崩溃。请帮我找出该改什么?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会根据你的数据类型和规模,匹配合适的架构和训练设置,然后逐一排查损失骤升或内存错误等常见问题的原因。它会参考优化器、领域特点和扩展方面的参考笔记。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
ML Training Recipes
Battle-tested patterns for PyTorch training across domains. Drawn from production codebases (Karpathy's autoresearch/nanochat, torchvision, HuggingFace) and modern training practice.
Reference files (read when needed)
references/architecture.md— Transformer/LLM architecture code patterns, weight initreferences/optimizers.md— Muon, AdamW hybrid, per-group LR, compiled optimizer stepsreferences/domain-specific.md— Vision, diffusion, contrastive, distributed, checkpointing, data loadingreferences/scaling-and-selection.md— Scaling laws, compute budget tables, decision trees, DGX Sparkreferences/biomedical.md— Drug discovery, protein models, medical imaging, genomics, clinical NLPreferences/experiment-loop.md— Autonomous experiment loop (autoresearch keep/discard/revert)
Architecture Selection
Pick the right model by data type and data scale:
| Data Type | < 10K samples | 10K-100K | > 100K |
|---|---|---|---|
| Images | Pretrained CNN + fine-tune | Fine-tune ViT or CNN | ViT from scratch |
| Text (gen) | Few-shot prompting | Fine-tune GPT/LLaMA (LoRA) | Pretrain from scratch |
| Tabular | XGBoost/LightGBM | Still XGBoost | Neural viable |
| Audio | Pretrained Whisper | Fine-tune AST | Train from scratch |
| Molecules | Pretrained GNN | Fine-tune molecular LM | Train GNN from scratch |
| Proteins | ESM-2 embeddings + head | Fine-tune ESM-2 | Train protein LM |
| Medical img | Pretrained CNN | nnU-Net (auto-config) | Swin-UNETR / MedSAM |
Key principle: architecture matters less than training recipe at equal compute. A well-tuned ResNet beats a poorly-tuned ViT (ref: "ResNet Strikes Back", Wightman 2021).
For biomedical domains, see references/biomedical.md.
For sequence model selection and compute planning, see references/scaling-and-selection.md.
Scaling Laws
Chinchilla rule (Hoffmann et al., 2022)
Compute-optimal training: ~20 tokens per parameter.
| Model Size | Compute-Optimal | Inference-Optimal (100×) |
|---|---|---|
| 125M | 2.5B tokens | 12.5B tokens |
| 1B | 20B tokens | 100B tokens |
| 7B | 140B tokens | 700B tokens |
FLOPs ≈ 6 × N × D (N=params, D=tokens). Data repetition limit: ~4 epochs before diminishing returns.
Training Loop
import gc, time, torch
torch.manual_seed(42)
torch.set_float32_matmul_precision("high") # TF32 on Ampere+
autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)
grad_accum_steps = total_batch_size // (batch_size * seq_len)
step = 0
while not done:
t0 = time.time()
for micro_step in range(grad_accum_steps):
with autocast_ctx:
loss = model(x, y)
(loss / grad_accum_steps).backward()
x, y = next(train_loader)
update_lr(optimizer, progress)
optimizer.step()
model.zero_grad(set_to_none=True) # frees memory vs zeroing
if loss.item() > 100: # fast-fail on divergence
print("FAIL: loss exploded"); exit(1)
torch.cuda.synchronize()
if step == 0:
gc.collect(); gc.freeze(); gc.disable() # avoid ~500ms GC stalls
step += 1
Key principles
- Gradient clipping:
clip_grad_norm_(params, 1.0)— near-universal for Transformers. Exception: Muon optimizer normalizes updates via orthogonalization, so clipping is optional. - Tensor Core alignment: batch size, hidden dims should be multiples of 8 (bf16) or 64 (A100).
- Time-based budgets make experiments comparable across hardware.
cudnn.benchmark = Truefor fixed-size vision inputs.
Optimizer Configuration
Modern LLM training uses different optimizers per parameter group:
| Parameter Type | Optimizer | LR (base) | Weight Decay |
|---|---|---|---|
| 2D weight matrices | Muon | 0.04 | 0.2 |
| Token embeddings | AdamW | 0.6 × scale | 0.0 |
| Unembedding (lm_head) | AdamW | 0.004 × scale | 0.0 |
| Per-layer scalars | AdamW | 0.005 × scale | 0.0 |
LR scaling by dimension: lr * (d_model / 768)^(-0.5) — keeps dynamics stable across sizes.
Rules of thumb
- Embeddings need higher LR (sparse updates). Never weight-decay embeddings.
- Weight decay scheduling: linearly decay WD to 0 over training.
- AdamW defaults:
常见问题
它只涵盖语言模型吗?
不是。它还涵盖视觉、扩散模型、医学影像、蛋白质和药物发现以及基因组学。
它提到了哪些优化器?
AdamW 和 Muon,以及如何为每组参数设置学习率。
它能帮忙运行大量实验吗?
能。它包含一个实验循环,会保留有帮助的改动,并撤销没有帮助的改动。
详情
作者的原始描述
Battle-tested PyTorch training recipes for all domains — LLMs, vision, diffusion, medical imaging, protein/drug discovery, spatial omics, genomics. Covers training loops, optimizer selection (AdamW, Muon), LR scheduling, mixed precision, debugging, and systematic experimentation. Use when training or fine-tuning neural networks, debugging loss spikes or OOM, choosing architectures, or optimizing GPU throughput.
热度
- GitHub 星标
- 14k
- 排名
- #11162
- 许可证
- MIT
- 更新时间
- 2026/10/10
评论
- …
同一仓库的更多技能
autoresearch
14k引导你的智能体完成一个完整的 AI 研究项目,从文献综述到实验和写作,并通过反复的测试与审查循环推进。
prompt-guard
14k让你的智能体在文本进入你的 AI 应用之前,使用 Meta 的小型分类器检查其中是否含有提示注入和越狱尝试。
pytorch-fsdp2
14k向你的智能体展示如何在训练脚本中加入 PyTorch FSDP2,使大型模型能被拆分到多张 GPU 上并正确保存。
experiment-tracking-swanlab
14k帮你的智能体设置 SwanLab,用于记录、比较和绘制机器学习实验,可在本地或你自己的服务器上运行。