Hi all. I have a (relatively) small question regarding automatic differentiation packages.
I have a function that outputs a Hessian and the corresponding Laplacian. I want to take the gradient concerning the Laplacian. This is fine. However, I also want to output the Hessian. When using
val, back = Zygote.pullback() one can differentiate the Laplacian and get the value of the Laplacian. I am wondering whether it is also possible to output the Hessian. So far I am stuck.
Minimal working example (MWE):
using Zygote, LinearAlgebra # Just an arbtrary function function randomFunction(x) mat = [x^2 x^2; x^2 x^2] return mat, tr(mat) # The trace equals 2x^2 end # Testing whether the function works println(randomFunction(2.0)) println(randomFunction(2.0)) # Obviously, does not give an error and gives the right result val1, back1 = Zygote.pullback((x) -> randomFunction(x), 2.0) println(val1, back1(one(val1))) # This also gives the same gradient as before, which is weird to me val2, back2 = Zygote.pullback((x) -> randomFunction(x), 2.0) println(val2, back2(one(val2))) # This does not work val, back = Zygote.pullback((x) -> randomFunction(x), 2.0) back(one(val))
Summary of what I would like:
randomFunction function, I would like to take the gradient of the second output while also giving the first matrix output (and the second output).