# How to test differentiability efficiently

**URL:** <https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631>\
**Category:** General Usage\
**Tags:** testing, autodiff\
**Created:** [November 23, 2023, 3:30pm UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631 "2023-11-23T15:30:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devetak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devetak/32/50611_2.png) [@Devetak](https://discourse.julialang.org/u/Devetak)\
**Post date:** [November 23, 2023, 3:30pm UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/1 "2023-11-23T15:30:22Z")

</div>

Hello.

As the tile suggest I am interested in testing (using Test) the “AD” capabilities of functions I write. My idea is to simply compare if `Zygote.gradient()` and finite differences give the same answer.

Many times I have functions which have multiple inputs say `my_func(a,b,c,d,e)`. So far what I do is generate `f = x -> my_func(x, b, c, d, e)` and test and so on.

Is there a better way of doing this? By better I mean less verbose than generating 5 functions for each function I make and then testing each. Perhaps to do it in fewer lines and more automatically.

One concern I have is that I want to know which input the test fails. Which splitting it this way makes it easier.

Any suggestions.

Thank you!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 23, 2023, 3:46pm UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/2 "2023-11-23T15:46:28Z")

</div>

> [@Devetak](#):
>
> By better I mean less verbose than generating 5 functions for each function I make and then testing each.

Change it to take a single vector argument (e.g. `x -> my_func(x...)`) and compute the Jacobian?

---

<div class="post-metadata">

**Author:** ![Devetak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devetak/32/50611_2.png) [@Devetak](https://discourse.julialang.org/u/Devetak)\
**Post date:** [November 23, 2023, 4:08pm UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/3 "2023-11-23T16:08:35Z")

</div>

woudn’t that make it hard to see which input is cousing troubles?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 23, 2023, 4:48pm UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/4 "2023-11-23T16:48:00Z")

</div>

Depending on which components of the Jacobian are wrong, you will know immediately which input screwed up. It’s exactly the same information, just all at once

---

<div class="post-metadata">

**Author:** ![Devetak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devetak/32/50611_2.png) [@Devetak](https://discourse.julialang.org/u/Devetak)\
**Post date:** [November 27, 2023, 8:42am UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/5 "2023-11-27T08:42:46Z")

</div>

Maybe, but if you split it you can see when the test fails which part did. If you just see Jacobian[2, 12] was miscalculated that is hard? Or is there a way of making that meaningful in tests?

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [November 27, 2023, 9:51am UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/6 "2023-11-27T09:51:22Z")

</div>

This makes the output a bit verbose, but you could wrap the tests in their own `@testset` with an appropriate name. Perhaps group the tests by the corresponding column of the jacobian and attach the name of the variable only to the corresponding `@testset`.

I had something like this in mind, but I’m not sure if it suits your needs

```julia
using Test

f(x) = x * x'

@testset "Differentiability" begin
    variableNames = [:x, :y]

    testInputs = [[1, 2], [3, 4]]

    for xVal in testInputs
        jac = f(xVal)
        @testset "point = $(xVal)" begin
            for (i, (x, c)) in enumerate(zip(variableNames, eachcol(jac)))
                @testset "$x = $(xVal[i])" begin @test c == [1, 2] end
            end
        end
    end
end

```

Using `NamedTuples` instead of putting the variables names in a separate vector might even be more convenient.

---

<div class="post-metadata">

**Author:** ![Devetak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devetak/32/50611_2.png) [@Devetak](https://discourse.julialang.org/u/Devetak)\
**Post date:** [November 30, 2023, 12:14pm UTC](https://discourse.julialang.org/t/how-to-test-differentiability-efficiently/106631/7 "2023-11-30T12:14:14Z")

</div>

I think it is very cool. Thank you!
