# How to use Flux for a general non-linear minimization

**URL:** <https://discourse.julialang.org/t/how-to-use-flux-for-a-general-non-linear-minimization/24111>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [May 11, 2019, 3:38pm UTC](https://discourse.julialang.org/t/how-to-use-flux-for-a-general-non-linear-minimization/24111 "2019-05-11T15:38:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [May 11, 2019, 3:38pm UTC](https://discourse.julialang.org/t/how-to-use-flux-for-a-general-non-linear-minimization/24111/1 "2019-05-11T15:38:14Z")

</div>

1. I am trying to apply great features `Flux` for likelihood minimization problem.  
Using ML language, losses for all training data points are summed up,  
there is just a function `LLH()` to be minimized. Is there an easy hack to `train!` functions that can help me?

```julia
using Flux
using Flux.Tracker
#
const data = [randn() for _ in 1:1000]
μ = param(0.5)
σ = param(1.1)
model(x) = 1/(sqrt(2π)*σ)*exp(-(x-μ)^2/(2*σ^2))
LLH() = sum(-log.(model.(data)))
# grad also works
gs = Tracker.gradient(LLH, Params([μ,σ]))
gs[μ], gs[σ]
# now I need to have an infinite training loop until convergence

```

1. I like very much [`NLopt`](https://github.com/JuliaOpt/NLopt.jl) minimizer with `LD_LBFGS` algorithm.  
Any suggestions on how to interface to it? (customary `apply` function?)

Many thanks.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 1, 2019, 9:58am UTC](https://discourse.julialang.org/t/how-to-use-flux-for-a-general-non-linear-minimization/24111/2 "2019-07-01T09:58:52Z")

</div>

You can easily train a flux model using Optims LBFGS with the help of  
[https://github.com/baggepinnen/FluxOptTools.jl](https://github.com/baggepinnen/FluxOptTools.jl)
