Julia DiffResults examples?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Julia DiffResults examples?



Julia's ForwardDiff documentation suggests that computing the function value, gradient and Hessian can be computed in one fell swoop using the DiffResults API, but there are NO examples. The DiffResults package itself has no examples either, and no documentation to speak of. The use case for this is self-evident: suppose I have a function f of a vector argument x, and I want to minimize it using Newton's method. Below is the blunt approach, where things get recomputed three times - how would I write it with DiffResults?


ForwardDiff


DiffResults


DiffResults


f


x


DiffResults


function NewtMin(f, x0, eps)
fgrad = x-> ForwardDiff.gradient(f, x)
fhess = x-> ForwardDiff.hessian(f, x)
oldval = f(x0)
newx = x0 - fhess(x0)fgrad(x0)
newval = f(newx)
while abs(newval - oldval) > eps
oldval = newval
newx = newx - fhess(newx)fgrad(newx)
newval = f(newx)
end
return newx
end




1 Answer
1



There are examples in the DiffResults.jl documentation from http://www.juliadiff.org/DiffResults.jl/stable/.


DiffResults.jl



And this is a simple rewriting of Newtmin using DiffResults, it works in julia v0.6.4. But I guess it can be refactored and optimized to be more elegant and performant.


Newtmin


DiffResults


using ForwardDiff
using DiffResults

function NewtMin(f, x0, eps)
result = DiffResults.HessianResult(x0)
ForwardDiff.hessian!(result, f, x0)
fhess_x0 = DiffResults.hessian(result)
fgrad_x0 = DiffResults.gradient(result)
oldval = DiffResults.value(result)
newx = x0 - fhess_x0fgrad_x0
newval = f(newx)
while abs(newval - oldval) > eps
oldval = newval
ForwardDiff.hessian!(result, f, newx)
fhess_newx = DiffResults.hessian(result)
fgrad_newx = DiffResults.gradient(result)
newx = newx - fhess_newxfgrad_newx
newval = f(newx)
end
return newx
end

foo(x) = sum(x.^2)

NewtMin(foo, [1.,1.,1.], 0.01)
## which should give a correct result at [0., 0., 0.]





Thanks! That was easy enough!
– Igor Rivin
Aug 13 at 16:10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard