Repository navigation
Implement Huber loss function in Machine Learning loss algorithms. #697
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
afdc0b6
feat: implement Huber loss
sozelfist 1684114
chore: fix add `huber_loss.rs` to `DIRECTORY.md`
sozelfist 23657c1
ref: handeling exception cases
sozelfist f307f77
ref(tests): add coverage tests
sozelfist 77eead3
ref(tests): rewrite tests using macro
sozelfist a77a0bc
chore: remove duplicated src dir
sozelfist 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
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,74 @@ | ||
| /// Computes the Huber loss between arrays of true and predicted values. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `y_true` - An array of true values. | ||
| /// * `y_pred` - An array of predicted values. | ||
| /// * `delta` - The threshold parameter that controls the linear behavior of the loss function. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The average Huber loss for all pairs of true and predicted values. | ||
| pub fn huber_loss(y_true: &[f64], y_pred: &[f64], delta: f64) -> Option<f64> { | ||
| if y_true.len() != y_pred.len() || y_pred.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| let loss: f64 = y_true | ||
| .iter() | ||
| .zip(y_pred.iter()) | ||
| .map(|(&true_val, &pred_val)| { | ||
| let residual = (true_val - pred_val).abs(); | ||
| match residual { | ||
| r if r <= delta => 0.5 * r.powi(2), | ||
| _ => delta * residual - 0.5 * delta.powi(2), | ||
| } | ||
| }) | ||
| .sum(); | ||
|
|
||
| Some(loss / (y_pred.len() as f64)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| macro_rules! huber_loss_tests { | ||
| ($($name:ident: $test_case:expr,)*) => { | ||
| $( | ||
| #[test] | ||
| fn $name() { | ||
| let (y_true, y_pred, delta, expected_loss) = $test_case; | ||
| assert_eq!(huber_loss(&y_true, &y_pred, delta), expected_loss); | ||
| } | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| huber_loss_tests! { | ||
| test_huber_loss_residual_less_than_delta: ( | ||
| vec![10.0, 8.0, 12.0], | ||
| vec![9.0, 7.0, 11.0], | ||
| 1.0, | ||
| Some(0.5) | ||
| ), | ||
| test_huber_loss_residual_greater_than_delta: ( | ||
| vec![3.0, 5.0, 7.0], | ||
| vec![2.0, 4.0, 8.0], | ||
| 0.5, | ||
| Some(0.375) | ||
| ), | ||
| test_huber_loss_invalid_length: ( | ||
| vec![10.0, 8.0, 12.0], | ||
| vec![7.0, 6.0], | ||
| 1.0, | ||
| None | ||
| ), | ||
| test_huber_loss_empty_prediction: ( | ||
| vec![10.0, 8.0, 12.0], | ||
| vec![], | ||
| 1.0, | ||
| 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,9 +1,11 @@ | ||
| mod hinge_loss; | ||
| mod huber_loss; | ||
| mod kl_divergence_loss; | ||
| 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::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.
Uh oh!
There was an error while loading. Please reload this page.