Skip to content

Fix double exp() of logit_scale in create_probabilities#197

Closed
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-double-exp-logit-scale
Closed

Fix double exp() of logit_scale in create_probabilities#197
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-double-exp-logit-scale

Conversation

Copilot AI commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

BaseClassifier.create_probabilities() was calling self.model.logit_scale.exp(), but the BioCLIP pretrained model stores logit_scale already as the scale (not its log), so this applied exp() twice — producing inflated logits and over-confident (potentially overflowing) probabilities.

Changes

  • src/bioclip/predict.py: Remove .exp() from logit_scale in create_probabilities():

    # Before
    logits = (self.model.logit_scale.exp() * img_features @ txt_features)
    # After
    logits = (self.model.logit_scale * img_features @ txt_features)

    Mirrors the identical fix in Imageomics/bioclip#39.

  • tests/test_predict.py: Add TestCreateProbabilities with two unit tests — one asserting the corrected direct-scale behavior, and one confirming the double-exp() path produces a meaningfully different (wrong) result.

Copilot AI linked an issue Jun 1, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix double exp() of logit_scale Fix double exp() of logit_scale in create_probabilities Jun 1, 2026
Copilot finished work on behalf of hlapp June 1, 2026 10:21
Copilot AI requested a review from hlapp June 1, 2026 10:21
@hlapp

hlapp commented Jun 1, 2026

Copy link
Copy Markdown
Member

@work4cs your change seems to lead to complete mis-prediction here (the test failure is in classifying the cat as a plant rather than a cat, not in the newly introduced unit test). Any idea why this is?

@work4cs

work4cs commented Jun 17, 2026

Copy link
Copy Markdown

@work4cs your change seems to lead to complete mis-prediction here (the test failure is in classifying the cat as a plant rather than a cat, not in the newly introduced unit test). Any idea why this is?

Jianyang mentioned that exp() only happens in forward function. And I noticed that self.model.logit_scale depends on if self.model.logit_scale has been updated to 'logit_scale.exp()`. It is actually not updated. So, actually, the original code is correct. I am trying to recover it.

And theoretically it should not make the ranking different, but numerically, it scales the image features before the matrix multiplication because model.logit_scale.exp() * image_features @ classifier is calculated from left to right, as (model.logit_scale.exp() * image_features) @ classifier. Then, it will change rounding behavior. If class scores are very close, this can change the ranking.

In conclusion, we should not change the code and the original is actually correct. I will revert my update.

@hlapp

hlapp commented Jun 18, 2026

Copy link
Copy Markdown
Member

Closing as per @work4cs's re-investigation.

@hlapp hlapp closed this Jun 18, 2026
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.

Fix double exp() of logit_scale

3 participants