Need some help in understanding zygote gradient

The problem is that you are modifying variable outside of the scope of the gradient, which is something AD is not happy with. If possible, I would rewrite the code as

f(x) = x^2
r, gs = Flux.withgradient(f, 5)
result +=r

1 Like