PromptFork

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.

Open in Studio
Prompt
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.
#pytorch#machine-learning#deep-learning#training#python
Source
promptfork seed
License
CC-BY-4.0
Published
6/22/2026

More prompts you might like

Build a robust PyTorch Dataset and DataLoader with an augmentation pipeline

Produces a custom PyTorch Dataset with correct transforms, a tuned DataLoader, and a debuggable augmentation pipeline that handles edge cases instead of throwing on the first weird sample.

#pytorch#machine-learning
New

Fine-tune a pretrained model in PyTorch with a deliberate layer-freezing strategy

Produces a transfer-learning script that swaps the right head, freezes the right layers, and uses distinct learning rates so you adapt a backbone instead of nuking its pretrained weights.

#pytorch#transfer-learning
New

RAG system prompt that refuses to hallucinate and cites sources

A retrieval-augmented system prompt that answers only from context and returns inline citations or 'I don't know'.

New

Pandas data-cleaning pipeline for a messy CSV

Produce a reproducible Pandas cleaning pipeline: types, missing values, dedupe, outliers.

New

Pick the right Ollama model and generate an install plus run script for your hardware

Produces a hardware-aware Ollama model recommendation for your task plus a ready-to-run install and run script with VRAM checks, instead of guessing a model name and hoping it fits.

#ollama#local-llm
New

Wire a local RAG pipeline to Ollama with a doc loader and vector store

Produces a complete, local-first RAG pipeline with document loading, chunking, Ollama embeddings, a vector store, retrieval, and a grounded answer step with citations, requiring no cloud APIs.

#ollama#rag
New