# How to compute partial derivatives efficiently with Zygote

**URL:** <https://discourse.julialang.org/t/how-to-compute-partial-derivatives-efficiently-with-zygote/28731>\
**Category:** Machine Learning\
**Created:** [September 13, 2019, 6:30pm UTC](https://discourse.julialang.org/t/how-to-compute-partial-derivatives-efficiently-with-zygote/28731 "2019-09-13T18:30:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SEA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea/32/8956_2.png) [@SEA](https://discourse.julialang.org/u/SEA)\
**Post date:** [September 13, 2019, 6:30pm UTC](https://discourse.julialang.org/t/how-to-compute-partial-derivatives-efficiently-with-zygote/28731/1 "2019-09-13T18:30:20Z")

</div>

Suppose we have a function f(x, y), and want to compute \partial\_x f(x,y) for many y’s, ideally using no memory allocation. Using Zygote, I tried Zygote.forwarddiff(y-\>f(x,y), y).

However, I believe this creates a new anonymous function each time we call it with a different x. So the call to fowarddiff is not efficient.

Also, we can’t use gradient or forwarddiff on f directly, because in reality there are many more arguments (x,y,z,…) and computing and storing all of their derivatives is again inefficient.

Is there a way to achieve what I intend to do with Zygote or any other AD package?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 13, 2019, 7:13pm UTC](https://discourse.julialang.org/t/how-to-compute-partial-derivatives-efficiently-with-zygote/28731/2 "2019-09-13T19:13:09Z")

</div>

> [@SEA](#):
>
> However, I believe this creates a new anonymous function each time we call it with a different x. So the call to fowarddiff is not efficient.

Creating an anonymous function (closure) is more or less free after it’s compiled in Julia so I don’t think you need to worry about it.

But there is a reason why taking partial derivative can be slow in Zygote at the moment: [Slow backward pass when the forward pass touches a large array · Issue #323 · FluxML/Zygote.jl · GitHub](https://github.com/FluxML/Zygote.jl/issues/323). Hopefully, this will be fixed by [[WIP] Use ChainRules by oxinabox · Pull Request #291 · FluxML/Zygote.jl · GitHub](https://github.com/FluxML/Zygote.jl/pull/291).

---

<div class="post-metadata">

**Author:** ![SEA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea/32/8956_2.png) [@SEA](https://discourse.julialang.org/u/SEA)\
**Post date:** [September 13, 2019, 7:23pm UTC](https://discourse.julialang.org/t/how-to-compute-partial-derivatives-efficiently-with-zygote/28731/3 "2019-09-13T19:23:38Z")

</div>

Thank you for the pointers, and let’s hope so!
