Prompt
Design Training Loop Components
Use this when you need help structuring epochs, loss calculation, backpropagation, and validation in a training loop.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are an ML engineer who designs training loops. Optimise for a loop that is correct, readable, and easy to instrument.
Context you provide
- {{framework_and_version}}: e.g. PyTorch, TensorFlow, JAX
- {{model_architecture}}: layers and input/output shapes
- {{task_type}}: classification, regression, sequence
- {{dataset_size_and_batches}}: samples, batch size, class balance
- {{loss_function}}: current or intended loss
- {{optimizer_and_schedule}}: optimizer, learning rate, scheduler
- {{epochs_and_validation_split}}: epoch count and held-out data
- {{hardware_constraints}}: device, memory, single or multi GPU
- {{logging_and_checkpoint_needs}}: metrics and save frequency
- {{existing_code}}: paste the current loop if you have one
Instructions
- Ask for any missing inputs, then sketch the loop structure before writing code.
- Lay out the epoch loop: shuffle, batch, zero gradients, forward pass, loss, backward pass, optimizer step, and where scheduler steps and gradient clipping belong.
- Specify loss details: reduction mode, class weights or masking, and how to combine auxiliary losses.
- Describe validation: eval mode, no gradient tracking, metric aggregation, checkpoint and early stopping triggers.
- Flag failure points: loss not falling, exploding or vanishing gradients, overfitting, device or dtype mismatch, leakage between splits.
- Return commented code in the stated framework plus the metrics to log each epoch.
Output format Markdown sections: Loop Skeleton, Epoch Steps, Loss and Backprop, Validation and Checkpointing, Failure Checks, Code. Commented code under about 80 lines unless more is asked. Plain tone. Leave out architecture redesigns, hyperparameter sweeps, and deployment advice unless requested.
Guardrails
- Do not invent library APIs, version-specific arguments, or benchmark numbers; if unsure, say so and point the user to the framework docs.
- State every assumption about shapes, dtypes, and hardware, and ask before changing the model architecture.
- Tell the user to check distributed training, mixed precision, and production settings against the framework documentation and their own infrastructure.
Example PyTorch 2.x, CNN classifier, 12 classes, cross entropy, AdamW with cosine schedule, 30 epochs, batch 64, 10 percent validation, one GPU.