Stacking layers example Flux - Flux.params empty


I used the following example of the Flux documentation and when I execute the code the parameters are empty.

using Flux

struct Affine

Affine(in::Integer, out::Integer) =
  Affine(randn(out, in), randn(out))

# Overload call, so the object can be used as a function
(m::Affine)(x) = m.W * x .+ m.b

a = Affine(10, 5)

a(rand(10)) # => 5-element vector

layers = [Dense(10, 5, σ), Dense(5, 2), softmax]

model(x) = foldl((x, m) -> m(x), layers, init = x)

model(rand(10)) # => 2-element vector

ps = Flux.params(model)

And when I use

model2 = Chain(
  Dense(10, 5, σ),
  Dense(5, 2),

not, but I think I need the parameter to call Flux.train!(loss, ps, data, opt). Can somebody tell me, what I´m doing wrong?

Best regards

1 Like

I suppose you are talking about Flux.params. Your model structure needs to implement the Flux.functor function for params to work (Chain implements it).

I don’t know how to do that for model in the example above as it closes over layers, but as a workaround, you should be able to get the parameters by calling params(layers).

If you want params(model) to work then an easy way is to define a struct which has layers as a member and which does the foldl thingy when called as a function. Then you can just put Flux.@functor Model (where Model is your model struct) and Flux will create the function for you.

My intention is to create the models through lists with strings

if layer_types[1] == "dense"
   layers = [Dense(size(data[1], 2), no_neurons[1], list_activations[1])]
else layer_types[1] == "lstm"
   layers = [LSTM(size(data[1], 2), no_neurons[1])]

for i in 2:length(layer_types)
    if layer_types[i] == "dense"
        push!(layers, Dense(no_neurons[i-1], no_neurons[i], list_activations[i]))
    else layer_types[i] == "lstm"
        push!(layers, LSTM(no_neurons[i-1], no_neurons[i]))

model = Chain(layers)

So that i.e. you can train several different models in one execution and so that non-programmers also can set up a training i.e with excel file.