I used the following example of the Flux documentation and when I execute the code the parameters are empty.
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, σ),
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?
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
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 == "dense"
layers = [Dense(size(data, 2), no_neurons, list_activations)]
else layer_types == "lstm"
layers = [LSTM(size(data, 2), no_neurons)]
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.