# Optim.jl: running instructions after each step

**URL:** https://discourse.julialang.org/t/optim-jl-running-instructions-after-each-step/33969
**Category:** General Usage
**Created:** [January 30, 2020, 8:36am UTC](https://discourse.julialang.org/t/optim-jl-running-instructions-after-each-step/33969 "2020-01-30T08:36:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rpetit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rpetit/32/12552_2.png) [@rpetit](https://discourse.julialang.org/u/rpetit)
#### Post date: [January 30, 2020, 8:36am UTC](https://discourse.julialang.org/t/optim-jl-running-instructions-after-each-step/33969/1 "2020-01-30T08:36:46Z")

</div>

Hi !

I would like to use Optim.jl to perform a gradient descent, and for some reason would like after each step to be able to run some instruction, possibly modifying the current iterate. Does anyone know if this is possible and how to do such thing ?

Thanks in advance !

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 30, 2020, 1:09pm UTC](https://discourse.julialang.org/t/optim-jl-running-instructions-after-each-step/33969/2 "2020-01-30T13:09:27Z")

</div>

> [@rpetit](#):
>
> after each step to be able to run some instruction, possibly modifying the current iterate

AFAICT this is not part of the exposed API, but I imagine you could iterate on [`update_state!`](https://github.com/JuliaNLSolvers/Optim.jl/blob/39cce4d6024ea13182b2ef416b07b43bf879de73/src/multivariate/solvers/first_order/gradient_descent.jl#L62) and do the transformation after each step.

You have to dig into the source code a bit though, and these functions can of course change without warning in each version.

---

<div class="post-metadata">

### Author: ![rpetit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rpetit/32/12552_2.png) [@rpetit](https://discourse.julialang.org/u/rpetit)
#### Post date: [January 30, 2020, 3:12pm UTC](https://discourse.julialang.org/t/optim-jl-running-instructions-after-each-step/33969/3 "2020-01-30T15:12:28Z")

</div>

Thanks for you reply ! I’ll dig into that shortly. Someone actually suggested on Optim.jl’s gitter to use the retract method which was introduced for performing manifold optimization, I’ll see if this actually allows to avoid using non-exposed code.
