-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Add Weighted Loss Functions to PyTorch : WMSE, WMAE, and Weighted Huber Loss #132049
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
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/132049
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 7167ef8 with merge base 44257c0 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @tolleybot, is there an associated issue for this PR? |
Yes, I added it the comments above: #132465 |
mikaylagawarecki
left a comment
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.
See comment on issue #132465 (comment)
|
seems like tests are failing |
|
@pytorchbot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Rebase failed due to Command |
|
Hi @mikaylagawarecki, can you please take another look at this when you have time, is there anything else needed before this can be merged? |
mikaylagawarecki
left a comment
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.
Apologies for the delay, this PR fell out of my queue
…eighted huber loss under test_nn.py
…and just added "weights" as an optional param to the mse, mae, and huber_loss. I udpated tests to reflect changes
2. removed test_mae_loss and created test_weighted_l1_loss_with_weights 3. fixed linting issues
Co-authored-by: mikaylagawarecki <[email protected]>
Add mse_loss to overrides.py
…_variadic signature to include weight updated overrides.py to remove duplicate mse_loss entry
It looks like after rebasing and some small change things have cleared upl. |
mikaylagawarecki
left a comment
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.
Thanks
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…ber Loss (pytorch#132049) #### Summary This pull request introduces new weighted loss functions to the PyTorch library: `weighted_huber_loss`, `wmse_loss`, and `wmae_loss`. These functions allow for precise control over the influence of each sample during training, important for imbalanced data or when certain samples are more significant than others. #### Changes - **`weighted_huber_loss`**: Huber loss modified to incorporate weights, providing a balance between L1 and L2 loss based on the `delta` parameter. - **`wmse_loss`** (Weighted Mean Squared Error): Applies weights to the standard MSE loss, useful for emphasizing certain samples in regression tasks. - **`wmae_loss`** (Weighted Mean Absolute Error): Adjusts MAE loss calculation by including weights, ideal for datasets with outliers. #### Code Details - **Input Validation**: Ensures `input`, `target`, and `weights` tensors match in size to prevent broadcasting errors. - **Reduction Options**: Supports `none`, `mean`, and `sum` reductions to suit various computational needs. - **Backward Compatibility**: Maintains support for deprecated arguments `size_average` and `reduce`, while encouraging use of the `reduction` argument. #### Usage Example ```python import torch input = torch.tensor([0.5, 2.5, 2.0], dtype=torch.float32) target = torch.tensor([0.0, 2.0, 1.5], dtype=torch.float32) weights = torch.tensor([1.0, 0.5, 1.5], dtype=torch.float32) loss = weighted_huber_loss(input, target, weights, delta=1.0) print(loss) ``` --- Feedback on these implementations is welcome; please let me know if further modifications are required. Resolves pytorch#132465 Pull Request resolved: pytorch#132049 Approved by: https://github.com/mikaylagawarecki Co-authored-by: mikaylagawarecki <[email protected]>
Summary
This pull request introduces new weighted loss functions to the PyTorch library:
weighted_huber_loss,wmse_loss, andwmae_loss. These functions allow for precise control over the influence of each sample during training, important for imbalanced data or when certain samples are more significant than others.Changes
weighted_huber_loss: Huber loss modified to incorporate weights, providing a balance between L1 and L2 loss based on thedeltaparameter.wmse_loss(Weighted Mean Squared Error): Applies weights to the standard MSE loss, useful for emphasizing certain samples in regression tasks.wmae_loss(Weighted Mean Absolute Error): Adjusts MAE loss calculation by including weights, ideal for datasets with outliers.Code Details
input,target, andweightstensors match in size to prevent broadcasting errors.none,mean, andsumreductions to suit various computational needs.size_averageandreduce, while encouraging use of thereductionargument.Usage Example
Feedback on these implementations is welcome; please let me know if further modifications are required.
Resolves #132465
cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki