I am trying to incorporate an early stopping to my code using Optim.jl but the specifications are not followed and I was hoping someone might know why. I have an example of this here.
function toy(X)
x, y = X
cost = (x - 3) ^ 2 + (y - 5) ^ 2 @info cost
return cost
end
Oh thanks, Adienes. Did you ever figure out a way to force the constraints to work? or did you have to use another package that obeys the constraints specification?
Hi, Unfortunately, outer_f_reltol did not have the desired effect. just g_abstol worked to some degree but I want to be able to stop the algorithm based on the the result of the loss of each iteration. I am still trying to figure out ways to solve this problem.
One issue is that Optim’s algorithm calls your function several times per iteration, but checks convergence only once per iteration, so what you see when you print from your cost function isn’t exactly what Optim’s look at for convergence.
If you hack into Optim with this you can see the function value is already 5 the first time convergence is assessed :
function toy(X)
x, y = X
cost = (x - 3) ^ 2 + (y - 5) ^ 2
@info "calling function : $cost"
return cost
end
function custom_stopping(x)
@info "check for custom stopping"
return false
end
@eval Optim begin
function assess_convergence(x, x_previous, f_x, f_x_previous, gx, x_abstol, x_reltol, f_abstol, f_reltol, g_abstol)
@info "assessing convergence"
f_x < 1000 && return true, false, false, false
return false, false, false, false
end
end
optimize(
toy, [0,0], [Inf,Inf], [10., 50.], Optim.Fminbox(),
Optim.Options(callback = custom_stopping)
)
[ Info: calling function : 2074.00084776729
[ Info: calling function : 2073.9991522400437
[ Info: calling function : 2074.027249636707
[ Info: calling function : 2073.9727505466353
[ Info: calling function : 2074.0
[ Info: check for custom stopping
[ Info: calling function : 1373.0575397465902
[ Info: calling function : 1373.0561782060774
[ Info: calling function : 1373.0752432895974
[ Info: calling function : 1373.0384747834933
[ Info: calling function : 1373.0568589732666
[ Info: calling function : 14.784521316821616
[ Info: calling function : 14.784142416634552
[ Info: calling function : 14.784584618784134
[ Info: calling function : 14.784079116620969
[ Info: calling function : 14.784331865524148
[ Info: calling function : 11.474314941709697
[ Info: calling function : 11.47403851522763
[ Info: calling function : 11.474101109876269
[ Info: calling function : 11.474252345510765
[ Info: calling function : 11.474176727478573
[ Info: calling function : 5.990978973840084
[ Info: calling function : 5.990656542939395
[ Info: calling function : 5.990810840734959
[ Info: calling function : 5.990824675619014
[ Info: calling function : 5.990817757302663
[ Info: assessing convergence
[ Info: check for custom stopping
[ Info: assessing convergence
[ Info: calling function : 5.990817757302663