# Zygote gradient

**URL:** <https://discourse.julialang.org/t/zygote-gradient/26715>\
**Category:** General Usage\
**Created:** [July 24, 2019, 3:27am UTC](https://discourse.julialang.org/t/zygote-gradient/26715 "2019-07-24T03:27:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wormholestds9](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wormholestds9](https://discourse.julialang.org/u/wormholestds9)\
**Post date:** [July 24, 2019, 3:27am UTC](https://discourse.julialang.org/t/zygote-gradient/26715/1 "2019-07-24T03:27:45Z")

</div>

Zygote gradient seems to return an odd result for a constant function. That is,  
f1 = (x) → 0.0  
gradient(f1,0) = (nothing,)  
but  
f2 = (x) → x^2  
gradient(f2, 0) = (0,)

Why does the first one return “nothing” instead of 0, which is what I expected.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 24, 2019, 7:18am UTC](https://discourse.julialang.org/t/zygote-gradient/26715/2 "2019-07-24T07:18:26Z")

</div>

Zygote uses `nothing` as a kind of strong zero, since the derivative of `f1` is always zero, while that of `f2` depends on `x` and is only zero for this particular value.

The reason to make this distinction is that if you give it `f1(g(x,y))`, it knows never to bother working out the gradient of `g` at all, as it will never be needed. But for `f2(g(x,y))`, the gradient function which gets compiled must allow for `g(x,y)` being nonzero.

---

<div class="post-metadata">

**Author:** ![wormholestds9](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wormholestds9](https://discourse.julialang.org/u/wormholestds9)\
**Post date:** [July 24, 2019, 9:58am UTC](https://discourse.julialang.org/t/zygote-gradient/26715/3 "2019-07-24T09:58:14Z")

</div>

Thank you.
