Zygote.jl
Zygote.jl copied to clipboard
Get wrong third order derivative of Morse potential
Dear Flux community, Hi. Recently I use Flux.jl to calculate the Taylor expansion of the Morse potential function and find that it does not work properly.
The following code will give
using Flux
V(x)=(1-exp(1-x))^2
dV(x)=gradient(V,x)[1]
d2V(x)=gradient(dV,x)[1]
d3V(x)=gradient(d2V,x)[1]
println(d2V.(0:3))
# [24.11966073880451, 2.0, -0.19441774939643386, -0.19740801091828866]
println(d3V.(0:3))
# [-59.112448791445196, NaN, -1.0826822658929016, -0.14652511110987346]
while the Morse potential (V
in the code) should have third order derivatives everywhere.
I also use ForwardDiff.jl to calculate the same thing, which gives the right answer.
using ForwardDiff
V(x)=(1-exp(1-x[1]))^2
dV(x)=ForwardDiff.derivative(V,x)
d2V(x)=ForwardDiff.derivative(dV,x)
d3V(x)=ForwardDiff.derivative(d2V,x)
println(d2V.(0:3))
# [24.119660738804505, 2.0, -0.19441774939643386, -0.1974080109182887]
println(d3V.(0:3))
# [-53.67588513452711, -6.0, -0.34692338355001695, 0.12414545536335196]
The tests can be reproduced under both Windows and Linux, I used Julia 1.4.2 with Flux.jl v0.10.4.