# Zygote differentiation issues

**URL:** <https://discourse.julialang.org/t/zygote-differentiation-issues/54130>\
**Category:** Machine Learning\
**Created:** [January 28, 2021, 4:45pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130 "2021-01-28T16:45:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adhikarirsr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adhikarirsr/32/21462_2.png) [@adhikarirsr](https://discourse.julialang.org/u/adhikarirsr)\
**Post date:** [January 28, 2021, 4:45pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/1 "2021-01-28T16:45:48Z")

</div>

In an attempt to replicate what I saw [here:](https://www.youtube.com/watch?v=rF2QAJLM730&t=832s&ab_channel=ACMSIGPLAN) " The impact of differentiable programming: how ∂P is enabling new science in Julia"

```julia
function g(x)
    if x < 0
        print("Enter function name: ")
        getfield(Base, Symbol(readline()))(x)
    else
        2*x^3 + 4*x^2 +5*x
    end
end

```

```julia
julia> g'(4)
133
julia> g'(-pi/6) 
Enter function name: sin 
ERROR: Can't differentiate foreigncall expression 
Stacktrace: 
[1] error(::String) at ./error.jl:33 
[2] Symbol at ./boot.jl:438 [inlined] 
[3] (::typeof(∂(Symbol)))(::Nothing) at /home/user/.julia/packages/Zygote/ggM8Z/src/compiler/interface2.jl:0 
[4] g at /home/user/julia_control/cm_control.jl:17 [inlined] 
[5] (::typeof(∂(g)))(::Float64) at /home/user/.julia/packages/Zygote/ggM8Z/src/compiler/interface2.jl:0 
[6] (::Zygote.var"#41#42"{typeof(∂(g))})(::Float64) at /home/user/.julia/packages/Zygote/ggM8Z/src/compiler/interface.jl:40 
[7] gradient(::Function, ::Float64) at /home/user/.julia/packages/Zygote/ggM8Z/src/compiler/interface.jl:49
[8] (::Zygote.var"#43#44"{typeof(g)})(::Float64) at /home/user/.julia/packages/Zygote/ggM8Z/src/compiler/interface.jl:52 [9] top-level scope at none:1

```

Why am I getting this error and how to resolve it?

```julia
using Flux
using Zygote
using Trebuchet

function shoot(wind, angle, weight)
  Trebuchet.shoot((wind, Trebuchet.deg2rad(angle), weight))[2]
end

julia> shoot'(5,50,220)
ERROR: MethodError: no method matching (::Zygote.var"#43#44"{typeof(shoot)})(::Int64, ::Int64, ::Int64)

```

It seems like Zygote doesn’t like `(::Int64, ::Int64, ::Int64)`. How to fix it?

Also, tried this:

```julia
function shoot(pars)
    Trebuchet.shoot((pars[1], Trebuchet.deg2rad(pars[2]), pars[3]))[2]
end

```

got this error:

```julia
julia> shoot'([5,55,200])
ERROR: Compiling Tuple{typeof(Trebuchet.shoot),Tuple{Int64,Float64,Int64}}: try/catch is not supported.

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 28, 2021, 5:21pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/2 "2021-01-28T17:21:55Z")

</div>

> [@adhikarirsr](#):
>
> `getfield(Base, Symbol(readline()))(x)`

I don’t think Zygote (or any tool which works only at compile time) will be able to differentiate a function that is determined only at run time?

---

<div class="post-metadata">

**Author:** ![adhikarirsr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adhikarirsr/32/21462_2.png) [@adhikarirsr](https://discourse.julialang.org/u/adhikarirsr)\
**Post date:** [January 28, 2021, 5:25pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/3 "2021-01-28T17:25:21Z")

</div>

Here: [The impact of differentiable programming: how ∂P is enabling new science in Julia - YouTube](https://youtu.be/rF2QAJLM730?t=513)

For him, it seems to be working.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 28, 2021, 5:38pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/4 "2021-01-28T17:38:44Z")

</div>

Ah, yes, I cheated a smidge there. I elided a `Zygote.@nograd Symbol`. That should probably be upstreamed — and I imagine that’s what I was thinking when I chose to not include it in the talk, but promptly forgot.

---

<div class="post-metadata">

**Author:** ![adhikarirsr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adhikarirsr/32/21462_2.png) [@adhikarirsr](https://discourse.julialang.org/u/adhikarirsr)\
**Post date:** [January 28, 2021, 7:11pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/5 "2021-01-28T19:11:46Z")

</div>

Thanks for replying.

I am new to this thing. Could you please explain what `Zygote.@nograd Symbol` does ? Is there a good resource, where I can read about these things (other than fluxml.ai’s blog)?  
Also, how did you make `shoot'()` work?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 28, 2021, 7:57pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/6 "2021-01-28T19:57:17Z")

</div>

`Zygote.@nograd Symbol` is basically telling Zygote that it shouldn’t bother trying to differentiate _the construction_ of the symbol `:sin` itself because it’s non-differentiable. It fails without this because it’s part of the small bit of Julia that’s not implemented in Julia — we end up calling into Julia’s C core (which is what’s marked as the “foreigncall”).

Looks like I never published my scratch work that I used to generate that talk. I should do that:

[https://github.com/mbauman/dTrebuchet](https://github.com/mbauman/dTrebuchet)

The key on differentiating through `shoot` is to opt-in to forward mode AD and ensure you use a single array argument when differentiating with respect to the passed argument.

[https://github.com/mbauman/dTrebuchet/blob/ae5e215747b6da2ce59197ff18cdde16807eb980/reinforcement.jl#L9-L10](https://github.com/mbauman/dTrebuchet/blob/ae5e215747b6da2ce59197ff18cdde16807eb980/reinforcement.jl#L9-L10)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 28, 2021, 8:10pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/7 "2021-01-28T20:10:26Z")

</div>

> [@stevengj](#):
>
> I don’t think Zygote (or any tool which works only at compile time) will be able to differentiate a function that is determined only at run time?

That’s actually something Zygote can very much do. It’s even on the Readme.

```julia
julia> using Zygote

julia> fs = Dict("sin" => sin, "cos" => cos, "tan" => tan);

julia> gradient(x -> fs[readline()](x), 1)
sin
(0.5403023058681398,)

```

That `sin` there was typed by me into the repl.

---

<div class="post-metadata">

**Author:** ![adhikarirsr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adhikarirsr/32/21462_2.png) [@adhikarirsr](https://discourse.julialang.org/u/adhikarirsr)\
**Post date:** [January 28, 2021, 8:12pm UTC](https://discourse.julialang.org/t/zygote-differentiation-issues/54130/8 "2021-01-28T20:12:32Z")

</div>

Thanks a lot
