ml-training-recipes
Skill này làm gì
Skill tập hợp các mẫu thực tế để huấn luyện mạng nơ-ron trong nhiều lĩnh vực, gồm mô hình ngôn ngữ, thị giác và mô hình sinh học. Skill dành cho người làm machine learning muốn kết quả huấn luyện đáng tin cậy.
Khi nào nên dùng
- ✓Khi bạn đang chọn mô hình và cách huấn luyện phù hợp với loại và lượng dữ liệu của mình.
- ✓Khi huấn luyện có loss tăng vọt hoặc hết bộ nhớ và bạn cần gỡ lỗi.
- ✓Khi bạn muốn chọn bộ tối ưu hoặc lịch learning rate cho một lần huấn luyện mới.
Liên kết skill
https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/10-optimization/ml-training-recipes/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Loss huấn luyện của tôi đột ngột tăng vọt rồi lần chạy bị lỗi hết bộ nhớ. Bạn có thể giúp tôi tìm cần thay đổi gì không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent ghép loại và kích thước dữ liệu của bạn với một kiến trúc và thiết lập huấn luyện phù hợp, rồi lần lượt xét các nguyên nhân thường gặp như loss tăng vọt hoặc lỗi bộ nhớ. Nó dựa trên các ghi chú tham khảo về bộ tối ưu, đặc thù lĩnh vực và việc mở rộng quy mô.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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:
Câu hỏi thường gặp
Skill chỉ bao phủ mô hình ngôn ngữ phải không?
Không. Nó cũng bao phủ thị giác, khuếch tán, ảnh y tế, khám phá protein và thuốc, và bộ gen.
Nó nhắc đến những bộ tối ưu nào?
AdamW và Muon, cùng cách đặt learning rate cho từng nhóm tham số.
Nó có giúp chạy nhiều thí nghiệm không?
Có. Nó có một vòng lặp thí nghiệm giữ lại các thay đổi có ích và hoàn lại các thay đổi không có ích.
Chi tiết
Mô tả gốc của tác giả
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.
Độ phổ biến
- Sao GitHub
- 14k
- Vị trí xếp hạng
- #11162
- Giấy phép
- MIT
- Cập nhật
- 10/10/2026
Bình luận
- …
Thêm skill từ kho mã này
autoresearch
14kHướng dẫn agent của bạn qua một dự án nghiên cứu AI đầy đủ, từ tổng quan tài liệu đến thí nghiệm và viết báo, với một vòng lặp kiểm thử và rà soát lặp lại.
prompt-guard
14kCho phép agent của bạn quét văn bản để phát hiện prompt injection và các nỗ lực jailbreak trước khi nó đến ứng dụng AI, dùng bộ phân loại nhỏ của Meta.
pytorch-fsdp2
14kChỉ cho agent của bạn cách thêm PyTorch FSDP2 vào script huấn luyện để các mô hình lớn được chia ra nhiều GPU và lưu đúng cách.
experiment-tracking-swanlab
14kGiúp agent của bạn thiết lập SwanLab để ghi, so sánh và vẽ biểu đồ các thí nghiệm machine learning, trên máy cục bộ hoặc server của riêng bạn.
Skill tương tự
pytorch-patterns
280kGiúp agent của bạn dùng PyTorch cho các mô hình học máy. Áp dụng thực hành tốt nhất để huấn luyện mô hình hiệu quả và đảm bảo kết quả có thể lặp lại.
evm-token-decimals
280kGiúp agent tránh lỗi về số thập phân khi làm việc với các mạng blockchain khác nhau. Đảm bảo giá trị token chính xác trong giao dịch và phép tính.
ito-inference
280kGiúp agent kiểm tra xem các mô hình AI đã sẵn sàng để dùng chưa. Kết nối yêu cầu tài nguyên tính toán với dịch vụ mô hình đã sẵn sàng.