DocsAI Training Coach📊 Understanding Training

The Training Loop

What exactly happens when you click 'Start Training'?

When FrameTrain starts training, a precise, repeating cycle runs. Each of these cycles is called a step. Understanding this loop helps you spot problems, read logs, and set hyperparameters sensibly.

The complete training loop

For each epoch (1 to num_epochs):
  ┌─ For each batch (1 to dataset_size / batch_size):
  │   1. Load batch            → next B training examples
  │   2. Forward pass          → compute prediction
  │   3. Compute loss          → prediction vs. label
  │   4. Backward pass         → compute gradients (autograd)
  │   5. Gradient clipping     → max_grad_norm (optional, prevents explosion)
  │   6. optimizer.step()      → update weights
  │   7. optimizer.zero_grad() → reset gradients (!)
  │   8. scheduler.step()      → adjust LR (if scheduler active)
  └─ End batch

  After each epoch (or every N steps):
  9. Validation loop        → measure loss & metrics on val set
  10. Log metrics           → accuracy, F1, perplexity etc.
  11. Save checkpoint       → when best val performance is reached
  12. Check early stopping  → stop if no improvement

Training mode vs. evaluation mode

PyTorch and all modern frameworks distinguish two modes. This difference is critical and is often forgotten:

model.train() – Training Mode

  • ✓ Dropout layer is active
  • ✓ BatchNorm uses batch statistics
  • ✓ Gradients are computed
  • ✓ Weights are updated
  • → Higher loss than during evaluation (dropout)

model.eval() – Evaluation Mode

  • ✗ Dropout layer is disabled
  • ✓ BatchNorm uses global running statistics
  • ✗ Gradients are NOT computed (torch.no_grad())
  • ✗ Weights are NOT changed
  • → Honest measurement of real performance

Common mistake: validation in the wrong mode

If you forget to switch to eval() before validation, you get a lower validation loss than reality (dropout disables neurons in train mode). FrameTrain handles this correctly automatically.

What exactly is a "step"?

A step = 1 batch forward + backward + weight update. The total number of steps determines how much the model learns overall:

Total Steps = ⌈dataset_size / batch_size⌉ × num_epochs

Example:
  Dataset:    1,000 training examples
  Batch Size: 8
  Epochs:     3

  Steps/Epoch  = ⌈1,000/8⌉ = 125
  Total Steps  = 125 × 3 = 375

Practical rule of thumb:
  For LLM fine-tuning: ~500–2,000 steps is usually enough
  For classification:  ~200–1,000 steps is often enough

Checkpointing strategy

Best Checkpoint

Save only the model with the best validation loss. Saves disk space. Recommended for production.

⭐ Standard

Every N Steps/Epochs

Save regularly, regardless of performance. Useful for resuming training after a crash.

For long training runs

Last N Checkpoints

Keep only the last N checkpoints. For ensemble training or analyzing the training trajectory.

For experiments