0
0
SciPydata~10 mins

Optimization callbacks and monitoring in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Optimization callbacks and monitoring
Start Optimization
Iteration begins
Evaluate function
Call callback with current state
Check stopping criteria
No Yes
Update variables
Next iteration
The optimization runs in steps. After each step, it evaluates the function, calls the callback to monitor progress, then decides to continue or stop.
Execution Sample
SciPy
from scipy.optimize import minimize

def callback(xk):
    print(f"Current x: {xk}")

res = minimize(lambda x: (x-3)**2, x0=0, callback=callback)
This code runs an optimization to find x that minimizes (x-3)^2, printing x at each step.
Execution Table
StepCurrent xFunction ValueCallback OutputAction
1[0.0]9.0Current x: [0.0]Update x towards 3
2[1.8]1.44Current x: [1.8]Update x towards 3
3[2.52]0.2304Current x: [2.52]Update x towards 3
4[2.808]0.0467Current x: [2.808]Update x towards 3
5[2.965]0.0012Current x: [2.965]Update x towards 3
6[2.999]0.0000Current x: [2.999]Converged, stop
7[3.0]0.0Current x: [3.0]Optimization ends
💡 Optimization stops when x is close enough to 3 and function value is near zero.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
x0.01.82.522.8082.9652.9993.03.0
function_value9.01.440.23040.04670.00120.00000.00.0
Key Moments - 2 Insights
Why does the callback print the current x before the variable updates?
The callback is called after the function evaluation at the current x but before the next update, as shown in execution_table rows where callback output matches current x before action.
Why does the optimization stop even though x is not exactly 3?
Optimization stops when the function value is close enough to zero (converged), not necessarily exactly at 3, as seen in step 6 where function value is near zero.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the function value at step 3?
A0.2304
B1.44
C0.0467
D9.0
💡 Hint
Check the 'Function Value' column at step 3 in the execution_table.
At which step does the optimization decide to stop?
AStep 5
BStep 7
CStep 6
DStep 4
💡 Hint
Look at the 'Action' column where it says 'Converged, stop'.
If the callback was removed, what would change in the execution table?
AFunction values would be different
BNo callback output column and no printed current x values
COptimization would not stop
Dx values would not update
💡 Hint
Callback only monitors progress; it does not affect variable updates or stopping.
Concept Snapshot
Optimization callbacks let you monitor progress each step.
Callback runs after function evaluation but before variable update.
Use callback to print or log current variables.
Optimization stops when criteria met, not necessarily exact solution.
Callbacks do not change optimization behavior, only observe it.
Full Transcript
In optimization with scipy, the process starts by evaluating the function at an initial guess. After each evaluation, a callback function is called with the current variables. This callback can print or log the current state to monitor progress. Then the optimizer updates the variables to move closer to the minimum. This repeats until the stopping criteria are met, such as the function value being close enough to zero. The callback helps track the optimization without changing its behavior. The example shows x moving from 0 to 3, with the callback printing x at each step. The optimization stops when x is near 3 and the function value is near zero.