forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from TheAlgorithms:master #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
src/machine_learning/optimization/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| mod adam; | ||
| mod gradient_descent; | ||
| mod momentum; | ||
|
|
||
| pub use self::adam::Adam; | ||
| pub use self::gradient_descent::gradient_descent; |
144 changes: 144 additions & 0 deletions
src/machine_learning/optimization/momentum.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /// Momentum Optimization | ||
| /// | ||
| /// Momentum is an extension of gradient descent that accelerates convergence by accumulating | ||
| /// a velocity vector in directions of persistent reduction in the objective function. | ||
| /// This helps the optimizer navigate ravines and avoid getting stuck in local minima. | ||
| /// | ||
| /// The algorithm maintains a velocity vector that accumulates exponentially decaying moving | ||
| /// averages of past gradients. This allows the optimizer to build up speed in consistent | ||
| /// directions while dampening oscillations. | ||
| /// | ||
| /// The update equations are: | ||
| /// velocity_{k+1} = beta * velocity_k + gradient_of_function(x_k) | ||
| /// x_{k+1} = x_k - learning_rate * velocity_{k+1} | ||
| /// | ||
| /// where beta (typically 0.9) controls how much past gradients influence the current update. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `derivative_fn` - The function that calculates the gradient of the objective function at a given point. | ||
| /// * `x` - The initial parameter vector to be optimized. | ||
| /// * `learning_rate` - Step size for each iteration. | ||
| /// * `beta` - Momentum coefficient (typically 0.9). Higher values give more weight to past gradients. | ||
| /// * `num_iterations` - The number of iterations to run the optimization. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A reference to the optimized parameter vector `x`. | ||
| #[allow(dead_code)] | ||
| pub fn momentum( | ||
| derivative: impl Fn(&[f64]) -> Vec<f64>, | ||
| x: &mut Vec<f64>, | ||
| learning_rate: f64, | ||
| beta: f64, | ||
| num_iterations: i32, | ||
| ) -> &mut Vec<f64> { | ||
| // Initialize velocity vector to zero | ||
| let mut velocity: Vec<f64> = vec![0.0; x.len()]; | ||
|
|
||
| for _ in 0..num_iterations { | ||
| let gradient = derivative(x); | ||
|
|
||
| // Update velocity and parameters | ||
| for ((x_k, vel), grad) in x.iter_mut().zip(velocity.iter_mut()).zip(gradient.iter()) { | ||
| *vel = beta * *vel + grad; | ||
| *x_k -= learning_rate * *vel; | ||
| } | ||
| } | ||
| x | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_momentum_optimized() { | ||
| fn derivative_of_square(params: &[f64]) -> Vec<f64> { | ||
| params.iter().map(|x| 2.0 * x).collect() | ||
| } | ||
|
|
||
| let mut x: Vec<f64> = vec![5.0, 6.0]; | ||
| let learning_rate: f64 = 0.01; | ||
| let beta: f64 = 0.9; | ||
| let num_iterations: i32 = 1000; | ||
|
|
||
| let minimized_vector = momentum( | ||
| derivative_of_square, | ||
| &mut x, | ||
| learning_rate, | ||
| beta, | ||
| num_iterations, | ||
| ); | ||
|
|
||
| let test_vector = [0.0, 0.0]; | ||
| let tolerance = 1e-6; | ||
|
|
||
| for (minimized_value, test_value) in minimized_vector.iter().zip(test_vector.iter()) { | ||
| assert!((minimized_value - test_value).abs() < tolerance); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_momentum_unoptimized() { | ||
| fn derivative_of_square(params: &[f64]) -> Vec<f64> { | ||
| params.iter().map(|x| 2.0 * x).collect() | ||
| } | ||
|
|
||
| let mut x: Vec<f64> = vec![5.0, 6.0]; | ||
| let learning_rate: f64 = 0.01; | ||
| let beta: f64 = 0.9; | ||
| let num_iterations: i32 = 10; | ||
|
|
||
| let minimized_vector = momentum( | ||
| derivative_of_square, | ||
| &mut x, | ||
| learning_rate, | ||
| beta, | ||
| num_iterations, | ||
| ); | ||
|
|
||
| let test_vector = [0.0, 0.0]; | ||
| let tolerance = 1e-6; | ||
|
|
||
| for (minimized_value, test_value) in minimized_vector.iter().zip(test_vector.iter()) { | ||
| assert!((minimized_value - test_value).abs() >= tolerance); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_momentum_faster_than_gd() { | ||
| fn derivative_of_square(params: &[f64]) -> Vec<f64> { | ||
| params.iter().map(|x| 2.0 * x).collect() | ||
| } | ||
|
|
||
| // Test that momentum converges faster than gradient descent | ||
| let mut x_momentum: Vec<f64> = vec![5.0, 6.0]; | ||
| let mut x_gd: Vec<f64> = vec![5.0, 6.0]; | ||
| let learning_rate: f64 = 0.01; | ||
| let beta: f64 = 0.9; | ||
| let num_iterations: i32 = 50; | ||
|
|
||
| momentum( | ||
| derivative_of_square, | ||
| &mut x_momentum, | ||
| learning_rate, | ||
| beta, | ||
| num_iterations, | ||
| ); | ||
|
|
||
| // Gradient descent from your original implementation | ||
| for _ in 0..num_iterations { | ||
| let gradient = derivative_of_square(&x_gd); | ||
| for (x_k, grad) in x_gd.iter_mut().zip(gradient.iter()) { | ||
| *x_k -= learning_rate * grad; | ||
| } | ||
| } | ||
|
|
||
| // Momentum should be closer to zero | ||
| let momentum_distance: f64 = x_momentum.iter().map(|x| x * x).sum(); | ||
| let gd_distance: f64 = x_gd.iter().map(|x| x * x).sum(); | ||
|
|
||
| assert!(momentum_distance < gd_distance); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.