# Composing Lux with Optim Algorithms

**URL:** <https://discourse.julialang.org/t/composing-lux-with-optim-algorithms/101846>\
**Category:** General Usage\
**Created:** [July 20, 2023, 6:18pm UTC](https://discourse.julialang.org/t/composing-lux-with-optim-algorithms/101846 "2023-07-20T18:18:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Stepaniants](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george_stepaniants/32/47230_2.png) [@George\_Stepaniants](https://discourse.julialang.org/u/George_Stepaniants)\
**Post date:** [July 20, 2023, 6:18pm UTC](https://discourse.julialang.org/t/composing-lux-with-optim-algorithms/101846/1 "2023-07-20T18:18:14Z")

</div>

Is there a way to use optimization algorithms from Optim to train Lux neural networks and neural ODEs? In particular, I would like to use LBFGS to train a Lux neural network but don’t see a simple way to do this. There is a library called FluxOptimTools that allows us to do this in Flux.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [July 20, 2023, 6:37pm UTC](https://discourse.julialang.org/t/composing-lux-with-optim-algorithms/101846/2 "2023-07-20T18:37:43Z")

</div>

See [Neural Ordinary Differential Equations · DiffEqFlux.jl](https://docs.sciml.ai/DiffEqFlux/stable/examples/neural_ode/). You can essentially use the Optimization.jl API to train Lux using Optim (and several other optimization packages out there)

---

<div class="post-metadata">

**Author:** ![George\_Stepaniants](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george_stepaniants/32/47230_2.png) [@George\_Stepaniants](https://discourse.julialang.org/u/George_Stepaniants)\
**Post date:** [July 20, 2023, 6:50pm UTC](https://discourse.julialang.org/t/composing-lux-with-optim-algorithms/101846/3 "2023-07-20T18:50:45Z")

</div>

I see, but there is no way to write a general training loop from which we could call Optimisers.update with an algorithm like LBFGS.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [July 20, 2023, 7:05pm UTC](https://discourse.julialang.org/t/composing-lux-with-optim-algorithms/101846/4 "2023-07-20T19:05:27Z")

</div>

`Optim.jl` is completely distinct from `Optimisers.jl` (maintained by different groups of people). Optim.jl doesn’t provide a way to apply updates like Optimisers sequentially. But in my experience, you can always use callbacks to do most things you need from a custom training loop.
