|
1 | 1 | # Optimization.jl |
2 | 2 |
|
3 | | -There are some solvers that are available in the Optimization.jl package directly without the need to install any of the solver wrappers. |
| 3 | +The Optimization.jl package provides the common interface for defining and solving optimization problems. All optimization solvers are provided through separate wrapper packages that need to be installed independently. |
4 | 4 |
|
5 | | -## Methods |
| 5 | +For a list of available solver packages, see the other pages in this section of the documentation. |
6 | 6 |
|
7 | | - - `LBFGS`: The popular quasi-Newton method that leverages limited memory BFGS approximation of the inverse of the Hessian. Through a wrapper over the [L-BFGS-B](https://users.iems.northwestern.edu/%7Enocedal/lbfgsb.html) fortran routine accessed from the [LBFGSB.jl](https://github.com/Gnimuc/LBFGSB.jl/) package. It directly supports box-constraints. |
| 7 | +Some commonly used solver packages include: |
8 | 8 |
|
9 | | - This can also handle arbitrary non-linear constraints through a Augmented Lagrangian method with bounds constraints described in 17.4 of Numerical Optimization by Nocedal and Wright. Thus serving as a general-purpose nonlinear optimization solver available directly in Optimization.jl. |
| 9 | +- [OptimizationLBFGSB.jl](@ref lbfgsb) - L-BFGS-B quasi-Newton method with box constraints |
| 10 | +- [OptimizationOptimJL.jl](@ref optim) - Wrappers for Optim.jl solvers |
| 11 | +- [OptimizationMOI.jl](@ref mathoptinterface) - MathOptInterface solvers |
| 12 | +- [OptimizationSophia.jl](@ref sophia) - Sophia optimizer for neural network training |
10 | 13 |
|
11 | | -```@docs |
12 | | -Optimization.Sophia |
13 | | -``` |
14 | | - |
15 | | -## Examples |
16 | | - |
17 | | -### Unconstrained rosenbrock problem |
18 | | - |
19 | | -```@example L-BFGS |
20 | | -
|
21 | | -using Optimization, OptimizationLBFGSB, Zygote |
22 | | -
|
23 | | -rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2 |
24 | | -x0 = zeros(2) |
25 | | -p = [1.0, 100.0] |
26 | | -
|
27 | | -optf = OptimizationFunction(rosenbrock, AutoZygote()) |
28 | | -prob = Optimization.OptimizationProblem(optf, x0, p) |
29 | | -sol = solve(prob, LBFGS()) |
30 | | -``` |
31 | | - |
32 | | -### With nonlinear and bounds constraints |
33 | | - |
34 | | -```@example L-BFGS |
35 | | -
|
36 | | -function con2_c(res, x, p) |
37 | | - res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1]) - 5] |
38 | | -end |
39 | | -
|
40 | | -optf = OptimizationFunction(rosenbrock, AutoZygote(), cons = con2_c) |
41 | | -prob = OptimizationProblem(optf, x0, p, lcons = [1.0, -Inf], |
42 | | - ucons = [1.0, 0.0], lb = [-1.0, -1.0], |
43 | | - ub = [1.0, 1.0]) |
44 | | -res = solve(prob, LBFGS(), maxiters = 100) |
45 | | -``` |
46 | | - |
47 | | -### Train NN with Sophia |
48 | | - |
49 | | -```@example Sophia |
50 | | -
|
51 | | -using Optimization, Lux, Zygote, MLUtils, Statistics, Plots, Random, ComponentArrays |
52 | | -
|
53 | | -x = rand(10000) |
54 | | -y = sin.(x) |
55 | | -data = MLUtils.DataLoader((x, y), batchsize = 100) |
56 | | -
|
57 | | -# Define the neural network |
58 | | -model = Chain(Dense(1, 32, tanh), Dense(32, 1)) |
59 | | -ps, st = Lux.setup(Random.default_rng(), model) |
60 | | -ps_ca = ComponentArray(ps) |
61 | | -smodel = StatefulLuxLayer{true}(model, nothing, st) |
62 | | -
|
63 | | -function callback(state, l) |
64 | | - state.iter % 25 == 1 && @show "Iteration: $(state.iter), Loss: $l" |
65 | | - return l < 1e-1 ## Terminate if loss is small |
66 | | -end |
67 | | -
|
68 | | -function loss(ps, data) |
69 | | - x_batch, y_batch = data |
70 | | - ypred = [smodel([x_batch[i]], ps)[1] for i in eachindex(x_batch)] |
71 | | - return sum(abs2, ypred .- y_batch) |
72 | | -end |
73 | | -
|
74 | | -optf = OptimizationFunction(loss, AutoZygote()) |
75 | | -prob = OptimizationProblem(optf, ps_ca, data) |
76 | | -
|
77 | | -res = Optimization.solve(prob, Optimization.Sophia(), callback = callback, epochs = 100) |
78 | | -``` |
| 14 | +For examples of using these solvers, please refer to their respective documentation pages. |
0 commit comments