Skip to content

Conversation

@tolleybot
Copy link
Contributor

@tolleybot tolleybot commented Jul 29, 2024

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

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 #132465

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 29, 2024

🔗 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 Failures

As of commit 7167ef8 with merge base 44257c0 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jul 29, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@pytorch-bot pytorch-bot bot added the release notes: nn release notes category label Jul 29, 2024
@albanD albanD removed their request for review July 29, 2024 15:12
@jbschlosser
Copy link
Contributor

Hi @tolleybot, is there an associated issue for this PR?

@jbschlosser jbschlosser added module: nn Related to torch.nn triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jul 30, 2024
@tolleybot
Copy link
Contributor Author

Hi @tolleybot, is there an associated issue for this PR?

Yes, I added it the comments above: #132465

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a 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)

@mikaylagawarecki
Copy link
Contributor

seems like tests are failing

@mikaylagawarecki
Copy link
Contributor

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Rebase failed due to Command git -C /home/runner/work/pytorch/pytorch rebase refs/remotes/origin/viable/strict pull/132049/head returned non-zero exit code 1

Rebasing (1/8)
Rebasing (2/8)
Rebasing (3/8)
Auto-merging test/test_nn.py
CONFLICT (content): Merge conflict in test/test_nn.py
Auto-merging torch/nn/functional.py
error: could not apply 065dd99e88... adding weighted huber loss to functional, and adding a new test for weighted huber loss under test_nn.py
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Could not apply 065dd99e88... adding weighted huber loss to functional, and adding a new test for weighted huber loss under test_nn.py

Raised by https://github.com/pytorch/pytorch/actions/runs/10567120792

@adamreeve
Copy link

Hi @mikaylagawarecki, can you please take another look at this when you have time, is there anything else needed before this can be merged?

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a 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

tolleybot and others added 18 commits October 30, 2024 13:39
…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]>
…_variadic signature to include weight

updated overrides.py to remove duplicate mse_loss entry
@tolleybot
Copy link
Contributor Author

@mikaylagawarecki I am working to find the correct way to add weights to the l1_loss function. It seems just adding the optional weight parameter is causing issues when running tests

It looks like after rebasing and some small change things have cleared upl.

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@mikaylagawarecki
Copy link
Contributor

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 31, 2024
@pytorchmergebot
Copy link
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

rahulsingh-intel pushed a commit to rahulsingh-intel/pytorch that referenced this pull request Nov 5, 2024
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: nn Related to torch.nn open source release notes: nn release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Weighted Loss Functions to PyTorch : WMSE, WMAE, and Weighted Huber Loss

6 participants