# Ignore derivatives in ReverseDiff

**URL:** https://discourse.julialang.org/t/ignore-derivatives-in-reversediff/97252
**Category:** Machine Learning
**Tags:** zygote, reversediff, autodiff, chainrulescore
**Created:** [April 8, 2023, 5:30pm UTC](https://discourse.julialang.org/t/ignore-derivatives-in-reversediff/97252 "2023-04-08T17:30:25Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![skrinkle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skrinkle/32/48866_2.png) [@skrinkle](https://discourse.julialang.org/u/skrinkle)
#### Post date: [April 8, 2023, 5:30pm UTC](https://discourse.julialang.org/t/ignore-derivatives-in-reversediff/97252/1 "2023-04-08T17:30:26Z")

</div>

I want to exclude some functions in my model code from gradient calculations, using `@ignore_derivatives` from `ChainRulesCore`. It works with `Zygote`, but not with `ReverseDiff`. Here’s a MWE.

```julia
using Zygote, ReverseDiff
import ChainRulesCore: @ignore_derivatives

# function to ignore in gradient calculation but not in forward pass
g = x -> x^2

# main function
function f(x)
    x = x'*x
    @ignore_derivatives x = g(x)
    return x
end

inp = [2.];

@show Zygote.gradient(f, inp)[1];
@show ReverseDiff.gradient(f, inp);

```

This yields

```julia
(Zygote.gradient(f, inp))[1] = [4.0]
ReverseDiff.gradient(f, inp) = [32.0]

```

which is as intended with `Zygote` but not with `ReverseDiff`. How could I make this work in `ReverseDiff`?
