-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Implementation of Marginal Ranking Loss Function #733
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
Closed
jkauerl
wants to merge
9
commits into
TheAlgorithms:master
from
jkauerl:feat/ml/loss/marginal_raking
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0dc6f25
Docs: added documentation to marginal ranking loss function
jkauerl d771a44
Feat: created function signature
jkauerl 6c152f5
Doc: added return comment
jkauerl d94e3ef
Feat: finished implementation and changed type of input
jkauerl 9a29646
Test: created a test case
jkauerl 30f8b05
Feat: added the correct exports
jkauerl 7f5d3e4
Feat: now using option as a return type
jkauerl b0e552a
Test: added a macro for testing purposes as suggested
jkauerl 2fe3de7
Test: macro tests took too long
jkauerl 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Marginal Ranking | ||
| // | ||
| // The 'mrg_ranking_loss' function calculates the Marginal Ranking loss, which is a | ||
| // loss function used for ranking problems in machine learning. | ||
| // | ||
| // ## Formula | ||
| // | ||
| // For a pair of values `x_first` and `x_second`, `margin`, and `y_true`, | ||
| // the Marginal Ranking loss is calculated as: | ||
| // | ||
| // - loss = `max(0, -y_true * (x_first - x_second) + margin)`. | ||
| // | ||
| // It returns the average loss by dividing the `total_loss` by total no. of | ||
| // elements. | ||
|
|
||
| pub fn mrg_ranking_loss( | ||
| x_first: &[f64], | ||
| x_second: &[f64], | ||
| margin: f64, | ||
| y_true: f64, | ||
| ) -> Option<f64> { | ||
| if x_first.len() != x_second.len() || x_first.is_empty() || x_second.is_empty() { | ||
| return None; | ||
| } | ||
| if margin < 0.0 { | ||
| return None; | ||
| } | ||
| if y_true != 1.0 && y_true != -1.0 { | ||
| return None; | ||
| } | ||
|
|
||
| let mut total_loss: f64 = 0.0; | ||
| for (f, s) in x_first.iter().zip(x_second.iter()) { | ||
| let loss: f64 = (margin - y_true * (f - s)).max(0.0); | ||
| total_loss += loss; | ||
| } | ||
| Some(total_loss / (x_first.len() as f64)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_loss() { | ||
| let first_values: Vec<f64> = vec![1.0, 2.0, 3.0]; | ||
| let second_values: Vec<f64> = vec![2.0, 3.0, 4.0]; | ||
| let margin: f64 = 1.0; | ||
| let actual_value: f64 = -1.0; | ||
| assert_eq!( | ||
| mrg_ranking_loss(&first_values, &second_values, margin, actual_value), | ||
| Some(0.0) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_loss_invalid_length0() { | ||
| let x_first: Vec<f64> = vec![1.0, 2.0, 3.0]; | ||
| let x_second: Vec<f64> = vec![2.0, 3.0]; | ||
| let margin: f64 = 1.0; | ||
| let y_true: f64 = 1.0; | ||
| assert_eq!(mrg_ranking_loss(&x_first, &x_second, margin, y_true), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_loss_invalid_length1() { | ||
| let x_first: Vec<f64> = vec![1.0, 2.0]; | ||
| let x_second: Vec<f64> = vec![2.0, 3.0, 4.0]; | ||
| let margin: f64 = 1.0; | ||
| let y_true: f64 = 1.0; | ||
| assert_eq!(mrg_ranking_loss(&x_first, &x_second, margin, y_true), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_invalid_values() { | ||
| let x_first: Vec<f64> = vec![1.0, 2.0, 3.0]; | ||
| let x_second: Vec<f64> = vec![2.0, 3.0, 4.0]; | ||
| let margin: f64 = -1.0; | ||
| let y_true: f64 = 1.0; | ||
| assert_eq!(mrg_ranking_loss(&x_first, &x_second, margin, y_true), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_invalid_y_true() { | ||
| let x_first: Vec<f64> = vec![1.0, 2.0, 3.0]; | ||
| let x_second: Vec<f64> = vec![2.0, 3.0, 4.0]; | ||
| let margin: f64 = 1.0; | ||
| let y_true: f64 = 2.0; | ||
| assert_eq!(mrg_ranking_loss(&x_first, &x_second, margin, y_true), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_empty_prediction0() { | ||
| let x_first: Vec<f64> = vec![]; | ||
| let x_second: Vec<f64> = vec![1.0, 2.0, 3.0]; | ||
| let margin: f64 = 1.0; | ||
| let y_true: f64 = 1.0; | ||
| assert_eq!(mrg_ranking_loss(&x_first, &x_second, margin, y_true), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_marginal_ranking_empty_prediction1() { | ||
| let x_first: Vec<f64> = vec![1.0, 2.0, 3.0]; | ||
| let x_second: Vec<f64> = vec![]; | ||
| let margin: f64 = 1.0; | ||
| let y_true: f64 = 1.0; | ||
| assert_eq!(mrg_ranking_loss(&x_first, &x_second, margin, y_true), None); | ||
| } | ||
| } | ||
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,11 +1,13 @@ | ||
| mod hinge_loss; | ||
| mod huber_loss; | ||
| mod kl_divergence_loss; | ||
| mod marginal_ranking; | ||
| mod mean_absolute_error_loss; | ||
| mod mean_squared_error_loss; | ||
|
|
||
| pub use self::hinge_loss::hng_loss; | ||
| pub use self::huber_loss::huber_loss; | ||
| pub use self::kl_divergence_loss::kld_loss; | ||
| pub use self::marginal_ranking::mrg_ranking_loss; | ||
| pub use self::mean_absolute_error_loss::mae_loss; | ||
| pub use self::mean_squared_error_loss::mse_loss; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor change we should make is to provide a link to a page that contains detailed information about the algorithm instead of writing a complex document.