Rewrite sigmoid gradient into numerically stable form#2041
Open
ricardoV94 wants to merge 1 commit intopymc-devs:v3from
Open
Rewrite sigmoid gradient into numerically stable form#2041ricardoV94 wants to merge 1 commit intopymc-devs:v3from
ricardoV94 wants to merge 1 commit intopymc-devs:v3from
Conversation
Enable allow_multiple_clients on the 1-sigmoid(x)->sigmoid(-x) rewrite so it fires even when sigmoid(x) has other consumers. This stabilizes expressions like sigmoid(x) * (1 - sigmoid(x)) which suffer catastrophic cancellation for large |x|. The sigmoid pullback is kept in naive form to preserve algebraic cancellation in composed expressions like log(1 - sigmoid(x)).
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.
Replace sigmoid(x) * (1 - sigmoid(x)) with sigmoid(x) * sigmoid(-x) in the Sigmoid pullback. The naive form suffers catastrophic cancellation for large |x| because (1 - expit(x)) rounds to zero.Instead of doing that (which we may want to). I left as is but let the stabilize rewrite be more aggressive and rewrite 1-sigmoid(x) -> sigmoid(-x), even if sigmoid(x) is used elsewhere. (Users who don't care about this can exclude "stabilize" then)
This may be too much tip-toeing. Maybe we want the rewrite to always be eager (and in this case implement the pullback already in this format). There was one test that checked whether the grad of a naive
log(1 - sigmoid(x))simplified (to not have a sum), and that one ended up cancelling a sigmoid(x) / sigmoid(x), that an eager stable pullback didn't produce. (rewrite ordering is fun).I don't know if sigmoids are expensive enough to worry about duplicating use in the first place.