# Trying to train with Flux.jl

**URL:** <https://discourse.julialang.org/t/trying-to-train-with-flux-jl/51786>\
**Category:** New to Julia\
**Tags:** question, flux, machine-learning\
**Created:** [December 14, 2020, 3:03am UTC](https://discourse.julialang.org/t/trying-to-train-with-flux-jl/51786 "2020-12-14T03:03:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 14, 2020, 3:03am UTC](https://discourse.julialang.org/t/trying-to-train-with-flux-jl/51786/1 "2020-12-14T03:03:59Z")

</div>

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/a/fa678fd585f6aa5a5a916208128d0652467a008b.png)  
And am just trying to put it through a couple dense layers with a custom activation in between:

```julia
model=Chain(Dense(6,1),
	x->(0.5σ.(x).+x),
	Dense(1,2,rrelu),
	softmax)

```

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](https://www.math.purdue.edu/~allen450/ML-with-Julia-Tutorial.html) (apologies for letting desperation get the best of me)

```julia
Flux.train!(Flux.mse,params(model),data,ADAM())

```

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

```julia
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 ?  
Cheers

---

<div class="post-metadata">

**Author:** ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)\
**Post date:** [December 14, 2020, 6:56am UTC](https://discourse.julialang.org/t/trying-to-train-with-flux-jl/51786/2 "2020-12-14T06:56:32Z")

</div>

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

```julia
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 😉

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 14, 2020, 7:55am UTC](https://discourse.julialang.org/t/trying-to-train-with-flux-jl/51786/3 "2020-12-14T07:55:50Z")

</div>

Now there’s another error:

```julia
UndefVarError: loss not defined

macro expansion@show.jl:641[inlined]
(::Main.workspace677.var"#1#2")()@Other: 3
throttled#38@utils.jl:439[inlined]
throttled@utils.jl:435[inlined]
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

```julia
loss(x,y)=sum(x.-y)^2

```

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 😄

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.

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [December 14, 2020, 10:09am UTC](https://discourse.julialang.org/t/trying-to-train-with-flux-jl/51786/4 "2020-12-14T10:09:26Z")

</div>

> [@Ayush\_Rawat](#):
>
> each example is a length 6 array of `Float64` .

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.
