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

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:

``````using Flux

struct VecofMat{T}
W::T
end

Flux.@functor VecofMat

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

x = rand(2)
gs = gradient(()->sum(model(x)), params(model))
end

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

model = VecofMat([rand(2,2) for i = 1:3])
``````

Ouputs:

``````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

(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!

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

``````using Flux

struct VecofMat{T}
W::T
end

Flux.@functor VecofMat

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

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

model = VecofMat([rand(2,2) for i = 1:3])