# Any Tracker.forward in Zygote.jl?

**URL:** <https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079>\
**Category:** General Usage\
**Created:** [November 14, 2019, 10:34am UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079 "2019-11-14T10:34:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [November 14, 2019, 10:34am UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079/1 "2019-11-14T10:34:09Z")

</div>

Hi,

Maybe a noob question.  
I’ve an old code based on Flux and Tracker, which solves the derivatives of a matrix

```julia
using Flux
ann = Chain(Dense(2, 10, tanh), Dense(10, 1))
u(x) = ann(x)
ux(x) = Tracker.forward(u, x)[2](1)[1]

X = zeros(2, 1000)
u(X)
ux(X)

```

When I try to turn to Zygote.jl, I’m confused about how to use AD solver when the function output isn’t scalar.  
How could I rewrite the above code in Zygote framework?

Many thanks 🙂

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 14, 2019, 3:15pm UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079/2 "2019-11-14T15:15:47Z")

</div>

What you write looks to be just `ux(x) = gradient(x, x)[1]`, which is the same on either. But if you need the value too, the equivalent of `Tracker.forward` is now `Zygote.pullback`.

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [November 14, 2019, 4:40pm UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079/3 "2019-11-14T16:40:26Z")

</div>

Thanks 🙂  
But now the calling of Zygote.pullback() in its master branch just reports:

```julia
UndefVarError: pullback not defined

Stacktrace:
 [1] getproperty(::Module, ::Symbol) at ./Base.jl:13
 [2] top-level scope at In[10]:1

```

Do you have recommended usage of it? Should I transfer to a dev branch or?

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [November 14, 2019, 6:47pm UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079/4 "2019-11-14T18:47:18Z")

</div>

I figured it is about the compatibility between Julia v1.3 and v1.2.  
When I turned back to v1.2, it worked.

But I’m still confused about the detailed usage of pullback function.  
Could you show me the equivalent codes using pullback() as the above case?  
Many thanks.

---

<div class="post-metadata">

**Author:** ![Sleort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleort/32/4875_2.png) [@Sleort](https://discourse.julialang.org/u/Sleort)\
**Post date:** [November 15, 2019, 1:02am UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079/5 "2019-11-15T01:02:57Z")

</div>

Are you looking for something like this:

```julia
using Flux, Zygote

ann = Chain(Dense(2, 10, tanh), Dense(10, 1))

X = rand(2,1000)
value, backpropagator = Zygote.pullback(ann, X)
sensitivity = ones(size(X)) #Some sensitivity (note: in this case it's not a scalar!)
backpropagator(sensitivity)[1]

```

? Or, in case you want both the value and gradient of a scalar valued function:

```julia
function value_and_gradient(f, x...)
    value, back = Zygote.pullback(f, x...)
    grad = back(1)[1]
    return value, grad
end

using Statistics: mean
ann2 = Chain(Dense(2, 10, tanh), Dense(10, 1), mean) #note: scalar output

value_gradient(ann2, X) # ann2(X), ∇ann2(X)

```

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [November 15, 2019, 1:36pm UTC](https://discourse.julialang.org/t/any-tracker-forward-in-zygote-jl/31079/6 "2019-11-15T13:36:28Z")

</div>

Thanks! works for me.
