Flux:the trainable parameter is empty

Hi everyone!I am trying to create a custom layer, and customize the parameter collection to train, the following is my code

using Flux

struct Linear{F,S<:AbstractArray,T<:AbstractArray}
    W::S
    b::T
    σ::F
end

function Linear(in::Integer, out::Integer, σ = identity;
                initW = Flux.glorot_uniform, initb = zeros)
    return Linear(initW(out, in), initb(out), σ)
end

function (a::Linear)(x::AbstractArray)
    W, b, σ = a.W, a.b, a.σ
    σ.(W*x .+ b)
end
Flux.@functor Linear (W,)
# Flux.trainable(m::Linear) = (m.W)

m = Linear(2,1)
ps = Flux.params(m) # always get Params([])

But the ps is always empty, Where is wrong?

2 Likes

Running exactly your code above:

julia> Flux.params(m)
Params([Float32[1.1857293 -0.41868022]])

Can you replicate this on a fresh Julia session? If so, what versions of Flux, Zygote, Julia, etc. are you running?

1 Like

Yes, I changed the environment then it does work.
Thanks!