# Compute gradient of gradient norm using zygote

**URL:** <https://discourse.julialang.org/t/compute-gradient-of-gradient-norm-using-zygote/79712>\
**Category:** New to Julia\
**Tags:** zygote\
**Created:** [April 19, 2022, 11:04pm UTC](https://discourse.julialang.org/t/compute-gradient-of-gradient-norm-using-zygote/79712 "2022-04-19T23:04:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 20, 2022, 5:22am UTC](https://discourse.julialang.org/t/compute-gradient-of-gradient-norm-using-zygote/79712/2 "2022-04-20T05:22:36Z")

</div>

Hi and welcome to the community!  
Could you post a minimal working example so that it is easier to help you?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

At first glance, it seems the problem comes from nested derivatives with Zygote: maybe this link can help

> <https://github.com/FluxML/Zygote.jl/issues/33>
>
> \`\`\`julia
> julia\> derivative(x-\>x\*derivative(y-\>x+y,1),1)
> ERROR: MethodError: no… method matching exprtype(::Core.Compiler.IRCode, ::String)
> Closest candidates are:
> exprtype(::Core.Compiler.IRCode, ::Expr) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\tools\\ir.jl:54
> exprtype(::Core.Compiler.IRCode, ::QuoteNode) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\tools\\ir.jl:51
> exprtype(::Core.Compiler.IRCode, ::GlobalRef) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\tools\\ir.jl:50
> ...
> Stacktrace:
> \[1\] \_broadcast\_getindex\_evalf at .\\broadcast.jl:574 \[inlined\]
> \[2\] \_broadcast\_getindex at .\\broadcast.jl:547 \[inlined\]
> \[3\] getindex at .\\broadcast.jl:507 \[inlined\]
> \[4\] copyto\_nonleaf!(::Array{DataType,1}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Base.Broadcast.Extruded{Array{Any,1},Tuple{Bool},Tuple{Int64}}}}, ::Base.OneTo{Int64}, ::Int64, ::Int64) at .\\broadcast.jl:923
> \[5\] copy(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Array{Any,1}}}) at .\\broadcast.jl:786
> \[6\] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Array{Any,1}}}) at .\\broadcast.jl:748
> \[7\] record!(::Core.Compiler.IRCode) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\reverse.jl:144
> \[8\] #Primal#39(::Int64, ::Type, ::Core.Compiler.IRCode) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\reverse.jl:189
> \[9\] Type at .\\none:0 \[inlined\]
> \[10\] #Adjoint#65 at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\reverse.jl:387 \[inlined\]
> \[11\] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:varargs,),Tuple{Int64}}, ::Type{Zygote.Adjoint}, ::Core.Compiler.IRCode) at .\\none:0
> \[12\] \_lookup\_grad(::Type) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\emit.jl:121
> \[13\] #s18#633 at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface2.jl:17 \[inlined\]
> \[14\] #s18#633(::Any, ::Any, ::Any) at .\\none:0
> \[15\] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at .\\boot.jl:506
> \[16\] derivative at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface.jl:37 \[inlined\]
> \[17\] (::Zygote.J{Tuple{typeof(derivative),getfield(Main, Symbol("##32#34")){Int64},Int64},Tuple{typeof(derivative),getfield(Main, Symbol("##32#34")){Int64},Int64,getfield(Zygote, Symbol("##148#back2#115")){getfield(Zygote, Symbol("##111#113")){1,Int64}},Zygote.J{Tuple{typeof(gradient),getfield(Main, Symbol("##32#34")){Int64},Int64},Tuple{typeof(gradient)}}}})(::Int64) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface2.jl:0
> \[18\] #31 at .\\REPL\[11\]:1 \[inlined\]
> \[19\] (::Zygote.J{Tuple{getfield(Main, Symbol("##31#33")),Int64},Tuple{getfield(Main, Symbol("##31#33")),Int64,getfield(Zygote, Symbol("##796#back2#450")){getfield(Zygote, Symbol("##448#449")){Int64,Int64}},Zygote.J{Tuple{typeof(derivative),getfield(Main, Symbol("##32#34")){Int64},Int64},Tuple{typeof(derivative),getfield(Main, Symbol("##32#34")){Int64},Int64,getfield(Zygote, Symbol("##148#back2#115")){getfield(Zygote, Symbol("##111#113")){1,Int64}},Zygote.J{Tuple{typeof(gradient),getfield(Main, Symbol("##32#34")){Int64},Int64},Tuple{typeof(gradient)}}}},getfield(Zygote, Symbol("##194#back2#147")){Zygote.Jnew{getfield(Main, Symbol("##32#34")){Int64},Nothing}}}})(::Int64) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface2.jl:0
> \[20\] (::getfield(Zygote, Symbol("##66#67")){Zygote.J{Tuple{getfield(Main, Symbol("##31#33")),Int64},Tuple{getfield(Main, Symbol("##31#33")),Int64,getfield(Zygote, Symbol("##796#back2#450")){getfield(Zygote, Symbol("##448#449")){Int64,Int64}},Zygote.J{Tuple{typeof(derivative),getfield(Main, Symbol("##32#34")){Int64},Int64},Tuple{typeof(derivative),getfield(Main, Symbol("##32#34")){Int64},Int64,getfield(Zygote, Symbol("##148#back2#115")){getfield(Zygote, Symbol("##111#113")){1,Int64}},Zygote.J{Tuple{typeof(gradient),getfield(Main, Symbol("##32#34")){Int64},Int64},Tuple{typeof(gradient)}}}},getfield(Zygote, Symbol("##194#back2#147")){Zygote.Jnew{getfield(Main, Symbol("##32#34")){Int64},Nothing}}}}})(::Int64) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface.jl:28
> \[21\] gradient(::Function, ::Int64) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface.jl:34
> \[22\] derivative(::Function, ::Int64) at C:\\Users\\han\\.julia\\packages\\Zygote\\5mNII\\src\\compiler\\interface.jl:37
> \[23\] top-level scope at none:0
> \`\`\`

---

_[View the full topic](https://discourse.julialang.org/t/compute-gradient-of-gradient-norm-using-zygote/79712)._
