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

Ignore Zeros when gathering parameters #1281

Closed
wants to merge 2 commits into from
Closed
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: 3 additions & 1 deletion src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Possible values include:
"""
trainmode!(m, mode = true) = mode isa Bool ? testmode!(m, !mode) : testmode!(m, mode)

params!(p::Flux.Params, x::Zeros{<:Number}, seen = Flux.IdSet()) = nothing

params!(p::Params, x::AbstractArray{<:Number}, seen = IdSet()) = push!(p, x)

function params!(p::Params, x, seen = IdSet())
Expand Down Expand Up @@ -84,4 +86,4 @@ f64(m) = paramtype(Float64, m)

# Functors for certain Julia data structures
@functor Cholesky
trainable(c::Cholesky) = ()
trainable(c::Cholesky) = ()
11 changes: 10 additions & 1 deletion test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,19 @@ end
@test sum(op) == prod(size(op))

bias = Conv((2,2), 1=>3, bias = Flux.Zeros())

# Test that disabled bias is not in parameters
# and gathered parameters can be loaded back
bias_parameters = bias |> Flux.params
@test length(bias_parameters) == 1
@test Flux.Zeros() ∉ bias_parameters
Flux.loadparams!(bias, bias_parameters)

# Test that disabled bias does not appear in gradients
op = bias(ip)
@test sum(op) ≈ 0.f0
gs = gradient(() -> sum(bias(ip)), Flux.params(bias))
@test gs[bias.bias] == nothing
@test !haskey(gs, bias.bias)

# Train w/o bias and make sure no convergence happens
# when only bias can be converged
Expand Down