# Number of times loss function is called

**URL:** <https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834>\
**Category:** Modelling & Simulations\
**Created:** [July 20, 2023, 12:32pm UTC](https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834 "2023-07-20T12:32:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_J](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t_j/32/51019_2.png) [@T\_J](https://discourse.julialang.org/u/T_J)\
**Post date:** [July 20, 2023, 12:32pm UTC](https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834/1 "2023-07-20T12:32:03Z")

</div>

Hi,

In the Lotka-Volterra system, I have placed:

```julia

function loss(θ)
    println("loss is called")
    X̂ = predict(θ)
    mean(abs2, Xₙ .- X̂)
end

losses = Float64[]

callback = function (p, l)
    push!(losses, l)
    if length(losses) % 10 == 0
        println("Current loss after $(length(losses)) iterations: $(losses[end])")
    end
    return false
end

adtype = Optimization.AutoZygote()
optf = Optimization.OptimizationFunction((x, p) -> loss(x), adtype)
optprob = Optimization.OptimizationProblem(optf, ComponentVector{Float64}(p))

res = Optimization.solve(optprob, ADAM(), callback = callback, maxiters = 10)

```

This is printing 20 times “loss is called”.

Why? If I am asking that `maxiters = 10` ?

Best Regards

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [July 20, 2023, 2:14pm UTC](https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834/2 "2023-07-20T14:14:54Z")

</div>

I noticed this behavior recently and had the same question. I suspect this is a result of the optimizer checking the gradient of the loss function. You might get a better sense of what’s going on under the hood by printing some additional information about the current value of `x` (or some subset of `x[]`) being passed to your `loss` function.

---

<div class="post-metadata">

**Author:** ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)\
**Post date:** [July 20, 2023, 2:25pm UTC](https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834/3 "2023-07-20T14:25:59Z")

</div>

Yeah it’s because of the loss function being called twice - once directly and once in the gradient calculation. If you want to count only the iterations you can put the counter in the callback function

---

<div class="post-metadata">

**Author:** ![T\_J](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t_j/32/51019_2.png) [@T\_J](https://discourse.julialang.org/u/T_J)\
**Post date:** [July 20, 2023, 2:39pm UTC](https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834/4 "2023-07-20T14:39:04Z")

</div>

What is the purpose of calling `println` in the calculation of the gradient? Will `println` functions have an influence on the calculation of the gradient?

---

<div class="post-metadata">

**Author:** ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)\
**Post date:** [July 20, 2023, 3:08pm UTC](https://discourse.julialang.org/t/number-of-times-loss-function-is-called/101834/5 "2023-07-20T15:08:32Z")

</div>

No, the loss function get evaluated when computing the gradient hence it gets printed. It wouldn’t have any effect
