0
0
SciPydata~20 mins

Optimization callbacks and monitoring in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Optimization Monitoring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of callback function during optimization

Consider the following Python code using scipy.optimize.minimize with a callback function that prints the current parameter values at each iteration.

import numpy as np
from scipy.optimize import minimize

def func(x):
    return (x[0] - 1)**2 + (x[1] - 2.5)**2

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

result = minimize(func, [0, 0], callback=callback)

What will be the output printed by the callback function during the optimization?

SciPy
import numpy as np
from scipy.optimize import minimize

def func(x):
    return (x[0] - 1)**2 + (x[1] - 2.5)**2

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

result = minimize(func, [0, 0], callback=callback)
AError: callback function must return a value
BOnly one line printing the initial parameters [0, 0]
CNo output printed because callback is ignored
DMultiple lines printing arrays showing the parameter values converging to [1.0, 2.5]
Attempts:
2 left
💡 Hint

Think about what the callback function does at each iteration of the optimizer.

🧠 Conceptual
intermediate
1:30remaining
Purpose of callback in scipy.optimize.minimize

What is the main purpose of providing a callback function to scipy.optimize.minimize?

ATo monitor or log the parameter values at each iteration during optimization
BTo stop the optimization immediately without conditions
CTo change the objective function dynamically during optimization
DTo provide initial guesses for the optimizer
Attempts:
2 left
💡 Hint

Think about what a callback function generally does in iterative processes.

Troubleshoot
advanced
2:00remaining
Why does this callback cause an error?

Given this code snippet:

def callback(xk):
    print(xk)
    return xk

result = minimize(func, [0, 0], callback=callback)

Why might this cause an error or unexpected behavior?

SciPy
def callback(xk):
    print(xk)
    return xk

result = minimize(func, [0, 0], callback=callback)
ABecause the callback function must accept two arguments, not one
BBecause the callback function must return True to continue optimization
CBecause the callback function should not return any value; returning xk causes an error
DBecause printing inside callback is not allowed and causes runtime error
Attempts:
2 left
💡 Hint

Check the documentation for the expected callback signature and behavior.

Best Practice
advanced
1:30remaining
Best practice for monitoring optimization progress

Which approach is best to monitor the progress of a long-running optimization using scipy.optimize.minimize?

AModify the objective function to print values instead of using a callback
BUse a callback function to log parameter values and objective function values at each iteration
CRun the optimizer without monitoring to avoid slowing down the process
DUse global variables to store parameters and print them after optimization finishes
Attempts:
2 left
💡 Hint

Consider how to get updates during optimization without interfering with the process.

🔀 Workflow
expert
2:30remaining
Order of steps to implement optimization monitoring

Arrange the following steps in the correct order to implement monitoring of an optimization process using scipy.optimize.minimize with a callback.

A1,2,3,4
B2,1,3,4
C1,3,2,4
D3,1,2,4
Attempts:
2 left
💡 Hint

Think about the logical order to prepare and run optimization with monitoring.