Skip to content

fix(processors): keep balanced weight for labels without negatives - #1821

Open
AnvitDevadiga wants to merge 3 commits into
sunlabuiuc:masterfrom
AnvitDevadiga:fix/balanced-weight-all-positive-labels
Open

AnvitDevadiga wants to merge 3 commits into
sunlabuiuc:masterfrom
AnvitDevadiga:fix/balanced-weight-all-positive-labels

Conversation

@AnvitDevadiga

@AnvitDevadiga AnvitDevadiga commented Dec 6, 2026 •

Copy link
Copy Markdown

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 weight negatives / positives was 0. binary_cross_entropy_with_logits multiplies 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_weight
  • tests/core/test_label_processor.py: test_balanced_warns_on_a_label_without_negatives, next to the existing test for labels without positives
  • tests/core/test_apricotm.py and tests/core/test_split_set_task.py: test_balanced_pos_weight and test_label_vocabulary_covers_every_split_part expected 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:

  • The new test fails on master (weight [0.0, 2.0]) and passes with the fix.
  • pytest on every test file under tests/ that mentions balanced or pos_weight passes. I couldn't run test_xgboost_model locally because xgboost needs libomp. XGBoost computes its own scale_pos_weight and 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.

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
@github-actions

github-actions Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 89.49% (69692 / 77875 lines)

Modified file Coverage Lines
pyhealth/processors/label_processor.py 99.29% 278 / 280

Workflow run · f81f74c

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Balanced multilabel loss silently drops labels present in every training sample

1 participant