# Flux.Chain vs expand everything in a function

**URL:** <https://discourse.julialang.org/t/flux-chain-vs-expand-everything-in-a-function/66982>\
**Category:** Machine Learning\
**Created:** [August 25, 2021, 2:39pm UTC](https://discourse.julialang.org/t/flux-chain-vs-expand-everything-in-a-function/66982 "2021-08-25T14:39:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiracFermion1](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@DiracFermion1](https://discourse.julialang.org/u/DiracFermion1)\
**Post date:** [August 25, 2021, 2:39pm UTC](https://discourse.julialang.org/t/flux-chain-vs-expand-everything-in-a-function/66982/1 "2021-08-25T14:39:25Z")

</div>

Hi, I was trying to understand what the difference is between `Flux.Chain` vs expanding all operations in a single function.

For instance, given following object:

```julia
Chain(Dense(d_in, d_hidden, tanh), Dense(d_hidden, d_hidden, tanh), Dense(d_hidden, d_in,tanh))

```

if I replace it with

```julia
function f(x)
    out = Dense(d_in, d_hidden, tanh)(x)
    out = Dense(d_hidden, d_hidden, tanh)(out)
    out = Dense(d_hidden, d_in,tanh)(out)
end

```

Does anyone know whether these two are exactly the same in terms of training/testing behaviors?

Thank you!

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [August 25, 2021, 2:59pm UTC](https://discourse.julialang.org/t/flux-chain-vs-expand-everything-in-a-function/66982/2 "2021-08-25T14:59:47Z")

</div>

They are not exactly the same, because the second example immediately discards the 3 `Dense` layers after `f` returns. If you constructed them beforehand, passed them into the function and passed their parameters when taking a gradient, then the behaviour will be the same. You’ll also have re-invented most of the functionality of `Chain`!
