Skip to content

Mark dropout_mask as non-differentiable #1870

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

Merged
merged 2 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
## v0.13
* After a deprecations cycle, the datasets in `Flux.Data` have
been removed in favour of MLDatasets.jl.
* `params` is not exported anymore since it is a common name and is also exported by Distributions.jl
* `params` is not exported anymore since it is a common name and is also exported by Distributions.jl
* `flatten` is not exported anymore due to clash with Iterators.flatten.
* Remove Juno.jl progress bar support as it is now obsolete.
* Improved compatibility of Dropout with Int and Complex types.
* `Dropout` gained improved compatibility with Int and Complex arrays and is now twice-differentiable.

## v0.12.10
* `Dropout`/`AlphaDropout` now supports [user-specified RNGs](https://github.com/FluxML/Flux.jl/pull/1838)
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ArrayInterface = "3.1, 4"
CUDA = "3"
Functors = "0.2.1"
MacroTools = "0.5"
NNlib = "0.8"
NNlib = "0.8.2"
NNlibCUDA = "0.2"
ProgressLogging = "0.1"
Reexport = "0.2, 1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/layers/normalise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function _dropout_mask(rng, x, p; dims=:)
return y
end

# TODO move this to NNlib
Zygote.ChainRulesCore.@non_differentiable dropout_mask(rng, x, p)

"""
Dropout(p; dims=:, rng = rng_from_array())

Expand Down
6 changes: 3 additions & 3 deletions test/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ import Flux: activations
Parallel(f_cnt, sin)(1)
@test CNT[] == 3
end

# Ref https://github.com/FluxML/Flux.jl/issues/1673
@testset "Input domain" begin
struct Input
Expand Down Expand Up @@ -278,7 +278,7 @@ import Flux: activations
vocab_size, embed_size = 10, 4
m = Flux.Embedding(vocab_size, embed_size)
@test size(m.weight) == (embed_size, vocab_size)

x = rand(1:vocab_size, 3)
y = m(x)
@test y isa Matrix{Float32}
Expand Down Expand Up @@ -315,7 +315,7 @@ end
# https://github.com/FluxML/NNlib.jl/issues/362
m3 = Chain(Conv((3,), 2 => 3, relu), Dense(2,2))
x3 = cat(Float32[1 2; 3 4; 5 6; 7 8]; dims=3)
@test_broken Zygote.hessian_dual(sum∘m3, x3) ≈ Zygote.hessian_reverse(sum∘m3, x3)
@test Zygote.hessian_dual(sum∘m3, x3) ≈ Zygote.hessian_reverse(sum∘m3, x3)
end

@testset "gradients of Chain{Vector}" begin
Expand Down
9 changes: 7 additions & 2 deletions test/layers/normalisation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ end
x = reshape(collect(1:prod(sizes)), sizes)

@test Flux.hasaffine(m) == true
@test length(Flux.params(m)) == 2
@test length(Flux.params(m)) == 2
x = Float64.(x)
y = m(x)
μ = mean(x, dims=1)
Expand All @@ -287,7 +287,7 @@ end
x = reshape(collect(1:prod(sizes)), sizes)
@test Flux.hasaffine(m) == false
@test length(Flux.params(m)) == 0

x = Float64.(x)
y = m(x)
μ = mean(x, dims=1)
Expand Down Expand Up @@ -458,3 +458,8 @@ end
@test BN(x) ≈ GN(x)
end
end

@testset "second derivatives" begin
m1 = Dropout(0.5)
@test Zygote.hessian_reverse(sum∘m1, [1.0,2.0,3.0]) == zeros(3, 3)
end