BREAKING CHANGES: - Removed orphaned dqn.rs monolithic trainer (4,975 lines) - Removed orphaned dqn_ensemble.rs module (816 lines) - Removed orphaned tft.rs and tft_complete_int8_integration_test.rs - TFT trainer split into modular directory structure DQN Module Refactoring: - Split trainers/dqn.rs into modular structure (config.rs, statistics.rs, trainer.rs) - Fixed hyperopt 39D search space (continuous params only) - Boolean flags (use_dueling, use_double_dqn, use_per, use_noisy_nets) are now FIXED architectural decisions - use_distributional defaults to false (Candle BUG #36 - scatter_add gradient issues) Clean Module Structure: - ml/src/trainers/dqn/ directory with proper mod.rs exports - ml/src/trainers/tft/ directory with config.rs, types.rs, model.rs, trainer.rs, tests.rs - All P0 features validated: TD-error clamping, batch diversity, LR scheduler, priority staleness Documentation: - Added comprehensive docs in docs/codebase-cleanup/ - ADR-001 for DQN refactoring decisions - Rainbow DQN component matrix and quick reference guides Build Status: Compiles with zero errors 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
6.2 KiB
Wave 26 P1.6: Adaptive Dropout Scheduler Implementation
Summary
Implemented adaptive dropout scheduling that linearly decreases dropout rate from an initial high value to a final low value over a specified number of training steps. High dropout early in training prevents overfitting, while low dropout late allows fine-tuning.
Implementation Details
1. DropoutScheduler Struct
Location: /home/jgrusewski/Work/foxhunt/ml/src/dqn/network.rs (lines 13-58)
pub struct DropoutScheduler {
initial_rate: f64,
final_rate: f64,
decay_steps: usize,
current_step: usize,
}
Key Methods:
new(initial_rate, final_rate, decay_steps)- Creates schedulerget_rate()- Returns current dropout rate using linear interpolationstep(steps)- Advances scheduler by N stepscurrent_step()- Returns current step count
Formula:
progress = min(current_step / decay_steps, 1.0)
rate = initial_rate * (1 - progress) + final_rate * progress
2. QNetworkConfig Extension
Location: /home/jgrusewski/Work/foxhunt/ml/src/dqn/network.rs (lines 60-93)
New Field:
pub dropout_schedule: Option<(f64, f64, usize)> // (initial, final, decay_steps)
None- Uses staticdropout_prob(existing behavior)Some((0.5, 0.1, 100000))- Adaptive dropout from 0.5 → 0.1 over 100k steps
3. QNetwork Integration
Changes:
-
Added scheduler field (line 132):
dropout_scheduler: std::sync::Mutex<Option<DropoutScheduler>> -
Initialize scheduler in constructor (lines 229-235):
let dropout_scheduler = if let Some((initial, final_rate, steps)) = config.dropout_schedule { Some(DropoutScheduler::new(initial, final_rate, steps)) } else { None }; -
Updated forward pass (lines 258-259):
let dropout_rate = self.get_dropout_rate(); let layers = NetworkLayers::new_with_dropout_rate(&var_builder, &config, &device, dropout_rate)?; -
Step scheduler after each forward (lines 292-296):
if let Ok(mut scheduler_opt) = self.dropout_scheduler.lock() { if let Some(scheduler) = scheduler_opt.as_mut() { scheduler.step(1); } } -
New helper method (lines 408-418):
pub fn get_dropout_rate(&self) -> f64 { if let Ok(scheduler_opt) = self.dropout_scheduler.lock() { if let Some(scheduler) = scheduler_opt.as_ref() { return scheduler.get_rate(); } } self.config.dropout_prob // Fallback to static }
4. NetworkLayers Refactoring
Location: /home/jgrusewski/Work/foxhunt/ml/src/dqn/network.rs (lines 142-180)
Changes:
- Split
new()to delegate tonew_with_dropout_rate() new_with_dropout_rate()accepts explicit dropout rate parameter- Allows dynamic dropout rate during forward pass
Test Coverage
Location: /home/jgrusewski/Work/foxhunt/ml/src/dqn/tests/dropout_scheduler_tests.rs
Test Cases (11 tests):
test_dropout_scheduler_creation- Verifies initializationtest_dropout_scheduler_linear_decay- Tests linear interpolation at 0%, 25%, 50%, 75%, 100%test_dropout_scheduler_step_increment- Tests single-step incrementstest_dropout_scheduler_current_step_tracking- Verifies step countertest_qnetwork_config_with_dropout_schedule- Config validationtest_qnetwork_with_adaptive_dropout- Full integration testtest_dropout_scheduler_zero_decay_steps- Edge case: immediate final ratetest_dropout_scheduler_same_initial_and_final- Edge case: constant ratetest_dropout_scheduler_realistic_training_schedule- Real-world example (0.5 → 0.05 over 100k steps)- Tests boundary conditions and thread safety
Usage Example
// Configure adaptive dropout
let config = QNetworkConfig {
state_dim: 64,
num_actions: 3,
dropout_schedule: Some((0.5, 0.1, 100_000)), // 0.5 → 0.1 over 100k steps
..Default::default()
};
let network = QNetwork::new(config)?;
// During training:
// - Early (step 0): dropout_rate = 0.5 (high regularization)
// - Mid (step 50k): dropout_rate = 0.3 (moderate)
// - Late (step 100k+): dropout_rate = 0.1 (fine-tuning)
Benefits
-
Early Training (High Dropout):
- Prevents overfitting to noisy early experiences
- Encourages robust feature learning
- Regularizes aggressive exploration
-
Late Training (Low Dropout):
- Allows network to use full capacity
- Enables precise fine-tuning
- Improves final policy quality
-
Smooth Transition:
- Linear decay avoids sudden behavior changes
- Gradual adaptation matches learning progress
Backward Compatibility
- Default behavior unchanged:
dropout_schedule: Noneuses staticdropout_prob - Opt-in feature: Only active when
dropout_scheduleis set - No breaking changes: Existing code continues to work
Technical Notes
- Thread Safety: Uses
Mutex<Option<DropoutScheduler>>for safe concurrent access - Step Tracking: Increments on every
forward()call - Saturation:
saturating_add()prevents overflow - Progress Clamping:
min(1.0)ensures rate doesn't go below final value
Files Modified
-
/home/jgrusewski/Work/foxhunt/ml/src/dqn/network.rs- Added
DropoutSchedulerstruct (45 lines) - Extended
QNetworkConfigwithdropout_schedulefield - Modified
QNetworkto integrate scheduler - Refactored
NetworkLayers::new()to support dynamic dropout
- Added
-
/home/jgrusewski/Work/foxhunt/ml/src/dqn/tests/dropout_scheduler_tests.rs(NEW)- 11 comprehensive tests (189 lines)
- Unit tests for scheduler logic
- Integration tests for QNetwork
-
/home/jgrusewski/Work/foxhunt/ml/src/dqn/tests/mod.rs- Added
mod dropout_scheduler_tests;
- Added
Next Steps
To run tests (once existing compilation errors are fixed):
cargo test --package ml --lib dqn::tests::dropout_scheduler_tests
cargo test --package ml --lib dqn::network::tests # Existing network tests
Status
✅ Implementation complete (TDD approach) ✅ Tests written (11 test cases) ⚠️ Tests pending - blocked by existing codebase compilation errors unrelated to this feature ✅ Backward compatible ✅ Documentation complete