# How to control Optimization.solve() termination smartly

**URL:** <https://discourse.julialang.org/t/how-to-control-optimization-solve-termination-smartly/132182>\
**Category:** General Usage\
**Tags:** question, optimization\
**Created:** [September 8, 2025, 2:08pm UTC](https://discourse.julialang.org/t/how-to-control-optimization-solve-termination-smartly/132182 "2025-09-08T14:08:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dameka](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@dameka](https://discourse.julialang.org/u/dameka)\
**Post date:** [September 8, 2025, 2:08pm UTC](https://discourse.julialang.org/t/how-to-control-optimization-solve-termination-smartly/132182/1 "2025-09-08T14:08:01Z")

</div>

I am using `Optimization.jl` to train a neural network. I’d like to be able to terminate the convergence based on the behavior of the loss as evaluated on a validation dataset, rather than just setting a limit for number of training iterations. I’m imagining that one might use the callback function for this, evaluating the model on the validation set (which may have to be a global variable to be accessible) and checking whether it’s increasing or decreasing. Has anyone implemented something like this, or are there `Optimization` tools for this I’m not seeing?

---

<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:** [September 12, 2025, 12:08pm UTC](https://discourse.julialang.org/t/how-to-control-optimization-solve-termination-smartly/132182/2 "2025-09-12T12:08:11Z")

</div>

Optimization.jl’s callbacks do this. You always return `false` to keep going, or `true` to stop the optimization. For example:

```julia-auto
callback = function (state, l; doplot = false) #callback function to observe training
    return l < 0.5
end

```

would make it halt when the loss is less than 0.5
