The Trixy ML training framework now includes enhanced early stopping functionality that supports a min_epochs parameter. This ensures that training continues for at least a specified number of epochs before early stopping can take effect, giving models sufficient time to learn initial patterns.
Standard early stopping can terminate training too early, especially for:
from trainer.base import TrainerConfig
config = TrainerConfig(
num_epochs=100, # Maximum number of epochs
min_epochs=20, # Minimum epochs before early stopping can trigger
early_stopping_patience=10, # Epochs to wait for improvement after min_epochs
# ... other parameters
)
min_epochs: Minimum number of epochs that must complete before early stopping is considerednum_epochs: Maximum number of epochs (training stops here regardless)early_stopping_patience: Number of epochs to wait for improvement after min_epochsmax_epochs: Hard upper limit (for safety)The configuration system validates these relationships:
min_epochs >= 0min_epochs <= num_epochsmin_epochs <= max_epochsnum_epochs <= max_epochsearly_stopping_patience > 0Initial Phase (epochs 0 to min_epochs-1):
Early Stopping Phase (epochs min_epochs and beyond):
early_stopping_patience# Enhanced early stopping logic
if val_loss is not None:
if val_loss < self.metrics.best_val_loss:
self.best_model_state = self.model.state_dict().copy()
self.early_stopping_counter = 0
else:
# Only increment counter after min_epochs
if epoch >= self.config.min_epochs:
self.early_stopping_counter += 1
if self.early_stopping_counter >= self.config.early_stopping_patience:
self.logger.info(f"Early stopping triggered after {epoch + 1} epochs "
f"(minimum {self.config.min_epochs} epochs completed)")
break
else:
# Reset counter if we're still below min_epochs
self.early_stopping_counter = 0
config = TrainerConfig(
trainer_name='wakeword_trainer',
num_epochs=100,
min_epochs=20, # Allow 20 epochs to learn wakeword patterns
early_stopping_patience=15, # Then wait 15 epochs for improvement
# With this config, earliest stopping is at epoch 35 (20 + 15)
)
config = TrainerConfig(
trainer_name='voice_recognition_trainer',
num_epochs=200,
min_epochs=30, # Allow 30 epochs for embeddings to stabilize
early_stopping_patience=20, # Then wait 20 epochs for improvement
# With this config, earliest stopping is at epoch 50 (30 + 20)
)
config = TrainerConfig(
num_epochs=500,
min_epochs=100, # Ensure substantial training
early_stopping_patience=50, # Very patient after min_epochs
# With this config, earliest stopping is at epoch 150 (100 + 50)
)
Epochs: 1-19 | min_epochs phase, validation worsening, counter stays 0
Epoch 20: | min_epochs reached, validation still bad, counter = 1
Epochs 21-24: | validation continues worsening, counter = 2,3,4,5
Epoch 25: | counter = 6, reaches patience (5), training stops
Result: | Training stops at epoch 25 (min_epochs + patience)
Epochs: 1-10 | min_epochs phase, validation worsening, counter stays 0
Epoch 11: | validation improves! counter reset to 0
Epochs 12-19: | validation worsens again, but still in min_epochs, counter stays 0
Epoch 20: | min_epochs reached, validation bad, counter = 1
Epochs 21-24: | validation continues worsening, counter = 2,3,4,5
Epoch 25: | counter = 6, reaches patience, training stops
Result: | Training stops at epoch 25, but best model is from epoch 11
Epochs: 1-100 | Validation improves periodically, counter never reaches patience
Result: | Training completes all 100 epochs normally
min_epochs get default value of 10No code changes required for existing trainers:
# Old code still works
config = TrainerConfig(
num_epochs=100,
early_stopping_patience=10
# min_epochs automatically set to 10
)
Model Complexity: More complex models need higher min_epochs
Dataset Size: Smaller datasets may need more epochs to stabilize
Architecture Type:
Learning Rate: Higher learning rates may converge faster
# Rapid prototyping
TrainerConfig(num_epochs=50, min_epochs=5, early_stopping_patience=5)
# Balanced training
TrainerConfig(num_epochs=100, min_epochs=15, early_stopping_patience=10)
# Production model
TrainerConfig(num_epochs=200, min_epochs=30, early_stopping_patience=20)
# Research/experimental
TrainerConfig(num_epochs=500, min_epochs=50, early_stopping_patience=30)
trainer/base.py: Enhanced early stopping logic in training looptrainer/config.py: Added validation rules and default valuestrainer/wakeword/trainer.py: Updated default configurationstrainer/voice_recognition/trainer.py: Updated default configurationsmin_epochs must be >= 0min_epochs <= num_epochs <= max_epochsEarly stopping messages now include min_epochs information:
Early stopping triggered after 35 epochs (minimum 20 epochs completed)
The enhanced functionality has been tested for:
The min_epochs parameter provides fine-grained control over early stopping behavior, ensuring models receive adequate training time while still preventing overfitting. This enhancement maintains full backward compatibility while providing powerful new training control capabilities.