Optimizing a Loss Function that takes arguments in addition to parameters

If I have a loss function that I want to use for optimizing a set of parameters, can that function take additional arguments as well (e.g., arrays that might vary in size, and boolean statements)?

If the answer is “yes”, how do I pass these other arguments to Optim.optimize?

This is done using a closure, for example,
obj = θ -> -1.0*H(θ, θnn, 10, auxstat, NNmodel, info)
The free argument θ is what is optimized over, and the others are fixed. Then you call an optimizer in Optim.jl doing something like
θsa = (Optim.optimize(obj, model.lb, model.ub, θnn, SAMIN(rt=rt, verbosity=sa_verbosity),Optim.Options(iterations=10^6))).minimizer

There is a nice tutorial at JuliaTutorial/Tutorial_22_Optimization1.ipynb at master · PaulSoderlind/JuliaTutorial · GitHub

2 Likes

Thanks so much, will take a look.

I think this is exactly what I was looking for.