Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AlphaDropout implementation and add tests #1781

Merged
merged 3 commits into from
Nov 26, 2021
Merged

Conversation

ToucheSir
Copy link
Member

@ToucheSir ToucheSir commented Nov 24, 2021

AFAICT, the original implementation never behaved as expected even pre-Zygote. This was likely not caught because the original PR didn't come with tests, so this PR should remedy that.

Behaviour and outputs are adapted from the PyTorch and TF implementations. Some points of note:

  1. We have to special-case p = 0 to avoid propagating NaNs when calcuating A and B.
  2. Likewise for p = 1. TF just returns the input, but I think the PyTorch approach of returning all zeros (+/- depending on the input sign) is more in line with Dropout.
  3. ifelse is used instead of something like https://github.com/keras-team/keras/blob/v2.7.0/keras/layers/noise.py#L200. I think it better reflects the conditional nature of the operation and it was also faster in local benchmarking.

PR Checklist

  • Tests are added
  • Entry in NEWS.md

@ToucheSir
Copy link
Member Author

Unexpected bonus: this is now 100% GPU compatible and leaves us with one less broken test :)

@CarloLucibello
Copy link
Member

looks good!

CarloLucibello
CarloLucibello previously approved these changes Nov 24, 2021
@darsnack
Copy link
Member

Looks good, I think this deserves an entry in NEWS.md. Especially the GPU bit.

@CarloLucibello
Copy link
Member

bors r+

return x
p = a.p
iszero(p) && return x
isone(p) && return sign.(x) .* T(0)
Copy link
Member

Choose a reason for hiding this comment

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

Is this correct? Are we intentionally creating -0.0 values here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, see point 2 in #1781 (comment).

iszero(p) && return x
isone(p) && return sign.(x) .* T(0)

α′ = T(-1.7580993408473766) # selu(-Inf) == -λα
Copy link
Member

Choose a reason for hiding this comment

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

We can use oftype here

Copy link
Member Author

@ToucheSir ToucheSir Nov 26, 2021

Choose a reason for hiding this comment

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

oftype requires a value of type T, but the only way to get that in this function would be to extract an element of x (which would fail if x is empty and might also trigger the getindex adjoint unnecessarily). Since this function should be specializing on eltype anyhow, it makes sense to take advantage of that for casting.

@bors
Copy link
Contributor

bors bot commented Nov 26, 2021

Build succeeded:

@bors bors bot merged commit 4e28377 into master Nov 26, 2021
@DhairyaLGandhi DhairyaLGandhi deleted the bc/alpha-dropout-tests branch November 27, 2021 16:42
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.

4 participants