# Stacking layers example Flux - Flux.params empty

**URL:** <https://discourse.julialang.org/t/stacking-layers-example-flux-flux-params-empty/32186>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 12, 2019, 10:20am UTC](https://discourse.julialang.org/t/stacking-layers-example-flux-flux-params-empty/32186 "2019-12-12T10:20:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [December 12, 2019, 10:20am UTC](https://discourse.julialang.org/t/stacking-layers-example-flux-flux-params-empty/32186/1 "2019-12-12T10:20:13Z")

</div>

Hi,

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

```julia
using Flux

struct Affine
  W
  b
end

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

```julia
model2 = Chain(
  Dense(10, 5, σ),
  Dense(5, 2),
  softmax)

```

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

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [December 12, 2019, 11:44am UTC](https://discourse.julialang.org/t/stacking-layers-example-flux-flux-params-empty/32186/2 "2019-12-12T11:44:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [December 12, 2019, 12:26pm UTC](https://discourse.julialang.org/t/stacking-layers-example-flux-flux-params-empty/32186/3 "2019-12-12T12:26:45Z")

</div>

My intention is to create the models through lists with strings

```julia
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])]
end

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]))
    end
end

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.
