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

Added test for Alpha Dropout #2367

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
44 changes: 44 additions & 0 deletions test/dropout.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

using Statistics
using Flux
using Test

#initial x value
# x = randn32(1000,1);
# x = [1,2,3,4,5]

# Mean
# E(xd + alpha(1-d)) = qu + (1-q)alpha
a_ = -1.7580993408473766
d = 0.2
q = 0.2
u = mean(x)

function mean_test(x)
# LHS
mean_left = (x*d) .+ (a_*(1-d))
mean_left = mean(mean_left)
# println(mean_left)

# RHS
mean_right = (q*u) .+ ((1-q)*a_)
# println(mean_right)
@test isapprox(mean_left, mean_right, atol=0.2)
end

x = randn(2000,1);
@testset "Alphadropout Tests" begin
mean_test(x);
end


# Variance
# Var(xd + alpha(1-d)) = q((1-q)(alpha-u)^2 + v)
# v = var(x)

# var_left = (x*d) .+ a_*(1-d)
# var_left = var(var_left)

# var_right = q*((1-q)*(a_-u).^2 + v)

# @test isapprox(var_left, var_right, atol=0.1)
9 changes: 8 additions & 1 deletion test/layers/normalisation.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Flux, Test, Statistics, Random
using Zygote: pullback, ForwardDiff

evalwgrad(f, x...) = pullback(f, x...)[1]
global evalwgrad(f, x...) = pullback(f, x...)[1]
darsnack marked this conversation as resolved.
Show resolved Hide resolved

@testset "Dropout" begin
@testset for rng_kwargs in ((), (; rng = MersenneTwister()))
Expand Down Expand Up @@ -88,12 +88,19 @@ end

x = randn(1000) # large enough to prevent flaky test
m = AlphaDropout(0.5; rng_kwargs...)
q = 0.5
u = mean(x)
α′ = -1.7580993408473766

y = evalwgrad(m, x)
# Should preserve unit mean and variance
@test mean(y) ≈ 0 atol=0.2
@test var(y) ≈ 1 atol=0.2

# Should check that the mean and variance matches the formula
# E(xd + α′(1-d)) = qu + (1-q)α′
@test mean(y) ≈ (q*u) + ((1-q)*α′)

testmode!(m, true) # should override istraining
@test evalwgrad(m, x) == x

Expand Down
Loading