# Stop\_gradient/detach equivalent in flux

**URL:** <https://discourse.julialang.org/t/stop-gradient-detach-equivalent-in-flux/31926>\
**Category:** Machine Learning\
**Tags:** question\
**Created:** [December 6, 2019, 7:33am UTC](https://discourse.julialang.org/t/stop-gradient-detach-equivalent-in-flux/31926 "2019-12-06T07:33:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rassibassi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rassibassi/32/9697_2.png) [@Rassibassi](https://discourse.julialang.org/u/Rassibassi)\
**Post date:** [December 6, 2019, 7:33am UTC](https://discourse.julialang.org/t/stop-gradient-detach-equivalent-in-flux/31926/1 "2019-12-06T07:33:40Z")

</div>

Hi,

is there an equivalent to Tensorflow’s stop\_gradient/PyTorch’s detach in FluxML?

Thanks!

Rasmus

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [December 8, 2019, 6:55pm UTC](https://discourse.julialang.org/t/stop-gradient-detach-equivalent-in-flux/31926/2 "2019-12-08T18:55:03Z")

</div>

Hi,

I don’t know if there is anything built in, but this issue has a good tip in the comments for how to do it yourself: [https://github.com/FluxML/Zygote.jl/issues/175](https://github.com/FluxML/Zygote.jl/issues/175)

Summary:

```julia
stop_gradient(f) = f()
Zygote.@nograd stop_gradient

# Now you can use it like this (note: does not work in REPL due to global scoping rules):
a = 3
b = 4
c = stop_gradient() do
     a + b
end

```

```julia

```

---

<div class="post-metadata">

**Author:** ![Rassibassi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rassibassi/32/9697_2.png) [@Rassibassi](https://discourse.julialang.org/u/Rassibassi)\
**Post date:** [December 11, 2019, 8:34pm UTC](https://discourse.julialang.org/t/stop-gradient-detach-equivalent-in-flux/31926/3 "2019-12-11T20:34:14Z")

</div>

Great! Thanks a lot, it made my code work 🙂 example over here: [Autoencoder for telecommunication (Constellation shaping)](https://discourse.julialang.org/t/autoencoder-for-telecommunication-constellation-shaping/30613)
