Why does Zygote Gradient not work on Polynomials.jl?

What am I missing. Why does Zygote’s gradient not work on Polynomials.jl polynomial. That module even has a derivative function.

New to this Zygote stuff.

module testZygote

using Zygote
using Polynomials

p=Polynomial([1.0,0.0,3.0,4.0])
@show derivative(p)(5.0)

@show gradient(p,5.0)


end

Gives me this… which also shows up when I’m using sciml_train and zygote.

julia> include("testZygotePoly.jl")
WARNING: replacing module testZygote.
(derivative(p))(5.0) = 330.0
ERROR: MethodError: no method matching similar(::Polynomial{Float64,:x}, ::Type{Float64}, ::Int64)
Closest candidates are:
  similar(::BandedMatrices.BandedMatrix, ::Type, ::Integer) at C:\Users\bakerar\.julia\packages\BandedMatrices\sSIkN\src\banded\BandedMatrix.jl:308  
  similar(::BandedMatrices.BandedMatrix, ::Type, ::Integer, ::Integer) at C:\Users\bakerar\.julia\packages\BandedMatrices\sSIkN\src\banded\BandedMatrix.jl:308
  similar(::BandedMatrices.BandedMatrix, ::Type, ::Integer, ::Integer, ::Integer) at C:\Users\bakerar\.julia\packages\BandedMatrices\sSIkN\src\banded\BandedMatrix.jl:308
  ...
Stacktrace:
 [1] _evalpoly_intermediates(::Float64, ::Polynomial{Float64,:x}) at C:\Users\bakerar\.julia\packages\ChainRules\wuTHR\src\rulesets\Base\evalpoly.jl:54
 [2] rrule(::typeof(evalpoly), ::Float64, ::Polynomial{Float64,:x}) at C:\Users\bakerar\.julia\packages\ChainRules\wuTHR\src\rulesets\Base\evalpoly.jl:15
 [3] chain_rrule at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\chainrules.jl:89 [inlined]
 [4] macro expansion at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\interface2.jl:0 [inlined]
 [5] _pullback(::Zygote.Context, ::typeof(evalpoly), ::Float64, ::Polynomial{Float64,:x}) at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\interface2.jl:9
 [6] Polynomial at C:\Users\bakerar\.julia\packages\Polynomials\1nhQ0\src\abstract.jl:65 [inlined]
 [7] _pullback(::Zygote.Context, ::Polynomial{Float64,:x}, ::Float64) at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\interface2.jl:0  
 [8] _pullback(::Polynomial{Float64,:x}, ::Float64) at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\interface.jl:33
 [9] pullback(::Polynomial{Float64,:x}, ::Float64) at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\interface.jl:39
 [10] gradient(::Polynomial{Float64,:x}, ::Float64) at C:\Users\bakerar\.julia\packages\Zygote\KpME9\src\compiler\interface.jl:48
 [11] top-level scope at show.jl:641

My package status is:

 [537997a7] AbstractPlotting v0.15.24
  [35d6a980] ColorSchemes v3.10.2
  [aae7a2af] DiffEqFlux v1.34.1
  [41bf760c] DiffEqSensitivity v6.42.0
  [0c46a032] DifferentialEquations v6.16.0
  [587475ba] Flux v0.11.6
  [f6369f11] ForwardDiff v0.10.16
  [e9467ef8] GLMakie v0.1.29
  [28b8d3ca] GR v0.55.0
  [0337cf30] GRUtils v0.6.0
  [10e44e05] MATLAB v0.8.0
  [961ee093] ModelingToolkit v5.13.0
  [429524aa] Optim v1.2.4
  [1dea7af3] OrdinaryDiffEq v5.52.0
  [58dd65bb] Plotly v0.3.0
  [a03496cd] PlotlyBase v0.4.3
  [91a5bcdd] Plots v1.10.6
  [c3e4b0f8] Pluto v0.12.21
  [f27b6e38] Polynomials v2.0.0
  [731186ca] RecursiveArrayTools v2.11.0
  [295af30f] Revise v3.1.14
  [00bb0486] StandardAerospace v0.3.1
  [276b4fcb] WGLMakie v0.3.3
  [e88e6eb3] Zygote v0.6.3
  [7e2434f1] xyzVector v0.2.1 `C:\Users\bakerar\Documents\2020\Julia\development\xyzVector`
  [37e2e46d] LinearAlgebra

Sorry if this is a dumb question. I thought using a polynomial fit here would make it easier for Zygote. Do I need to write it out by hand as a function? Do I need to create my own chainrules?

I think it might be due to a custom rule for evalpoly, see my comment on Github.
There I have described possible workarounds.

1 Like

Oh man, this stuff gets complicated fast. I need a course on how to define composite types to work with these differentiating engines. I think I have one that I created myself that also struggles with throwing errors. I find it hard to know what to include.

For the polynomial problem my work around was basically manually define the polynomial as a Tuple and then evaluate it with a custom function. I haven’t gotten the rest of that work to function with zygote but I think
I got past the polynomial problem by this simple workaround.

2 Likes