Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix precision issue with expansion that prefers 'probs' over 'logits'#18614

Closed
ahmadsalim wants to merge 3 commits into
pytorch:masterfrom
ahmadsalim:fix/logitsoverprobs
Closed

Fix precision issue with expansion that prefers 'probs' over 'logits'#18614
ahmadsalim wants to merge 3 commits into
pytorch:masterfrom
ahmadsalim:fix/logitsoverprobs

Conversation

@ahmadsalim
Copy link
Copy Markdown
Contributor

I have experienced that sometimes both were in __dict__, but it chose to copy probs which loses precision over logits. This is especially important when training (bayesian) neural networks or doing other type of optimization, since the loss is heavily affected.

@ezyang ezyang requested a review from neerajprad April 1, 2019 22:06
Comment thread torch/distributions/bernoulli.py Outdated
if 'logits' in self.__dict__:
new.logits = self.logits.expand(batch_shape)
new._param = new.logits
else:
Copy link
Copy Markdown
Contributor

@neerajprad neerajprad Apr 1, 2019

Choose a reason for hiding this comment

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

If both the parametrizations have already been computed, we should copy both of them, rather than having to recompute them again later, so I would suggest changing this to:

if 'probs' in self.__dict__:
  ... # expand and set
if 'logits' in self.__dict__:
 ... # expand and set

That will also address your original issue since it will use the initially specified logits for any log pdf computation, in addition to caching the logit to prob or prob to logit computation if its already done.

Copy link
Copy Markdown
Contributor

@neerajprad neerajprad left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Could you also change this for the remaining distributions - binomial, multinomial, relaxed_bernoulli, negative_binomial, geometric?

@ahmadsalim
Copy link
Copy Markdown
Contributor Author

Thanks for the comments! I will do the adjustments as you suggested for all the distributions 😄

@neerajprad
Copy link
Copy Markdown
Contributor

LGTM! The lint check fails are not from this PR.

@soumith
Copy link
Copy Markdown
Collaborator

soumith commented Apr 2, 2019

the CI is broken today, but I am putting it in the land queue.

Copy link
Copy Markdown
Collaborator

@soumith soumith left a comment

Choose a reason for hiding this comment

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

@pytorchbot merge this please

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@ezyang is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@ezyang merged this pull request in 8e1e291.

zhangguanheng66 pushed a commit to zhangguanheng66/pytorch that referenced this pull request May 6, 2019
…pytorch#18614)

Summary:
I have experienced that sometimes both were in `__dict__`, but it chose to copy `probs` which loses precision over `logits`. This is especially important when training (bayesian) neural networks or doing other type of optimization, since the loss is heavily affected.
Pull Request resolved: pytorch#18614

Differential Revision: D14793486

Pulled By: ezyang

fbshipit-source-id: d4ff5e34fbb4021ea9de9f58af09a7de00d80a63
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…pytorch#18614)

Summary:
I have experienced that sometimes both were in `__dict__`, but it chose to copy `probs` which loses precision over `logits`. This is especially important when training (bayesian) neural networks or doing other type of optimization, since the loss is heavily affected.
Pull Request resolved: pytorch#18614

Differential Revision: D14793486

Pulled By: ezyang

fbshipit-source-id: d4ff5e34fbb4021ea9de9f58af09a7de00d80a63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants