# Flux: gradient of Chain returns nothing

**URL:** <https://discourse.julialang.org/t/flux-gradient-of-chain-returns-nothing/62725>\
**Category:** General Usage\
**Tags:** question, flux, zygote\
**Created:** [June 11, 2021, 9:08am UTC](https://discourse.julialang.org/t/flux-gradient-of-chain-returns-nothing/62725 "2021-06-11T09:08:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgbrasier](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jgbrasier](https://discourse.julialang.org/u/jgbrasier)\
**Post date:** [June 11, 2021, 9:08am UTC](https://discourse.julialang.org/t/flux-gradient-of-chain-returns-nothing/62725/1 "2021-06-11T09:08:24Z")

</div>

I have defined a MLP (multi layer perceptron) with the Chain command, and this MLP is wrapped in a mutable structure.  
The gradient of this Chain (2 layer dense NN) returns nothing. Is this a bug, or am I doing this the wrong way?

Minimum working example:

```julia
function loss(filter, x, y)
    return norm(y - filter.f(x))
end

mutable struct Filter{a}
    f::a
end

funct = Chain(Dense(2, 10, sigmoid), Dense(10, 2))
filt = Filter(funct)
opt = ADAM()

y = [1.0; 1.0]
x = [0.0; 0.0]
ps = Flux.Params(filt.f)

for i= 1:5
    grads = gradient(ps) do
        loss(filt, x, y)
    end
    update!(opt, ps, grads)
    println(grads[ps[1]])
    println(ps[1].weight, "\n")
end

```

which prints:

```julia
nothing
Float32[-0.30972472 0.48900542; 0.40722048 -0.47656116; 0.27757087 0.12060472; 0.23041193 -0.7000659; 0.118268944 0.45202392; -0.32605898 -0.16675131; 0.63128626 -0.24441159; -0.67778873 -0.36194095; 0.14881344 -0.52690184; -0.0017206029 -0.22051147]

nothing
Float32[-0.30972472 0.48900542; 0.40722048 -0.47656116; 0.27757087 0.12060472; 0.23041193 -0.7000659; 0.118268944 0.45202392; -0.32605898 -0.16675131; 0.63128626 -0.24441159; -0.67778873 -0.36194095; 0.14881344 -0.52690184; -0.0017206029 -0.22051147]

nothing
Float32[-0.30972472 0.48900542; 0.40722048 -0.47656116; 0.27757087 0.12060472; 0.23041193 -0.7000659; 0.118268944 0.45202392; -0.32605898 -0.16675131; 0.63128626 -0.24441159; -0.67778873 -0.36194095; 0.14881344 -0.52690184; -0.0017206029 -0.22051147]

nothing
Float32[-0.30972472 0.48900542; 0.40722048 -0.47656116; 0.27757087 0.12060472; 0.23041193 -0.7000659; 0.118268944 0.45202392; -0.32605898 -0.16675131; 0.63128626 -0.24441159; -0.67778873 -0.36194095; 0.14881344 -0.52690184; -0.0017206029 -0.22051147]

nothing
Float32[-0.30972472 0.48900542; 0.40722048 -0.47656116; 0.27757087 0.12060472; 0.23041193 -0.7000659; 0.118268944 0.45202392; -0.32605898 -0.16675131; 0.63128626 -0.24441159; -0.67778873 -0.36194095; 0.14881344 -0.52690184; -0.0017206029 -0.22051147]

```

As such, the weights of the NN are obviously not updated.  
Any help would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [June 11, 2021, 9:37am UTC](https://discourse.julialang.org/t/flux-gradient-of-chain-returns-nothing/62725/2 "2021-06-11T09:37:27Z")

</div>

Change

```julia
ps = Flux.Params(filt.f)

```

to

```julia
ps = Flux.params(filt.f)

```
