# Incorrect (implicit) gradients for a custom model in Flux

**URL:** https://discourse.julialang.org/t/incorrect-implicit-gradients-for-a-custom-model-in-flux/56902
**Category:** Machine Learning
**Tags:** question, flux
**Created:** [March 10, 2021, 6:39pm UTC](https://discourse.julialang.org/t/incorrect-implicit-gradients-for-a-custom-model-in-flux/56902 "2021-03-10T18:39:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![arlk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arlk/32/2465_2.png) [@arlk](https://discourse.julialang.org/u/arlk)
#### Post date: [March 10, 2021, 6:39pm UTC](https://discourse.julialang.org/t/incorrect-implicit-gradients-for-a-custom-model-in-flux/56902/1 "2021-03-10T18:39:49Z")

</div>

I’m seeing incorrectly computed gradients when I use a vector of matrices as a model in Flux. The implicit gradients compute to 0 but the structural gradients seem to be computed correctly.  
MWE:

```julia
using Flux

struct VecofMat{T}
    W::T
end

Flux.@functor VecofMat

function (model::VecofMat)(x)
    mapreduce(m->m*x, +, model.W)
end

function implicit_grads(model)
    x = rand(2)
    gs = gradient(()->sum(model(x)), params(model))
    return gs.grads
end

function structural_grads(model)
    x = rand(2)
    gs = gradient((model)->sum(model(x)), model)
    return gs[1]
end

model = VecofMat([rand(2,2) for i = 1:3])
@show implicit_grads(model)
@show structural_grads(model)

```

Ouputs:

```julia
julia> implicit_grads(model)
IdDict{Any,Any} with 3 entries:
  [0.717542 0.507439; 0.662728 0.493812] => nothing
  [0.888345 0.512297; 0.347085 0.122115] => nothing
  [0.801164 0.874216; 0.394027 0.199299] => nothing

julia> structural_grads(model)
(W = [[0.8304204082278925 0.9041784786828033; 0.8304204082278925 0.9041784786828033], [0.8304204082278925 0.9041784786828033; 0.8304204082278925 0.9041784786828033], [0.8304204082278925 0.9041784786828033; 0.8304204082278925 0.9041784786828033]],)

```

These issues disappear when I use a multi-dimensional array instead of a vector of matrices. But I just wanted to know if I’m making a mistake somewhere when I’m computing the implicit gradients. Thanks!

---

<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: [March 11, 2021, 6:51am UTC](https://discourse.julialang.org/t/incorrect-implicit-gradients-for-a-custom-model-in-flux/56902/2 "2021-03-11T06:51:06Z")

</div>

Seems like a nasty bug, you should file an issue in Flux. It can be reduced a little bit more

```julia
using Flux

struct VecofMat{T}
    W::T
end

Flux.@functor VecofMat

function implicit_grads(model)
    gs = gradient(() -> sum(model.W[1]), params(model))
    return gs.grads
end

function structural_grads(model)
    gs = gradient( model -> sum(model.W[1]), model)
    return gs[1]
end

model = VecofMat([rand(2,2) for i = 1:3])
@show implicit_grads(model)
@show structural_grads(model)

```

---

<div class="post-metadata">

### Author: ![arlk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arlk/32/2465_2.png) [@arlk](https://discourse.julialang.org/u/arlk)
#### Post date: [March 12, 2021, 7:33pm UTC](https://discourse.julialang.org/t/incorrect-implicit-gradients-for-a-custom-model-in-flux/56902/3 "2021-03-12T19:33:06Z")

</div>

Just did. Thanks for taking a look! 🙂

---

<div class="post-metadata">

### Author: ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)
#### Post date: [March 12, 2021, 10:28pm UTC](https://discourse.julialang.org/t/incorrect-implicit-gradients-for-a-custom-model-in-flux/56902/4 "2021-03-12T22:28:53Z")

</div>

Cross referencing the bug report for those who are interested: [Implicit params: no gradient for `Array` element of `Vector{AbstractArray}` when parent `Vector` used in AD · Issue #1017 · FluxML/Zygote.jl · GitHub](https://github.com/FluxML/Flux.jl/issues/1536)
