# Multiple linear regression model using Flux.jl

**URL:** <https://discourse.julialang.org/t/multiple-linear-regression-model-using-flux-jl/90775>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [November 24, 2022, 8:11pm UTC](https://discourse.julialang.org/t/multiple-linear-regression-model-using-flux-jl/90775 "2022-11-24T20:11:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![moataz-sabry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moataz-sabry/32/36856_2.png) [@moataz-sabry](https://discourse.julialang.org/u/moataz-sabry)\
**Post date:** [November 24, 2022, 8:11pm UTC](https://discourse.julialang.org/t/multiple-linear-regression-model-using-flux-jl/90775/1 "2022-11-24T20:11:33Z")

</div>

Hello everyone,

I’m new to machine learning and I was trying to extend the basic example from [Flux](https://fluxml.ai/Flux.jl/stable/models/overview/) documentation to a multiple linear regression model where the input `S` and the output `V` are both vectors with 3 elements each, x, y and z.

As long as the relation between `S` and `V` is identity, I get great predictions using my model. Simply changing the function between `S` and `V` as in the following example so that V\_y = 2S\_y, leads to wrong results.

What could be the cause of this problem?

```julia
## Random Data set
F = 3
D = 1800

S_train, S_test = rand(1:100, F, D), rand(1:100, F, D)

```

```julia
## V = ƒ(S), both V & S are vectors with 3 elements
function ƒ(S::Matrix)
    V = zero(S)
    D = size(S, 1)
    
    D > 1 && for (x, y, z) in Iterators.partition(eachindex(S), D)
        V[x] = S[x]
        V[y] = 2S[y]
        V[z] = S[z]
    end

    isone(D) && (V .= map(actual, S))
    return V
end
V_train, V_test = ƒ(S_train), ƒ(S_test)

```

```julia
# default activation function: identity
using Flux: train!

opt = Descent(1e-7) ## optimizer: classic gradient descent strategy
data = [(V_train, S_train)]
model = Dense(F => F)
parameters = Flux.params(model)
loss(x, y) = Flux.Losses.mse(model(x), y)

for _ in Base.OneTo(100_000)
    train!(loss, parameters, data, opt)
end

```

```julia
model(S_test)
3×1800 Matrix{Float32}:
 79.9905 51.0251 83.9732 1.19865 … 7.04939 99.9106 69.8579 21.1866
 12.4992 36.5154 16.4895 20.1102 41.5276 20.9554 21.4264 43.6019
 80.0067 62.9638 77.0303 81.7296 25.9406 68.1154 14.1989 97.7402

V_test
3×1800 Matrix{Int64}:
 80 51 84 1 40 86 97 77 61 … 82 49 28 75 7 100 70 21
 50 146 66 80 44 42 154 194 4 30 8 62 54 166 84 86 174
 80 63 77 82 3 68 94 35 9 78 77 81 32 26 68 14 98

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [November 24, 2022, 8:57pm UTC](https://discourse.julialang.org/t/multiple-linear-regression-model-using-flux-jl/90775/2 "2022-11-24T20:57:23Z")

</div>

Train calls the loss function as `loss(d...)` if each datum `d` is a tuple, i.e., with your definition of `data` it is called as `loss( (V_train, S_train)... ) = loss(V_train, S_train)`. On the other hand, your loss function is defined as taking input and target as arguments (in that order). Thus, your model learns to map `V` to `S` instead of the intended mapping. Flipping either the order of targets and inputs in your data set or in the arguments of the loss function will fix it.
