# Why Dense layer and Chain don't work for calculating gradient?

**URL:** <https://discourse.julialang.org/t/why-dense-layer-and-chain-dont-work-for-calculating-gradient/137360>\
**Category:** General Usage\
**Created:** [May 31, 2026, 10:24am UTC](https://discourse.julialang.org/t/why-dense-layer-and-chain-dont-work-for-calculating-gradient/137360 "2026-05-31T10:24:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MathJulia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mathjulia/32/224167_2.png) [@MathJulia](https://discourse.julialang.org/u/MathJulia)\
**Post date:** [May 31, 2026, 10:24am UTC](https://discourse.julialang.org/t/why-dense-layer-and-chain-dont-work-for-calculating-gradient/137360/1 "2026-05-31T10:24:02Z")

</div>

I worked with Julia for 5 years. Flux.jl gradient worked correctly. but in new versions I receive this error. Chain(Dense…, …) I see this error.  
\*\*MethodError: no method matching l(::Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, ::Int64, ::Int64)  
Chain(Dense…, …) I see this error.  
\*\*MethodError: no method matching l(::Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, ::Int64, ::Int64)

Closest candidates are:  
l(::Any)  
@ Main REPL[4]:1\*\*  
Flux can’t create gradient of models which created by Chain. what can I do?  
Could this be from Zygote?

Closest candidates are:  
l(::Any)  
@ Main REPL[4]:1\*\*  
Flux can’t create gradient of models which created by Chain. what can I do?  
Could this be from Zygote?  
My model is Chain(Dense(1 =\> 10), Dense(10 =\> 1)).  
l is loss function.  
Please show me the the correct way to create network and optimize it with Chain for network of Dense layers.

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2090d1ca18c68dbbd25e5ce65eb865b0813f1d1.jpeg)  
MethodError: no method matching (::Dense{typeof(identity), Matrix{Float32}, Vector{Float32}})(::Chain{Tuple{Dense{…}, Dense{…}}})

Closest candidates are:  
(::Dense)(::AbstractVecOrMat)  
@ Flux C:\Users\javan.julia\packages\Flux\9PibT\src\layers\basic.jl:196  
(::Dense)(::AbstractArray)  
@ Flux C:\Users\javan.julia\packages\Flux\9PibT\src\layers\basic.jl:202

Stacktrace:  
[1] macro expansion  
@ C:\Users\javan.julia\packages\Zygote\55SqB\src\compiler\interface2.jl:0 [inlined]  
[2] \_pullback(ctx::Zygote.Context{false}, f::Dense{typeof(identity), Matrix{…}, Vector{…}}, args::Chain{Tuple{…}})  
@ Zygote C:\Users\javan.julia\packages\Zygote\55SqB\src\compiler\interface2.jl:81
