# Problem on model and gradient descend in Flux

**URL:** <https://discourse.julialang.org/t/problem-on-model-and-gradient-descend-in-flux/121589>\
**Category:** General Usage\
**Created:** [October 22, 2024, 1:06pm UTC](https://discourse.julialang.org/t/problem-on-model-and-gradient-descend-in-flux/121589 "2024-10-22T13:06:49Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [October 22, 2024, 5:47pm UTC](https://discourse.julialang.org/t/problem-on-model-and-gradient-descend-in-flux/121589/2 "2024-10-22T17:47:43Z")

</div>

Don’t think that will work as expected:

1. While you can pass functions to `Chain` they will be opaque, i.e., `Flux` cannot see inside to get parameters. Further, your function `x -> Dense(5, 5)` never calls the dense layer!  
Simply use `model1 = Dense(5 => 5, relu)` or `Chain(Dense(5 => 5), relu)` instead.
2. You can combine all your model parts into a single model:

```julia
model = Chain(Dense(4 => 5, relu), # your model1
              Parallel(tuple, # combine both model outputs into tuple
                       Dense(5 => 6), # model2
                       Dense(5 => 7))) # model3

# Use as follows ... note that I have changed the dimensions to better understand where each value is coming from
batch = rand(4, 8)
size.(model(batch)) # will be ((6, 8), (7, 8))

gradient(model) do m
    m2, m3 = m(batch)
    loss1 = m2 .- trueX
    loss2 = m3 .- trueY
    sum(vcat(loss1, loss2))
end

```

---

_[View the full topic](https://discourse.julialang.org/t/problem-on-model-and-gradient-descend-in-flux/121589)._
