项目作者: magerton

项目描述 :
Numerical check on analytic gradient + hessian of function.
高级语言: Julia
项目地址: git://github.com/magerton/FiniteDiffCheck.jl.git
创建时间: 2017-09-26T13:06:42Z
项目社区:https://github.com/magerton/FiniteDiffCheck.jl

开源协议:Other

下载


FiniteDiffCheck

Build Status

Coverage Status

codecov.io

Checks analytic vs finite difference gradient + hessian using central differencing.
While the Calculus.jl package does
lots more & takes one-sided differences, it won’t do Jacobians (though Calculus2.jl does). Also, FiniteDiffCheck.jl can use AbstractVectors like MVector from StaticArrays.jl.

Example:

  1. using FiniteDiffCheck
  2. function fgh!(x::AbstractVector, dx::AbstractVector, d2x::AbstractMatrix)
  3. length(x) == 2 || throw(DimensionMismatch())
  4. f = x[1]^2. + 3.*x[1]*x[2] + x[2]^2.
  5. if length(dx) > 0
  6. dx[1] = 2.*x[1] + 3.*x[2]
  7. dx[2] = 3.*x[1] + 2.*x[2]
  8. end
  9. if length(d2x) > 0
  10. d2x[1,1] = 2.
  11. d2x[2,1] = 3.
  12. d2x[1,2] = 3.
  13. d2x[2,2] = 2.
  14. end
  15. return f
  16. end
  17. θ0 = rand(2)
  18. # when defining tmp struct
  19. tmp = jacchecktmp0)
  20. @show out = jac_hess_check!(tmp, fgh!, θ0)
  21. @show all(out .< 3e-10)
  22. # without
  23. @show jac_hess_check(fgh!, θ0)