# Zygote “double where trick”

**URL:** <https://discourse.julialang.org/t/zygote-double-where-trick/108118>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 28, 2023, 6:25am UTC](https://discourse.julialang.org/t/zygote-double-where-trick/108118 "2023-12-28T06:25:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartalecH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smartalech/32/32379_2.png) [@smartalecH](https://discourse.julialang.org/u/smartalecH)\
**Post date:** [December 28, 2023, 6:25am UTC](https://discourse.julialang.org/t/zygote-double-where-trick/108118/1 "2023-12-28T06:25:35Z")

</div>

I spend a lot of time with jax, where I often have to employ the “[double where trick](https://jax.readthedocs.io/en/latest/faq.html#gradients-contain-nan-where-using-where)” to prevent NaNs from propagating in the backward pass.

The Zygote docs mention that arrays should be immutable. So if I want to enforce some limiting values (because I’m anticipating some NaNs to pop up), then I’m assuming I need to do array-based logic, just as I do with jax (rather than looping through each index and updating the value based on some conditional).

Are there any idiomatic tricks with Zygote in this context? Should I also implement a “double where” to keep the backward pass clean? Or is the compiler smart enough to work around this?

Thanks!
