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
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.
Every N Steps/Epochs
Save regularly, regardless of performance. Useful for resuming training after a crash.
Last N Checkpoints
Keep only the last N checkpoints. For ensemble training or analyzing the training trajectory.
Loss Functions
The training compass – which function for which task?
The loss function is the heart of training: it measures how wrong the model currently is and gives the optimizer its direction. Choosing the right loss function is just as important as the model architecture.
The most important loss functions in detail
Cross-Entropy Loss (CE)
Classification & language modelsL = -1/N Σᵢ Σₖ yᵢₖ · log(ŷᵢₖ)The standard for all classification tasks. Penalizes wrong classes logarithmically – the more confidently wrong the model is, the harsher the penalty. Also used for next-token prediction in LLM training.
Examples: Spam detection, sentiment analysis, all LLMs
In FrameTrain: chosen automatically for language models and classification.
Binary Cross-Entropy (BCE)
Binary classificationL = -(y·log(ŷ) + (1-y)·log(1-ŷ))A special case of CE for exactly two classes. Sigmoid output between 0 and 1. Good for yes/no decisions.
Examples: Good/bad, positive/negative, spam/not spam
More efficient than CE with 2 classes, noticeable on large datasets.
Mean Squared Error (MSE)
Regression (continuous values)L = 1/N Σᵢ (yᵢ - ŷᵢ)²Measures the squared distance between prediction and target. Very sensitive to outliers (squared penalty). Smoother gradient than MAE.
Examples: Price prediction, temperature regression
If your dataset has outliers, prefer MAE or Huber loss.
Mean Absolute Error (MAE)
Regression (robust to outliers)L = 1/N Σᵢ |yᵢ - ŷᵢ|Measures absolute distance. More robust than MSE with outliers because the penalty is linear instead of squared. Gradient is not differentiable at 0.
Examples: Time series with outliers, financial data
Huber loss combines MSE (near the target) and MAE (far away) – the best of both.
Special loss functions for LLMs
Causal Language Modeling Loss (CLM)
The standard for all autoregressive decoder models (GPT, LLaMA, Mistral). Cross-entropy across all token positions: the model learns to predict the next token.
Input: [BOS] "I love "
Target: "I love " [pizza] ← predict the next token
Loss = -log P(pizza | "I love")
+ potentially further token positionsMasked Language Modeling Loss (MLM)
For BERT-style encoders. 15% of tokens are masked, and the model must predict them. Bidirectional context – the model sees tokens before and after the masked token.
Input: "I [MASK] pizza very much." Target: "I love pizza very much." ← [MASK] = "love" Loss = cross-entropy only on masked positions
FrameTrain chooses automatically
Metrics & Evaluation
What do accuracy, F1, perplexity, and BLEU actually tell you?
The loss is the internal training compass. Metrics are the human-interpretable performance indicators. A low loss doesn't guarantee good metrics – and vice versa.
Classification metrics in detail
The confusion matrix – the foundation
True Positive
False Negative
False Positive
True Negative
Accuracy
(TP + TN) / (TP + TN + FP + FN)Proportion of all correct predictions. Easy to understand, but misleading on imbalanced datasets.
⚠️ Problem: with 99% negative examples, a model that ALWAYS says "negative" achieves 99% accuracy – and has learned nothing!
Precision
TP / (TP + FP)Of all cases classified as positive: how many are actually positive? Important when false positives are costly (e.g. spam filter → no real email should be marked as spam).
Recall (Sensitivity)
TP / (TP + FN)Of all true positive cases: how many were found? Important when false negatives are dangerous (e.g. disease detection → no real disease should be missed).
F1-Score
2 × (Precision × Recall) / (Precision + Recall)The harmonic mean of precision and recall. The best single number for imbalanced datasets. Penalizes extreme imbalances between precision and recall.
AUC-ROC
Area under the ROC curve (0–1)Measures the model's ability to separate classes, independent of the classification threshold. 1.0 = perfect, 0.5 = random.
Language model metrics
Perplexity (PPL)
The most important metric for language models. Measures how "surprised" the model is by the test data. Lower perplexity = better. A value of 10 means the model has to choose, on average, between ~10 equally likely tokens.
Perplexity = exp(Cross-Entropy Loss) Examples: PPL = 5 → very good language model (confident predictions) PPL = 20 → mediocre PPL = 100 → poor model (very uncertain) PPL = e^0 = 1 → perfect prediction (impossible in practice)
BLEU Score
For translation and text generation. Compares generated text with reference text based on n-gram overlap (1-gram to 4-gram). Value between 0 and 1.
Guidelines: BLEU > 0.4 = good translation, > 0.6 = very good, > 0.8 = nearly human
ROUGE (for summarization)
ROUGE-1, ROUGE-2, ROUGE-L measure recall of n-grams against reference summaries. Standard for summarization tasks.
Which metric to trust when?
Train / Validation / Test Split
The golden rule of machine learning
One of the most fundamental rules of ML: your dataset gets split into strictly separate parts. Without this separation, your metrics are worthless.
Typical dataset split
🏋️ Training set (60–80%)
The model learns EXCLUSIVELY on this data. Weights are updated.
Rule: The only data the model is allowed to "see" while learning.
✓ Allowed:
• Use all the data
• Augmentation allowed
• Shuffle between epochs
✗ Forbidden:
• Mixing in validation/test data
🔍 Validation set (10–20%)
Measured after every epoch. Used for hyperparameter selection and early stopping. The model does NOT learn from it.
Rule: May be used multiple times, but not for learning!
✓ Allowed:
• Choose hyperparameters based on val_loss
• Early stopping based on val_loss
• Checkpoint selection
✗ Forbidden:
• Training on validation data
• Using test data as validation
🎯 Test set (10–20%)
Used only ONCE, at the very end. Gives the honest final performance estimate.
Rule: The test set is sacred. Touch it only ONCE!
✓ Allowed:
• Evaluate once at the end
• Final publication/report
✗ Forbidden:
• Evaluating multiple times
• Adjusting hyperparameters afterward
• Using it as a second validation set
Data leakage – the most common and most damaging mistake
Consequence: metrics are optimistic and completely worthless. The model has "cheated".
Common cause: normalizing/standardizing over the entire dataset instead of just the training data. Correct approach: fit the scaler only on training data, then apply it to val/test.
Cross-validation: when data is scarce
With very little data, a single split can be unrepresentative. K-fold cross-validation helps:
K-Fold Cross-Validation (k=5): Fold 1: [████ Train ████ Train ████ Train ████ Train] [Val] Fold 2: [████ Train ████ Train ████ Train] [Val] [████ Train] Fold 3: [████ Train ████ Train] [Val] [████ Train ████ Train] ... → Train 5 models, average 5 val scores → More robust estimate of true performance → But: 5× the cost! Recommended when: dataset < 1,000 examples
Stratified split – essential with imbalance
With imbalanced classes, a stratified split ensures all splits have the same class distribution. Without stratification, a split can randomly end up with very few examples of a rare class in the validation set.
# Random split (bad with imbalance): Train: 95% negative, 5% positive Val: 80% negative, 20% positive ← randomly unrepresentative! # Stratified split (always recommended): Train: 95% negative, 5% positive Val: 95% negative, 5% positive ← same distribution ✓
FrameTrain recommendation