-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update 'Allow Optim.Options to pass to BAT.find_mode' to current main…
… branch
- Loading branch information
Showing
14 changed files
with
297 additions
and
9 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
*.jl.mem | ||
.ipynb_checkpoints | ||
Manifest.toml | ||
.vscode/settings.json |
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
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,101 @@ | ||
using BAT | ||
using Optim | ||
|
||
posterior = BAT.example_posterior() | ||
|
||
optalg = OptimAlg(; | ||
optalg = Optim.NelderMead(parameters=Optim.FixedParameters()), | ||
maxiters=200, | ||
kwargs = (f_calls_limit=100,), | ||
) | ||
|
||
my_mode = bat_findmode(posterior, optalg) | ||
|
||
fieldnames(typeof(my_mode.info.res)) | ||
|
||
|
||
using BAT | ||
#using Optim | ||
#using Optimization | ||
using OptimizationOptimJL | ||
|
||
using InverseFunctions, FunctionChains, DensityInterface | ||
|
||
|
||
posterior = BAT.example_posterior() | ||
optalg = OptimizationAlg(; optalg = OptimizationOptimJL.ParticleSwarm(n_particles=10), maxiters=200, kwargs=(f_calls_limit=500,)) | ||
my_result = bat_findmode(posterior, optalg) | ||
|
||
a = my_result.info | ||
|
||
@test a.cache.solver_args.maxiters == 500 | ||
|
||
dump(a.alg) | ||
|
||
fieldnames(typeof(a.cache.solver_args)) | ||
|
||
fieldnames(typeof(a.original.method)) | ||
|
||
my_mode.info.original | ||
|
||
|
||
|
||
|
||
# Define a NamedTuple with keyword arguments | ||
nt = (a=1, b=2) | ||
|
||
# Define a function that accepts keyword arguments | ||
function my_function(; a=0, b=0, c=0) | ||
println("a = $a") | ||
println("b = $b") | ||
println("c = $c") | ||
end | ||
|
||
# Call the function and unpack the NamedTuple | ||
my_function(; nt...) | ||
|
||
|
||
|
||
|
||
context = get_batcontext() | ||
target = posterior | ||
transformed_density, trafo = BAT.transform_and_unshape(PriorToGaussian(), target, context) | ||
inv_trafo = inverse(trafo) | ||
initalg = BAT.apply_trafo_to_init(trafo, InitFromTarget()) | ||
x_init = collect(bat_initval(transformed_density, initalg, context).result) | ||
|
||
f = fchain(inv_trafo, logdensityof(target), -) | ||
f2 = (x, p) -> f(x) | ||
|
||
|
||
optimization_function = Optimization.OptimizationFunction(f2, Optimization.SciMLBase.NoAD()) | ||
optimization_problem = Optimization.OptimizationProblem(optimization_function, x_init) | ||
optimization_result = Optimization.solve(optimization_problem,OptimizationOptimJL.NelderMead()) | ||
|
||
|
||
optalg = OptimizationAlg(;optalg = OptimizationOptimJL.NelderMead()) | ||
my_mode = bat_findmode(posterior, optalg) | ||
|
||
my_mode.info.original | ||
fieldnames(typeof(my_mode.info)) | ||
|
||
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2 | ||
f = rosenbrock | ||
|
||
|
||
using AutoDiffOperators | ||
|
||
b = Optimization.SciMLBase.NoAD() | ||
supertype(typeof(b)) | ||
|
||
adm = ADModule(:ForwardDiff) | ||
|
||
adsel = BAT.get_adselector(context) | ||
supertype(typeof(adsel)) | ||
|
||
|
||
|
||
adm2 = convert_ad(ADTypes.AbstractADType, adm) | ||
ADTypes.AutoForwardDiff() | ||
|
||
optimization_function = Optimization.OptimizationFunction(f2, adm2) |
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,72 @@ | ||
# This file is a part of BAT.jl, licensed under the MIT License (MIT). | ||
|
||
module BATOptimizationExt | ||
|
||
@static if isdefined(Base, :get_extension) | ||
import Optimization | ||
else | ||
import ..Optimization | ||
end | ||
|
||
using BAT | ||
BAT.pkgext(::Val{:Optimization}) = BAT.PackageExtension{:Optimization}() | ||
|
||
|
||
using Random | ||
using DensityInterface, ChangesOfVariables, InverseFunctions, FunctionChains | ||
using HeterogeneousComputing, AutoDiffOperators | ||
using StructArrays, ArraysOfArrays, ADTypes | ||
|
||
using BAT: MeasureLike | ||
|
||
using BAT: get_context, get_adselector, _NoADSelected | ||
using BAT: bat_initval, transform_and_unshape, apply_trafo_to_init | ||
# using BAT: negative #deprecated? | ||
|
||
|
||
AbstractModeEstimator(optalg::Any) = OptimizationAlg(optalg) | ||
convert(::Type{AbstractModeEstimator}, alg::OptimizationAlg) = alg.optalg | ||
|
||
BAT.ext_default(::BAT.PackageExtension{:Optimization}, ::Val{:DEFAULT_OPTALG}) = nothing #Optim.NelderMead() | ||
|
||
|
||
function build_optimizationfunction(f, adsel::AutoDiffOperators.ADSelector) | ||
adm = convert_ad(ADTypes.AbstractADType, adsel) | ||
optimization_function = Optimization.OptimizationFunction(f, adm) | ||
return optimization_function | ||
end | ||
|
||
function build_optimizationfunction(f, adsel::BAT._NoADSelected) | ||
optimization_function = Optimization.OptimizationFunction(f) | ||
return optimization_function | ||
end | ||
|
||
|
||
function BAT.bat_findmode_impl(target::MeasureLike, algorithm::OptimizationAlg, context::BATContext) | ||
transformed_density, trafo = transform_and_unshape(algorithm.trafo, target, context) | ||
inv_trafo = inverse(trafo) | ||
|
||
initalg = apply_trafo_to_init(trafo, algorithm.init) | ||
x_init = collect(bat_initval(transformed_density, initalg, context).result) | ||
|
||
# Maximize density of original target, but run in transformed space, don't apply LADJ: | ||
f = fchain(inv_trafo, logdensityof(target), -) | ||
target_f = (x, p) -> f(x) | ||
|
||
adsel = get_adselector(context) | ||
|
||
optimization_function = build_optimizationfunction(target_f, adsel) | ||
optimization_problem = Optimization.OptimizationProblem(optimization_function, x_init) | ||
|
||
algopts = (maxiters = algorithm.maxiters, maxtime = algorithm.maxtime, abstol = algorithm.abstol, reltol = algorithm.reltol) | ||
optimization_result = Optimization.solve(optimization_problem, algorithm.optalg; algopts..., algorithm.kwargs...) | ||
|
||
transformed_mode = optimization_result.u | ||
result_mode = inv_trafo(transformed_mode) | ||
|
||
(result = result_mode, result_trafo = transformed_mode, trafo = trafo, info = optimization_result) | ||
end | ||
|
||
|
||
|
||
end # module BATOptimizationExt |
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
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,36 @@ | ||
# This file is a part of BAT.jl, licensed under the MIT License (MIT). | ||
|
||
""" | ||
OptimizationAlg | ||
Selects an optimization algorithm from the | ||
[Optimization.jl](https://github.com/SciML/Optimization.jl) | ||
package. | ||
Note that when using first order algorithms like `OptimizationOptimJL.LBFGS`, your | ||
[`BATContext`](@ref) needs to include an `ADSelector` that specifies | ||
which automatic differentiation backend should be used. | ||
Constructors: | ||
* ```$(FUNCTIONNAME)(; fields...)``` | ||
`optalg` must be an `Optimization.AbstractOptimizer`. | ||
The field `kwargs` can be used to pass additional keywords to the optimizers | ||
See the [Optimization.jl documentation](https://docs.sciml.ai/Optimization/stable/) for the available keyword arguments. | ||
Fields: | ||
$(TYPEDFIELDS) | ||
!!! note | ||
This algorithm is only available if the `Optimization` package or any of its submodules, like `OptimizationOptimJL`, is loaded (e.g. via | ||
`import Optimization`). | ||
""" | ||
@with_kw struct OptimizationAlg{ | ||
ALG, | ||
TR<:AbstractTransformTarget, | ||
IA<:InitvalAlgorithm | ||
} <: AbstractModeEstimator | ||
optalg::ALG = ext_default(pkgext(Val(:Optimization)), Val(:DEFAULT_OPTALG)) | ||
trafo::TR = PriorToGaussian() | ||
init::IA = InitFromTarget() | ||
maxiters::Int64 = 1_000 | ||
maxtime::Float64 = NaN | ||
abstol::Float64 = NaN | ||
reltol::Float64 = 0.0 | ||
kwargs::NamedTuple = (;) | ||
end | ||
export OptimizationAlg |
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.