I am trying to define a callback function to be used when optimizing. I am only interested in the current objective function's value for each iteration.
The documentation suggests, that most optimize methods, and specifically BFGS (my method used), support a callback function with the signature: callback(OptimizeResult: intermediate_result). But trying to access the intermediate_result's fun attribute throws AttributeError.
Using the signature callback(xk) works fine, accessing only the parameter of the current iteration. But to get the value of my objective at each iteration, I would need to re-evaluate the objective, which is not desirable.
How can I get the objective value at each iteration to be returned in my callback without redundant function evaluations of my objective?
If you read the documentation you linked closely it says:
Meaning, you cannot leave the variable as
xkand have it be a positional argument, it must be calledintermediate_resultand must be a keyword argument. In Python 3 you can force a function argument to be a keyword argument by putting*for the first argument (relevant Stack Overflow question).