# Regression in Flux.jl

**URL:** https://discourse.julialang.org/t/regression-in-flux-jl/38733
**Category:** Machine Learning
**Tags:** question, flux
**Created:** [May 4, 2020, 11:18am UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733 "2020-05-04T11:18:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 4, 2020, 11:18am UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733/1 "2020-05-04T11:18:44Z")

</div>

Trying to get started with `Flux.jl`. I’m trying estimate a linear regression. I have the following:

```julia
using Flux
using Random

Random.seed!(1234)

β = [10., 20., 30.] # true parameters

predict(x) = (x*β)

function loss(x, y)
    ŷ = predict(x)
    sum((ŷ .- y).^2)
end

x = hcat(rand(500,2), ones(500,1)) # create data

y = x*β .+ 10*rand() # population equation

θ = Flux.Params([β])
gs = Flux.gradient( () -> loss(x, y), θ )
opt = Flux.Optimise.Descent()
Flux.train!(loss, θ, [(x, y)], opt)

β |> print

```

However I do not get back the OLS estimates `(x'*x)^(-1)*(x'*y)`. Could there be an issue with the optimizer? I didn’t see any options for setting tolerance in the documentation.

---

<div class="post-metadata">

### Author: ![jondeuce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondeuce/32/16378_2.png) [@jondeuce](https://discourse.julialang.org/u/jondeuce)
#### Post date: [May 4, 2020, 4:25pm UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733/2 "2020-05-04T16:25:15Z")

</div>

Note that `Flux.train!` only trains for one “epoch”. In this case, that means one iteration of gradient descent with the default step size. [The training section of the documentation](https://fluxml.ai/Flux.jl/stable/training/training/#Datasets-1) elaborates on how to train your model for many “epochs”.

In general you will have to implement your own convergence criteria, though in this simple case training for a few thousand epochs with the default settings will likely be sufficient.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 4, 2020, 4:35pm UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733/3 "2020-05-04T16:35:21Z")

</div>

Yep, that was it. Coming from another discipline, I thought “training” meant estimation. It’s very strange to me to have the training stop after one iteration…

---

<div class="post-metadata">

### Author: ![jondeuce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondeuce/32/16378_2.png) [@jondeuce](https://discourse.julialang.org/u/jondeuce)
#### Post date: [May 4, 2020, 4:43pm UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733/4 "2020-05-04T16:43:24Z")

</div>

Yeah, it is somewhat confusing nomenclature. In general, it’s something of a general design philosophy of Flux to provide basic tools and assume very little about what you want to do with them. If you look at [the source code of Flux.train!](https://github.com/FluxML/Flux.jl/blob/7a32a703f0f2842dda73d4454aff5990ade365d5/src/optimise/train.jl#L59-L77), for example, besides the error checking it’s really just a few lines of code computing the loss function gradient and updating the parameters.

---

<div class="post-metadata">

### Author: ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)
#### Post date: [May 4, 2020, 5:01pm UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733/5 "2020-05-04T17:01:09Z")

</div>

I do something similar here:

> [@Generic Function to train NN w/ Flux](https://discourse.julialang.org/t/generic-function-to-train-nn-w-flux/37208):
>
> Updated: I would like to write a generic function to train a model in Flux. An ordinary linear regression w/ intercept is a special case of a neural network. If the optimizers work well I should get the same result (esp from such a simple model). Unfortunately I don’t. First: prepare Boston housing data using MLJ: @load\_boston, partition using Flux, Statistics X, y = @load\_boston; X = hcat(X...); train, test = partition(eachindex(y), .7, rng=333); # Xm = hcat(X, size(X,1) |\> ones ); #add …

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [May 4, 2020, 5:09pm UTC](https://discourse.julialang.org/t/regression-in-flux-jl/38733/6 "2020-05-04T17:09:44Z")

</div>

that’s very useful, thank you!
