# Why do we need "GalacticOptim.instantiate\_function"?

**URL:** <https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760>\
**Category:** Modelling & Simulations\
**Tags:** optimization, diffeqflux\
**Created:** [May 27, 2022, 3:52am UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760 "2022-05-27T03:52:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzhenirovskyy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzhenirovskyy/32/7549_2.png) [@mzhenirovskyy](https://discourse.julialang.org/u/mzhenirovskyy)\
**Post date:** [May 27, 2022, 3:52am UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/1 "2022-05-27T03:52:42Z")

</div>

This example seems to work even without “instantiate\_function”.  
[https://diffeqflux.sciml.ai/stable/examples/neural\_ode\_galacticoptim/](https://diffeqflux.sciml.ai/stable/examples/neural_ode_galacticoptim/)

I can’t find any docs about “instantiate\_function”.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [May 27, 2022, 8:01am UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/2 "2022-05-27T08:01:49Z")

</div>

really weird, specially because `instantiate_function` returns itself an `OptimizationFunction`:

> <https://github.com/SciML/Optimization.jl/blob/master/src/function/function.jl>

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 27, 2022, 12:09pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/3 "2022-05-27T12:09:18Z")

</div>

It builds the closures required for the calls to exist without the `p` parameters as part of it, which is required by many of the optimizers. We should probably turn that into callable structs though to help with type inference.

---

<div class="post-metadata">

**Author:** ![mzhenirovskyy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzhenirovskyy/32/7549_2.png) [@mzhenirovskyy](https://discourse.julialang.org/u/mzhenirovskyy)\
**Post date:** [May 27, 2022, 7:46pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/4 "2022-05-27T19:46:43Z")

</div>

Thank you @ChrisRackauckas, do we have **instantiate\_function** documentation?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 27, 2022, 7:52pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/5 "2022-05-27T19:52:53Z")

</div>

No, you can make a docstring that says exactly what I said above here there. It’s an internal so it shouldn’t be in the documentation.

---

<div class="post-metadata">

**Author:** ![mzhenirovskyy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzhenirovskyy/32/7549_2.png) [@mzhenirovskyy](https://discourse.julialang.org/u/mzhenirovskyy)\
**Post date:** [May 27, 2022, 8:10pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/6 "2022-05-27T20:10:34Z")

</div>

Sorry to bother the community with the doc questions. I think that Julia is a great technology!!! But docs is one of my biggest stoppers to use Julia in production.

When I open the official example here:  
[https://diffeqflux.sciml.ai/stable/examples/neural\_ode\_galacticoptim/](https://diffeqflux.sciml.ai/stable/examples/neural_ode_galacticoptim/)

I see the the next line:  
`optfunc = GalacticOptim.instantiate_function(optf, prob_neuralode.p, adtype, nothing)`

I guess I need to follow the official guide and use “instantiate\_function” - it’s a reason why I’m asking about docs.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 27, 2022, 8:20pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/7 "2022-05-27T20:20:54Z")

</div>

Oh yes, I totally forgot about that. Yes, it’s used for the AD choices these days instead of the direct `OptimizationFunction` definition. [OptimizationFunction · GalacticOptim.jl](https://galacticoptim.sciml.ai/dev/API/optimization_function/) needs to get updated, and we should turn the sections into docstrings on the dispatches. Could you help with that?

That one dispatch is just internal for wrapping the closures though and should be omitted from the docs, just get a quick comment if it’s sufficiently non-obvious.

We also need to fill out [SciMLBase.jl/scimlfunctions.jl at master · SciML/SciMLBase.jl · GitHub](https://github.com/SciML/SciMLBase.jl/blob/master/src/scimlfunctions.jl#L1485), and I pinged @Vaibhavdixit02 for that one.

---

<div class="post-metadata">

**Author:** ![mzhenirovskyy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzhenirovskyy/32/7549_2.png) [@mzhenirovskyy](https://discourse.julialang.org/u/mzhenirovskyy)\
**Post date:** [May 27, 2022, 9:51pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/8 "2022-05-27T21:51:05Z")

</div>

@ChrisRackauckas  
I’m not sure I have a full understanding, but I created the pull request to fix that doc.  
[https://github.com/SciML/GalacticOptim.jl/pull/259/commits/73dee4d1348f671260362b604c9a1a9cc4b02703](https://github.com/SciML/GalacticOptim.jl/pull/259/commits/73dee4d1348f671260362b604c9a1a9cc4b02703)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 28, 2022, 12:37pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/9 "2022-05-28T12:37:55Z")

</div>

Oh I see the issue. Yeah that was an old interface break.

```julia
# use GalacticOptim.jl to solve the problem
adtype = GalacticOptim.AutoZygote()
optf = GalacticOptim.OptimizationFunction((x, p) -> loss_neuralode(x), adtype)
optprob = GalacticOptim.OptimizationProblem(optf, prob_neuralode.p)

result_neuralode = GalacticOptim.solve(optprob,
                                       ADAM(0.05),
                                       callback = callback,
                                       maxiters = 300)

optprob2 = remake(optprob,u0 = result_neuralode.u)

result_neuralode2 = GalacticOptim.solve(optprob2,
                                        LBFGS(),
                                        callback = callback,
                                        allow_f_increases = false)

```

is the right code. `GalacticOptim.instantiate_function` shouldn’t be called by the user, so that tutorial was just doing weird stuff. I updated the tutorial and so now it should be more clearly only using the documented interface.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 28, 2022, 3:58pm UTC](https://discourse.julialang.org/t/why-do-we-need-galacticoptim-instantiate-function/81760/10 "2022-05-28T15:58:28Z")

</div>

I fixed the DfifEqFlux.jl documentation on that, and redid the OptimizationFunction documentation to make this more clear:

[https://github.com/SciML/GalacticOptim.jl/pull/261](https://github.com/SciML/GalacticOptim.jl/pull/261)
