Trying to train with Flux.jl

Taking my first steps with Flux.jl, I’m trying to train a simple model.

I’ve got my training data in an array data, containing Float64 arrays with 6 entries each:

And am just trying to put it through a couple dense layers with a custom activation in between:


Which works well enough, as checking it on each example by model(data[1]) works as expected. So, I try training with some code I edited off here (apologies for letting desperation get the best of me)


I’ll refrain from mentioning all the traces I’ve run through while trying to get this to work. But the most recent one is

MethodError: no method matching mse(::Array{Float64,1})
Closest candidates are:
mse(::Any, !Matched::Any; agg) at C:\Users\icebear\.julia\packages\Flux\q3zeA\src\losses\functions.jl:17

Having reached here after a long day(and night) of errors, I’m understanding that my method of feeding data to train! is incorrect.

Is that doubt well-founded ?

Without any further knowledge about your problem or the data, I assume that you need to add

Flux.mse(x::AbstractArray) = Flux.mse(x, zeros(eltype(x), size(x)...)

to make it work. Flux.mse only has a method defined on two arguments, so either you have to provide you data as data = [(x1,y1), (x2, y2),...] or add a dispatch on a single argument :wink:

Now there’s another error:

UndefVarError: loss not defined

macro expansion@show.jl:641[inlined]
(::Main.workspace677.var"#1#2")()@Other: 3
macro expansion@train.jl:106[inlined]
macro expansion@progress.jl:134[inlined]
#train!#12(::Flux.var"#throttled#42"{Flux.var"#throttled#38#43"{Bool,Bool,Main.workspace677.var"#1#2",Int64}}, ::typeof(Flux.Optimise.train!), ::Function, ::Zygote.Params, ::Array{Array{Float64,1},1}, ::Flux.Optimise.ADAM)@train.jl:100
top-level scope@Local: 1

Funnily enough, I get the above error even if I change my loss function to be


even though I know this one works, as calling loss(model(data[1]),target[1]) works properly.

P.S. @Julius_Martensen good seeing you back here, your last post was 4 months ago :smile:

Edit: What would help you regarding the data ? each example is a length 6 array of Float64. And the problem is really just a toy one. I just want to understand how to use train! for now, once that’s done, then I’ll actually start working on my problem.

that is precisely the problem, making data into a matrix by doing hcat(data...) made train! work nice n good.
Moral of the story: Keep your data like sane people do.

1 Like