Scaffold a clean PyTorch training loop with eval and early stopping
Gives you a reproducible, well-structured PyTorch training script — config, model, dataloaders, train/eval loop, metrics, checkpointing, and early stopping — tuned to your task.
You are a senior ML engineer who writes clean, reproducible PyTorch code. Task: [CLASSIFICATION / REGRESSION / SEGMENTATION — and the data, e.g. '30k labeled tabular rows, 40 features, 5 imbalanced classes']. Runtime: [single GPU / CPU / multi-GPU DDP]. PyTorch [2.x]. Produce a single runnable train.py with: 1. A config dataclass for every hyperparameter (lr, batch size, epochs, weight decay, seed, device) with sane defaults and a fixed seed for reproducibility. 2. A model module matching the task, with configurable depth/width and dropout — small and correct, not novel. 3. Weighted loss + sampler/pos_weight handling if the data is imbalanced. 4. train() and evaluate() functions: running loss, accuracy/F1 as appropriate, tqdm progress, and zero gradient leaks in eval. 5. Simple early stopping on the validation metric with patience [N]. 6. Checkpoint best-val weights to disk; log to CSV or behind a Weights & Biases flag. 7. An argparse CLI so any config value can be overridden. Guardrail: this is an engineering scaffold, not research advice. Do not claim SOTA results or invent metrics. Say plainly where I must supply real data and labels. Output the full script in one fenced block, then a 4-item runbook with exact launch commands. Success signal: the output is good only if it runs with one command, seeds are fixed, eval has no gradient leaks, and early stopping triggers on the validation metric.
Use case
Use when starting a new model and you want a sane, reproducible scaffold instead of pasting notebook fragments.
When to use this
Before training begins; specify task, data shape, and runtime (GPU/ CPU/ DDP).
Follow-up prompts
- Add mixed-precision (AMP) and gradient clipping without changing the API.
- Wrap the model in Lightning or HF Trainer and diff the two.
- Add a confusion matrix + per-class report plotting helper.
- Source
- promptfork seed
- License
- CC-BY-4.0
- Published
- 6/22/2026