# Need some help in understanding zygote gradient

**URL:** <https://discourse.julialang.org/t/need-some-help-in-understanding-zygote-gradient/86781>\
**Category:** Machine Learning\
**Created:** [September 5, 2022, 3:15am UTC](https://discourse.julialang.org/t/need-some-help-in-understanding-zygote-gradient/86781 "2022-09-05T03:15:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Post date:** [September 5, 2022, 3:15am UTC](https://discourse.julialang.org/t/need-some-help-in-understanding-zygote-gradient/86781/1 "2022-09-05T03:15:45Z")

</div>

Hi, I met some troubles in understanding zygote gradient.

for example: I plan to get the gradient for `f(x) = x^2` at `x = 5`, and also sum the result of `f(x)` for each time

```julia
let
  result = 0 
  f(x) = begin
      a = x^2
      result += a
      a
  end
  @info gradient(x -> f(x), 5)
  @info result
end

#it works, I got 
#[ Info: (10.0,)
#[ Info: 25

```

but if I run the code directly, an Error araised:

```julia
result = 0 
f(x) = begin
    a = x^2
    result += a
    a
end

gradient(x -> f(x), 5) 

ERROR: MethodError: no method matching +(::IRTools.Inner.Undefined, ::Int64)

```

what is the meaning of this error info while it works in a `let` block

Thanks for any suggestion, have a good day.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [September 5, 2022, 4:43am UTC](https://discourse.julialang.org/t/need-some-help-in-understanding-zygote-gradient/86781/2 "2022-09-05T04:43:36Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [September 7, 2022, 3:18pm UTC](https://discourse.julialang.org/t/need-some-help-in-understanding-zygote-gradient/86781/3 "2022-09-07T15:18:27Z")

</div>

Specifically, you are modifying a _global_ variable. This should work, but because it is a global you need you qualify it with `global` before modifying it in `f`. You can see this if you try to run `f` directly: it doesn’t work!

```julia
julia> f(1)
ERROR: UndefVarError: result not defined
Stacktrace:
 [1] f(x::Int64)
   @ Main ./REPL[3]:3
 [2] top-level scope
   @ REPL[4]:1

julia> f(x) = begin # you have to add the global keyword like this:
           a = x^2
           global result += a
           a
       end
f (generic function with 1 method)

julia> f(1)
1

julia> f(x) = begin # or this
           a = x^2
           global result
           result += a
           a
       end
f (generic function with 1 method)

julia> f(1)
1

```

All this to say that Zygote can’t make functions with errors suddenly work 😉
