Optimizers as enums with internal state and methods
This chapter builds on the previous enum-based optimizer design. We now give each variant its own internal state and encapsulate behavior using methods. This pattern is useful when you want enum-based control flow with encapsulated logic.
Defining the optimizer enum
Each optimizer variant includes its own parameters and, when needed, its internal state.
#![allow(unused)] fn main() { #[derive(Debug, Clone)] pub enum Optimizer { /// Gradient Descent optimizer with a fixed learning rate. GradientDescent { learning_rate: f64 }, /// Momentum-based optimizer with velocity tracking. Momentum { learning_rate: f64, momentum: f64, velocity: Vec<f64>, }, } }
Here, GradientDescent
stores only the learning rate, while Momentum
additionally stores its velocity vector.