Skip to content

Conversation

@fehiepsi
Copy link
Contributor

@fehiepsi fehiepsi commented May 18, 2019

This PR fixes #17843. In addition (test locally), this still maintains the continuity of log_prob which is addressed in #15962

cc @neerajprad

@pytorchbot pytorchbot added the module: distributions Related to torch.distributions label May 18, 2019
@fehiepsi fehiepsi closed this May 18, 2019
@fehiepsi
Copy link
Contributor Author

Although this is stable, the implementation will give incorrect grad when logits=0 (only at this single point) due to torch.abs and torch.clamp operators. I'm not sure how to resolve it. We might reconsider the version pre-#15962.

@fehiepsi fehiepsi reopened this May 18, 2019
@fehiepsi
Copy link
Contributor Author

It turns out that the issue lies at torch.clamp which returns grad at 0 is 1. If it returns 0.5, grad will be correct.

Related #2195 where the same issue happens for binary cross entropy with logits. If we make grad of torch.clamp at 0 is 0.5, the result for grad at 0 of binomal logprob and binary cross entropy with logits (pre #2195) will be correct. @apaszke What do you think?

@fehiepsi
Copy link
Contributor Author

Nevermind, we have x = x.clamp(min=0) + x.clamp(max=0), the LHS has grad at 0 is 1 while RHS is 2. We can utilize this difference to add a dummy variable (x - x.clamp(min=0) - x.clamp(max=0)) / 2 to x.clamp(min=0) to adjust its grad.

@fehiepsi
Copy link
Contributor Author

This is ready to review. I think that I have addressed problems which I can think about:

  • overflow of log_prob
  • discontinuity of grad of log_prob
  • correct grad at logits=0

Copy link
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.

Looks great!

@neerajprad
Copy link
Contributor

@pytorchbot merge this please

@pytorchbot pytorchbot added the merge-this-please Was marked for merge with @pytorchbot merge this please label May 20, 2019
Copy link
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
Contributor

@ezyang merged this pull request in 0719714.

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

Labels

merge-this-please Was marked for merge with @pytorchbot merge this please Merged module: distributions Related to torch.distributions open source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binomial.log_prob returns -inf when actual probability is 1 if logit is large

6 participants