# Is there any good way to check gradient calculated by Zygote.jl

**URL:** <https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782>\
**Category:** General Usage\
**Tags:** package, zygote, finitediff\
**Created:** [April 22, 2021, 7:08am UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782 "2021-04-22T07:08:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard-Li](https://avatars.discourse-cdn.com/v4/letter/r/85f322/32.png) [@Richard-Li](https://discourse.julialang.org/u/Richard-Li)\
**Post date:** [April 22, 2021, 7:08am UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/1 "2021-04-22T07:08:56Z")

</div>

Hi there,  
I’d like to know is there any good way to check the gradient of custom matrix functions calculated by Zygote.jl.

like this one:

```julia
function my_custom_matrix_func(m)
    return sum(m * m')
end 

```

I know FiniteDifferences.jl, but I am wondering if it can do finite difference on custom matrix function and return the gradient.

Thanks for any reply.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [April 22, 2021, 9:14am UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/2 "2021-04-22T09:14:56Z")

</div>

It would be nice to have some functions which took care of everything automatically. … Something with the convenience of `gradient`/`params` but for FiniteDiff and which does the check automatically.

If this exists, I would like to know

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 22, 2021, 6:14pm UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/3 "2021-04-22T18:14:21Z")

</div>

Maybe I’m missing something but doesn’t FiniteDifferences already do this? E.g.:

```julia
julia> using FiniteDifferences

julia> grad(central_fdm(3,1), my_custom_matrix_func, [1. 2; 3 4])[1]
2×2 Matrix{Float64}:
 8.0 12.0
 8.0 12.0

```

---

<div class="post-metadata">

**Author:** ![burmecia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/burmecia/32/28211_2.png) [@burmecia](https://discourse.julialang.org/u/burmecia)\
**Post date:** [August 15, 2021, 11:59pm UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/4 "2021-08-15T23:59:05Z")

</div>

Not sure if there any official ways to check Zygote gradient, but I usually check it by this:

```julia
gs = gradient(...) # whatever gradient you get

# check parameter `p` and corresponding gradient `g` in gradient
for (p, g) in pairs(gs)
  @info(p) # print out parameter
  @info(g) # print out corresponding gradient
end

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 21, 2021, 12:43pm UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/5 "2021-08-21T12:43:00Z")

</div>

That’s not checking the numerical values.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [August 21, 2021, 12:43pm UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/6 "2021-08-21T12:43:28Z")

</div>

It would be nice to have something that automatically works with the `params` interface.

---

<div class="post-metadata">

**Author:** ![burmecia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/burmecia/32/28211_2.png) [@burmecia](https://discourse.julialang.org/u/burmecia)\
**Post date:** [August 23, 2021, 11:21pm UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/7 "2021-08-23T23:21:12Z")

</div>

Alright, maybe we’re talking about different things, I thought you were trying to check gradients by human eyes. And I just find another useful utility [@showgrad](https://fluxml.ai/Zygote.jl/latest/utils/#Zygote.@showgrad), which can help debug gradients.

---

<div class="post-metadata">

**Author:** ![curtd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curtd/32/15868_2.png) [@curtd](https://discourse.julialang.org/u/curtd)\
**Post date:** [August 26, 2021, 4:34pm UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/8 "2021-08-26T16:34:42Z")

</div>

If you’re just wanting to confirm your numerical gradients are performing correctly, IMO you should always be performing the gradient test. If you have your function f(x) : \mathbb{R}^n \mapsto \mathbb{R} evaluated at a random point x\in\mathbb{R}^n, given the (computed) gradient \nabla f(x) \in \mathbb{R}^n and a random direction \Delta \in \mathbb{R}^n, the following should hold

|f(x+h\Delta) - f(x)| = O(h) \\ |f(x+h\Delta) - f(x) - h \langle \nabla f(x), \Delta \rangle | = O(h^2)

In Julia code, for your matrix case, this would be something along the lines of

```julia
n = 1000
x = randn(n, n)
delta = randn(n,n)
f = my_custom_matrix_func
f0 = f(x)
gradf = # computed from Zygote, or wherever
df = dot(gradf, delta)
h = 10 .^ (-6.0:0.0)
err_zeroth_order = zeros(length(h))
err_first_order = zeros(length(h))
for (i,hi) in enumerate(h)
     f1 = f(x+hi*delta)
     err_zeroth_order[i] = abs(f1-f0)
     err_first_order[i] = abs(f1-f0-hi*df)
end
h0 = median(diff(log10.*(err_zeroth_order))) # Should be ~ 1
h1 = median(diff(log10.*(err_first_order))) # Should be ~2 if your gradient is computed correctly

```

The O(h^2) behaviour won’t exactly hold for very small h as numerical imprecision errors dominate the convergence error. Lots of edge case considerations for this one but I hope this gets the basic idea across. It’s a good test to add to a test suite!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 30, 2021, 8:58am UTC](https://discourse.julialang.org/t/is-there-any-good-way-to-check-gradient-calculated-by-zygote-jl/59782/9 "2021-08-30T08:58:07Z")

</div>

In 99% of cases you don’t want to implement your own FD code for testing, but use something robust like

[https://github.com/JuliaDiff/FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl)

with higher order algorithms and stepsize adaptation.
