# Need some help with FluxOpt

**URL:** <https://discourse.julialang.org/t/need-some-help-with-fluxopt/74904>\
**Category:** Machine Learning\
**Tags:** optim, flux, optimization, neural-network\
**Created:** [January 20, 2022, 4:56am UTC](https://discourse.julialang.org/t/need-some-help-with-fluxopt/74904 "2022-01-20T04:56:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stochastic\_guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stochastic_guy/32/9877_2.png) [@stochastic\_guy](https://discourse.julialang.org/u/stochastic_guy)\
**Post date:** [January 20, 2022, 4:56am UTC](https://discourse.julialang.org/t/need-some-help-with-fluxopt/74904/1 "2022-01-20T04:56:29Z")

</div>

I am trying to use the `FluxOptTools` ([https://github.com/baggepinnen/FluxOptTools.jl](https://github.com/baggepinnen/FluxOptTools.jl)) to train a Flux model using `Optim` . I followed the example provided in the readme. It is working as intended. But when I slightly changed it to my case it isn’t working properly. The code is running without errors, but the parameters of the model are not getting updated. Can anyone please help me figure out what I am doing wrong?  
Here is my code:

```nohighlight
using Pkg
Pkg.activate(".")
Pkg.add(["DataFrames", "RDatasets","Flux", "FluxOptTools", "Zygote", "Optim", "LossFunctions"])
using DataFrames
using RDatasets
using Flux, Zygote, Optim, FluxOptTools, Statistics
using LossFunctions
diabetes = dataset("MASS", "Pima.te")
y_df = diabetes[!,:Type] .== "Yes"
X_df = diabetes[!, Not(:Type)]
# Converting X and y into matrices and vectors 
y = vec(y_df)'
X = Matrix(Matrix(X_df)')

m = Chain(Dense(7,20),    
                Dense(20,50),
                Dense(50,10),
                Dense(10,1,sigmoid)
                )
loss() = mean(value(PerceptronLoss(),m(X),y))

Zygote.refresh()
pars = Flux.params(m) # Initializing parameters 
initial_par = pars
lossfun, gradfun, fg!, p0 = optfuns(loss, pars)
res = Optim.optimize(Optim.only_fg!(fg!), p0, LBFGS() ,Optim.Options(show_trace=true))

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 20, 2022, 6:59pm UTC](https://discourse.julialang.org/t/need-some-help-with-fluxopt/74904/2 "2022-01-20T18:59:36Z")

</div>

Since `y>=0` and `m(X)>0`, their product will always be `>=0`, so `PerceptronLoss` will always be zero. Maybe `L1HingeLoss` would be more appropriate for this problem? I tried this:

```
loss() = value(L1HingeLoss(), y, m(X), AggMode.Mean())

```

But that results in `NaN` in the convergence measures and I couldn’t figure out how to fix that.
