-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support all GalacticOptim-compatible optimizers (#25)
* Add GalacticOptim as dependency * Use GalacticOptim API * Add comment about recomputing gradient * Add ForwardDiff as dependency * Refactor single-path pathfinder * Rearrange imports * Test optimization functions * Update tests * Increment version number * Document compatible optimizers * Copy positions just in case * Test NLopt works * Rename files * Clarify where remaining kwargs are forwarded * Increment minimum supported version to LTS * Test minimum supported version * Standardize use of params * Require grad function is specified * Add AbstractDifferentiation as dependency * Use AD backend to create optimization function * Improve optimization tests * Test AD backend functionality * Include AD * Use more compact keyword syntax * Provide nothing for parameters * Rearrange code * Support passing OptimizationFunction to multi-path pathfinder * Docstring formatting * Improve documentation * Test ad_backend * Mark version number as DEV
- Loading branch information
Showing
15 changed files
with
344 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
version: | ||
- '1.6' | ||
- '1' | ||
os: | ||
- ubuntu-latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
name = "Pathfinder" | ||
uuid = "b1d3bc72-d0e7-4279-b92f-7fa5d6d2d454" | ||
authors = ["Seth Axen <[email protected]> and contributors"] | ||
version = "0.2.3" | ||
version = "0.3.0-DEV" | ||
|
||
[deps] | ||
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d" | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
GalacticOptim = "a75be94c-b780-496d-a8a9-0878b188d577" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Optim = "429524aa-4258-5aef-a3af-852621145aeb" | ||
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150" | ||
|
@@ -15,17 +18,21 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" | |
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" | ||
|
||
[compat] | ||
AbstractDifferentiation = "0.4" | ||
Distributions = "0.25" | ||
ForwardDiff = "0.10" | ||
GalacticOptim = "2" | ||
Optim = "1.4" | ||
PDMats = "0.11" | ||
PSIS = "0.2" | ||
StatsBase = "0.33" | ||
StatsFuns = "0.9" | ||
julia = "1" | ||
julia = "1.6" | ||
|
||
[extras] | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd" | ||
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["ForwardDiff", "Test"] | ||
test = ["NLopt", "ReverseDiff", "Test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
function build_optim_function(f; ad_backend=AD.ForwardDiffBackend()) | ||
∇f(x) = only(AD.gradient(ad_backend, f, x)) | ||
return build_optim_function(f, ∇f; ad_backend) | ||
end | ||
function build_optim_function(f, ∇f; ad_backend=AD.ForwardDiffBackend()) | ||
# because we need explicit access to grad, we generate these ourselves instead of using | ||
# GalacticOptim's auto-AD feature. | ||
# TODO: switch to caching API if available, see | ||
# https://github.com/JuliaDiff/AbstractDifferentiation.jl/issues/41 | ||
function grad(res, x, p...) | ||
∇fx = ∇f(x) | ||
@. res = -∇fx | ||
return res | ||
end | ||
function hess(res, x, p...) | ||
H = only(AD.hessian(ad_backend, f, x)) | ||
@. res = -H | ||
return res | ||
end | ||
function hv(res, x, v, p...) | ||
Hv = only(AD.lazy_hessian(ad_backend, f, x) * v) | ||
@. res = -Hv | ||
return res | ||
end | ||
return GalacticOptim.OptimizationFunction((x, p...) -> -f(x); grad, hess, hv) | ||
end | ||
|
||
function build_optim_problem(optim_fun, x₀; kwargs...) | ||
return GalacticOptim.OptimizationProblem(optim_fun, x₀, nothing; kwargs...) | ||
end | ||
|
||
function optimize_with_trace(prob, optimizer) | ||
u0 = prob.u0 | ||
fun = prob.f | ||
grad! = fun.grad | ||
function ∇f(x) | ||
∇fx = similar(x) | ||
grad!(∇fx, x, nothing) | ||
rmul!(∇fx, -1) | ||
return ∇fx | ||
end | ||
# caches for the trace of x, f(x), and ∇f(x) | ||
xs = typeof(u0)[] | ||
fxs = typeof(fun.f(u0, nothing))[] | ||
∇fxs = typeof(similar(u0))[] | ||
function callback(x, nfx, args...) | ||
# NOTE: GalacticOptim doesn't have an interface for accessing the gradient trace, | ||
# so we need to recompute it ourselves | ||
# see https://github.com/SciML/GalacticOptim.jl/issues/149 | ||
∇fx = ∇f(x) | ||
# terminate if optimization encounters NaNs | ||
(isnan(nfx) || any(isnan, x) || any(isnan, ∇fx)) && return true | ||
# some backends mutate x, so we must copy it | ||
push!(xs, copy(x)) | ||
push!(fxs, -nfx) | ||
push!(∇fxs, ∇fx) | ||
return false | ||
end | ||
GalacticOptim.solve(prob, optimizer; cb=callback) | ||
return xs, fxs, ∇fxs | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.