I have a function with three inputs and two outputs. The first argument is a vector, the two others are scalars. The function is built from smaller functions. I want to get derivatives of the composite function with respect to the second argument (so scalar). For that I am using jacobian
from Zygote. However, I get different results from jacobian
that I would obtain manually. What’s wrong?
using Zygote
f1 = (u,β) -> [1 -1+3*(u[2].^2)]
f2 = (u,β) -> f1(u,β) .+ β
f3 = (u,β,y) -> [ 3.0*u[1]+u[2] -3+u[1]+2*u[2]-3*(u[2]^2) 1 ]
f4 = (u,β) -> [I f2(u,β)']
fImportant = (u,β,y) -> 2* f4(u,β)*transpose(f3(u,β,y))
ftest =x -> fImportant([0,0],x,0.5)
case1 = jacobian(x->ftest(x),0.0)
fManualtest = (bb)-> [2+2*bb;-8+2*bb]
case2 = jacobian(x->fManualtest(x),0.0)
xx=range(-1,1,1000)
norm(ftest.(xx)-fManualtest.(xx)) ######Checking that the manual function is actually correct
It seems that Zygote had some issues with jacobians but the topic was closed, so I am assuming the issue was solved. I am using Zygote v0.6.71.
Using ForwardDiff works:
using ForwardDiff
ftest =x -> fImportant([0,0],x,0.5)
case1 = ForwardDiff.derivative(x->ftest(x),0.0)
fManualtest = (bb)-> [2+2*bb;-8+2*bb]
case2 = ForwardDiff.derivative(x->fManualtest(x),0.0)