Skip to content

Commit 3272807

Browse files
SebastianM-Cclaude
andcommitted
Split docs for LBFGSB and Sophia
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 90a3f88 commit 3272807

File tree

3 files changed

+112
-72
lines changed

3 files changed

+112
-72
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# OptimizationLBFGSB.jl
2+
3+
[`OptimizationLBFGSB.jl`](https://github.com/SciML/Optimization.jl/tree/master/lib/OptimizationLBFGSB) is a package that wraps the [L-BFGS-B](https://users.iems.northwestern.edu/%7Enocedal/lbfgsb.html) fortran routine via the [LBFGSB.jl](https://github.com/Gnimuc/LBFGSB.jl/) package.
4+
5+
## Installation
6+
7+
To use this package, install the `OptimizationLBFGSB` package:
8+
9+
```julia
10+
using Pkg
11+
Pkg.add("OptimizationLBFGSB")
12+
```
13+
14+
## Methods
15+
16+
- `LBFGSB`: The popular quasi-Newton method that leverages limited memory BFGS approximation of the inverse of the Hessian. It directly supports box-constraints.
17+
18+
This can also handle arbitrary non-linear constraints through an 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.
19+
20+
```@docs
21+
OptimizationLBFGSB.LBFGSB
22+
```
23+
24+
## Examples
25+
26+
### Unconstrained rosenbrock problem
27+
28+
```@example LBFGSB
29+
using OptimizationBase, OptimizationLBFGSB, ADTypes, Zygote
30+
31+
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
32+
x0 = zeros(2)
33+
p = [1.0, 100.0]
34+
35+
optf = OptimizationFunction(rosenbrock, ADTypes.AutoZygote())
36+
prob = OptimizationProblem(optf, x0, p)
37+
sol = solve(prob, LBFGSB())
38+
```
39+
40+
### With nonlinear and bounds constraints
41+
42+
```@example LBFGSB
43+
function con2_c(res, x, p)
44+
res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1]) - 5]
45+
end
46+
47+
optf = OptimizationFunction(rosenbrock, ADTypes.AutoZygote(), cons = con2_c)
48+
prob = OptimizationProblem(optf, x0, p, lcons = [1.0, -Inf],
49+
ucons = [1.0, 0.0], lb = [-1.0, -1.0],
50+
ub = [1.0, 1.0])
51+
res = solve(prob, LBFGSB(), maxiters = 100)
52+
```
Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,14 @@
11
# Optimization.jl
22

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.
44

5-
## Methods
5+
For a list of available solver packages, see the other pages in this section of the documentation.
66

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:
88

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
1013

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.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# OptimizationSophia.jl
2+
3+
[`OptimizationSophia.jl`](https://github.com/SciML/Optimization.jl/tree/master/lib/OptimizationSophia) is a package that provides the Sophia optimizer for neural network training.
4+
5+
## Installation
6+
7+
To use this package, install the `OptimizationSophia` package:
8+
9+
```julia
10+
using Pkg
11+
Pkg.add("OptimizationSophia")
12+
```
13+
14+
## Methods
15+
16+
```@docs
17+
OptimizationSophia.Sophia
18+
```
19+
20+
## Examples
21+
22+
### Train NN with Sophia
23+
24+
```@example Sophia
25+
using OptimizationBase, OptimizationSophia, Lux, ADTypes, Zygote, MLUtils, Statistics, Random, ComponentArrays
26+
27+
x = rand(10000)
28+
y = sin.(x)
29+
data = MLUtils.DataLoader((x, y), batchsize = 100)
30+
31+
# Define the neural network
32+
model = Chain(Dense(1, 32, tanh), Dense(32, 1))
33+
ps, st = Lux.setup(Random.default_rng(), model)
34+
ps_ca = ComponentArray(ps)
35+
smodel = StatefulLuxLayer{true}(model, nothing, st)
36+
37+
function callback(state, l)
38+
state.iter % 25 == 1 && @show "Iteration: $(state.iter), Loss: $l"
39+
return l < 1e-1 ## Terminate if loss is small
40+
end
41+
42+
function loss(ps, data)
43+
x_batch, y_batch = data
44+
ypred = [smodel([x_batch[i]], ps)[1] for i in eachindex(x_batch)]
45+
return sum(abs2, ypred .- y_batch)
46+
end
47+
48+
optf = OptimizationFunction(loss, ADTypes.AutoZygote())
49+
prob = OptimizationProblem(optf, ps_ca, data)
50+
51+
res = solve(prob, OptimizationSophia.Sophia(), callback = callback, epochs = 100)
52+
```

0 commit comments

Comments
 (0)