# What is a model in Julia Flux 0.13 and higher?

**URL:** <https://discourse.julialang.org/t/what-is-a-model-in-julia-flux-0-13-and-higher/100653>\
**Category:** General Usage\
**Tags:** flux\
**Created:** [June 21, 2023, 1:40pm UTC](https://discourse.julialang.org/t/what-is-a-model-in-julia-flux-0-13-and-higher/100653 "2023-06-21T13:40:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yrogirg](https://avatars.discourse-cdn.com/v4/letter/y/df788c/32.png) [@Yrogirg](https://discourse.julialang.org/u/Yrogirg)\
**Post date:** [June 21, 2023, 1:40pm UTC](https://discourse.julialang.org/t/what-is-a-model-in-julia-flux-0-13-and-higher/100653/1 "2023-06-21T13:40:22Z")

</div>

Cross post from stackoverflow ([flux.jl - What is a model in Julia Flux 0.13 and higher? - Stack Overflow](https://stackoverflow.com/questions/76516316/what-is-a-model-in-julia-flux-0-13-and-higher))

I want to use Julia Flux for machine learning with custom models (not neural networks, so I won’t be using/combining models provided by Flux). I want to do it with Flux because I want access to various advanced gradient decent algorithms.

To do so I intend to use the training API ([Training API · Flux](https://fluxml.ai/Flux.jl/stable/training/reference/#Training-API-Reference))

The problem is that the documentation is not very detailed. For example there are functions like

```julia
Flux.setup(rule, model)

```

and

```julia
Flux.train!(loss, model, data, opt_state)

```

however nowhere in the API there is a description of what the `model` is and what form should it take.

As a test problem, consider matrix factorization. That is, given matrix `A`

```julia
using Random

dim = 2

A = rand(dim, dim)

```

find such `x` and `y` that

```julia
x * y' ≈ A

```

I would guess that the `model` should be defined as `model(x, y) = x * y'`, but then `Flux.setup(AdaGrad(), model)` produces a warning

```julia
Warning: setup found no trainable parameters in this model

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 21, 2023, 1:47pm UTC](https://discourse.julialang.org/t/what-is-a-model-in-julia-flux-0-13-and-higher/100653/2 "2023-06-21T13:47:36Z")

</div>

But there is an example right in the first box on the page you link?

```julia
julia> model = Dense(2=>1, leakyrelu; init=ones);

julia> opt_state = Flux.setup(Momentum(0.1), model)

julia> x1, y1 = [0.2, -0.3], [0.4];

julia> Flux.train!(model, [(x1, y1), (x1, y1)], opt_state) do m, x, y
         sum(abs.(m(x) .- y)) * 100
       end

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [June 21, 2023, 2:52pm UTC](https://discourse.julialang.org/t/what-is-a-model-in-julia-flux-0-13-and-higher/100653/3 "2023-06-21T14:52:12Z")

</div>

If you want to create custom models that don’t use the built-in layers there is some information about that in the documentation as well.  
[https://fluxml.ai/Flux.jl/stable/models/advanced/#Custom-Model-Example](https://fluxml.ai/Flux.jl/stable/models/advanced/#Custom-Model-Example)
