Skip to content

Commit 0edf602

Browse files
rm Flux.Zeros (#1882)
* rm Flux.Zeros, take N+1 * human-readable loadparams tests, same results * fixup * make the words match the code * upgrade to test Chain, more errors, but same on master * Update src/utils.jl Co-authored-by: Carlo Lucibello <[email protected]> * Update src/layers/basic.jl Co-authored-by: Carlo Lucibello <[email protected]>
1 parent 0b3e8c5 commit 0edf602

File tree

11 files changed

+66
-157
lines changed

11 files changed

+66
-157
lines changed

src/Flux.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ using CUDA
3737
const use_cuda = Ref{Union{Nothing,Bool}}(nothing)
3838

3939
include("utils.jl")
40-
include("zeros.jl")
4140
include("onehot.jl")
4241
include("functor.jl")
4342

src/deprecations.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ end
2626

2727
@deprecate frequencies(xs) group_counts(xs)
2828

29+
struct Zeros
30+
function Zeros()
31+
Base.depwarn("Flux.Zeros is no more, has ceased to be, is bereft of life, is an ex-boondoggle... please use bias=false instead", :Zeros)
32+
false
33+
end
34+
end
35+
Zeros(args...) = Zeros() # was used both Dense(10, 2, initb = Zeros) and Dense(rand(2,10), Zeros())
36+
2937
# Channel notation: Changed to match Conv, but very softly deprecated!
3038
# Perhaps change to @deprecate for v0.14, but there is no plan to remove these.
3139
Dense(in::Integer, out::Integer, σ = identity; kw...) =

src/layers/basic.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ end
167167
function Base.show(io::IO, l::Dense)
168168
print(io, "Dense(", size(l.weight, 2), " => ", size(l.weight, 1))
169169
l.σ == identity || print(io, ", ", l.σ)
170-
l.bias == Zeros() && print(io, "; bias=false")
170+
l.bias == false && print(io, "; bias=false")
171171
print(io, ")")
172172
end
173173

@@ -301,7 +301,7 @@ end
301301
Bilinear((in1, in2) => out, σ=identity; bias=true, init=glorot_uniform)
302302
Bilinear(W::AbstractArray, [bias, σ])
303303
304-
Creates a bilinear layer, which operates on two inputs at the same time.
304+
Creates a layer which is fully connected between two inputs and the output, and otherwise similar to [`Dense`](@ref).
305305
Its output, given vectors `x` & `y`, is another vector `z` with,
306306
for all `i ∈ 1:out`:
307307
@@ -394,7 +394,7 @@ function Base.show(io::IO, l::Bilinear)
394394
print(io, "Bilinear((", size(l.weight, 2), ", ", size(l.weight, 3), ") => ", size(l.weight, 1))
395395
end
396396
l.σ == identity || print(io, ", ", l.σ)
397-
l.bias == Flux.Zeros() && print(io, "; bias=false")
397+
l.bias === false && print(io, "; bias=false")
398398
print(io, ")")
399399
end
400400

src/layers/conv.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ _paddims(x::Tuple, y::Tuple) = (x..., y[(end - (length(y) - length(x) - 1)):end]
66
expand(N, i::Tuple) = i
77
expand(N, i::Integer) = ntuple(_ -> i, N)
88

9+
conv_reshape_bias(c) = c.bias isa AbstractVector ?
10+
reshape(c.bias, map(_->1, c.stride)..., :, 1) :
11+
c.bias
12+
913
"""
1014
SamePad()
1115
@@ -61,8 +65,8 @@ Then:
6165
6266
Keywords to control initialization of the layer:
6367
* `init` - Function used to generate initial weights. Defaults to `glorot_uniform`.
64-
* `bias` - Initial bias is zero by default, this can be disabled entirely by setting it to
65-
`false`, or another vector explicitly as `bias = randn(Float32, out)`.
68+
* `bias` - The initial bias vector is all zero by default. Trainable bias can be disabled entirely
69+
by setting this to `false`, or another vector can be provided such as `bias = randn(Float32, out)`.
6670
6771
See also [`ConvTranspose`](@ref), [`DepthwiseConv`](@ref), [`CrossCor`](@ref).
6872
@@ -159,10 +163,9 @@ end
159163
@functor Conv
160164

161165
function (c::Conv)(x::AbstractArray)
162-
b = reshape(c.bias, map(_->1, c.stride)..., :, 1)
163166
σ = NNlib.fast_act(c.σ, x)
164167
cdims = DenseConvDims(x, c.weight; stride = c.stride, padding = c.pad, dilation = c.dilation, groups = c.groups)
165-
σ.(conv(x, c.weight, cdims) .+ b)
168+
σ.(conv(x, c.weight, cdims) .+ conv_reshape_bias(c))
166169
end
167170

168171
_channels_in(l ::Conv) = size(l.weight, ndims(l.weight)-1) * l.groups
@@ -183,7 +186,7 @@ function _print_conv_opt(io::IO, l)
183186
if hasproperty(l, :groups)
184187
(l.groups == 1) || print(io, ", groups=", l.groups)
185188
end
186-
(l.bias isa Zeros) && print(io, ", bias=false")
189+
(l.bias === false) && print(io, ", bias=false")
187190
end
188191

189192
"""
@@ -276,10 +279,9 @@ end
276279
ChainRulesCore.@non_differentiable conv_transpose_dims(::Any, ::Any)
277280

278281
function (c::ConvTranspose)(x::AbstractArray)
279-
b = reshape(c.bias, map(_->1, c.stride)..., :, 1)
280282
σ = NNlib.fast_act(c.σ, x)
281283
cdims = conv_transpose_dims(c, x)
282-
σ.(∇conv_data(x, c.weight, cdims) .+ b)
284+
σ.(∇conv_data(x, c.weight, cdims) .+ conv_reshape_bias(c))
283285
end
284286

285287
function Base.show(io::IO, l::ConvTranspose)
@@ -371,10 +373,9 @@ depthwiseconvfilter(filter::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer};
371373
init = glorot_uniform) where N = init(filter..., div(ch[2], ch[1]), ch[1])
372374

373375
function (c::DepthwiseConv)(x)
374-
b = reshape(c.bias, map(_->1, c.stride)..., :, 1)
375376
σ = NNlib.fast_act(c.σ, x)
376377
cdims = DepthwiseConvDims(x, c.weight; stride=c.stride, padding=c.pad, dilation=c.dilation)
377-
σ.(depthwiseconv(x, c.weight, cdims) .+ b)
378+
σ.(depthwiseconv(x, c.weight, cdims) .+ conv_reshape_bias(c))
378379
end
379380

380381
function Base.show(io::IO, l::DepthwiseConv)
@@ -452,10 +453,9 @@ function crosscor(x, w, ddims::DenseConvDims)
452453
end
453454

454455
function (c::CrossCor)(x::AbstractArray)
455-
b = reshape(c.bias, map(_->1, c.stride)..., :, 1)
456456
σ = NNlib.fast_act(c.σ, x)
457457
cdims = DenseConvDims(x, c.weight; stride=c.stride, padding=c.pad, dilation=c.dilation)
458-
σ.(crosscor(x, c.weight, cdims) .+ b)
458+
σ.(crosscor(x, c.weight, cdims) .+ conv_reshape_bias(c))
459459
end
460460

461461
function Base.show(io::IO, l::CrossCor)

src/utils.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,17 +441,18 @@ rand32(dims...) = Base.rand(Float32, dims...)
441441
randn32(dims...) = Base.randn(Float32, dims...)
442442

443443
"""
444-
create_bias(weights, bias, length)
444+
create_bias(weights, bias, size...)
445445
446446
Return a bias parameter for a layer, based on the value given
447447
to the constructor's keyword `bias=bias`.
448448
449-
* `bias == true` creates a zero vector, of the same type as weights.
450-
* `bias == false` returns `Zeros()`, a special struct which exists only to encode the absence of bias.
451-
* `bias::AbstractArray` uses the array provided, provided it has the correct size and eltype. If the type is wrong, it will be converted.
449+
* `bias == true` creates a trainable array of the given size, of the same type as `weights`, initialised to zero.
450+
* `bias == false` returns `false`, which is understood by AD to be non-differentiable.
451+
* `bias::AbstractArray` uses the array provided, provided it has the correct size.
452+
It does not at present correct the `eltype` to match that of `weights`.
452453
"""
453454
function create_bias(weights::AbstractArray, bias::Bool, dims::Integer...)
454-
bias ? fill!(similar(weights, dims...), 0) : Zeros()
455+
bias ? fill!(similar(weights, dims...), 0) : false
455456
end
456457
function create_bias(weights::AbstractArray, bias::AbstractArray, dims::Integer...)
457458
size(bias) == dims || throw(DimensionMismatch("expected bias of size $(dims), got size $(size(bias))"))

src/zeros.jl

Lines changed: 0 additions & 52 deletions
This file was deleted.

test/cuda/layers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ end
155155
end
156156
end
157157

158-
@testset "Dense with Zeros bias" begin
159-
l = Dense(ones(Float32, 4, 3), Flux.Zeros()) |> gpu
158+
@testset "Dense without bias" begin
159+
l = Dense(ones(Float32, 4, 3), false) |> gpu
160160
ip = zeros(Float32, 3, 7) |> gpu
161161

162162
@test sum(l(ip)) 0.f0

test/layers/basic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ import Flux: activations
175175
@test b1.σ == identity
176176

177177
b2 = Flux.Bilinear(randn(3,4,5), false)
178-
@test b2.bias == Flux.Zeros()
178+
@test b2.bias === false
179179

180180
b3 = Flux.Bilinear(randn(Float16, 3,4,5), true, tanh)
181181
@test b3.σ == tanh

test/layers/conv.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ end
273273

274274
@testset "constructors: $fun" for fun in [Conv, CrossCor, ConvTranspose, DepthwiseConv]
275275
@test fun(rand(2,3,4)).bias isa Vector{Float64}
276-
@test fun(rand(2,3,4,5), false).bias isa Flux.Zeros
276+
@test fun(rand(2,3,4,5), false).bias === false
277277
if fun == Conv
278278
@test fun(rand(2,3,4,5,6), rand(6)).bias isa Vector{Float64}
279279
@test_skip fun(rand(2,3,4,5,6), 1:6).bias isa Vector{Float64}

test/optimise.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using Random
1515
Nesterov(), RMSProp(), Momentum()]
1616
Random.seed!(42)
1717
w′ = randn(10, 10)
18-
b = Flux.Zeros()
18+
b = false
1919
loss(x) = Flux.Losses.mse(w*x, w′*x .+ b)
2020
for t = 1: 10^5
2121
θ = params([w′, b])

0 commit comments

Comments
 (0)