Repository navigation
fix(processors): keep balanced weight for labels without negatives - #1821
Open
AnvitDevadiga wants to merge 3 commits into
Open
AnvitDevadiga wants to merge 3 commits into
AnvitDevadiga wants to merge 3 commits into
Conversation
pos_weight='balanced' set the weight of a label with no negative examples to 0, so every positive of that label dropped out of the loss and its gradient. Like labels without positives, such labels now get weight 1 and a warning. Fixes sunlabuiuc#1814
Test CoverageTotal: 89.49% (69692 / 77875 lines)
|
test_balanced_pos_weight pinned the weight of 0 that sunlabuiuc#1814 reports; the label without negatives now falls back to 1 like a label without positives.
test_label_vocabulary_covers_every_split_part pinned weight 0 for drug "a", which occurs in every training sample; it now falls back to 1 (sunlabuiuc#1814).
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Contributor: AnvitDevadiga
Contribution Type: Bug fix
Description: Fixes #1814. With
pos_weight="balanced", a label that is positive in every fitted sample has no negatives, so its weightnegatives / positiveswas 0.binary_cross_entropy_with_logitsmultiplies the positive term by that weight, so all of that label's positives dropped out of the loss and its gradient, with no warning. With the example from the issue, the gradient for label A was[0.0, 0.0]; it is now[-0.125, -0.125].Labels without positives already get weight 1 and a warning. Labels without negatives now get the same, which is the first option suggested in the issue.
Files to Review:
pyhealth/processors/label_processor.py:_WeightedBCE._fit_pos_weighttests/core/test_label_processor.py:test_balanced_warns_on_a_label_without_negatives, next to the existing test for labels without positivestests/core/test_apricotm.pyandtests/core/test_split_set_task.py:test_balanced_pos_weightandtest_label_vocabulary_covers_every_split_partexpected weight 0 for a label that is positive in every (training) sample. That is the behavior Balanced multilabel loss silently drops labels present in every training sample #1814 reports, so they now expect 1.Verification:
[0.0, 2.0]) and passes with the fix.pyteston every test file undertests/that mentionsbalancedorpos_weightpasses. I couldn't runtest_xgboost_modellocally because xgboost needs libomp. XGBoost computes its ownscale_pos_weightand does not use the changed function.python tools/check_pr_rules.py --base origin/master --head HEAD: all rules pass.I used Opus 5.5 to analyze the issue, reviewed the code myself, and implemented the fix.