python - 在 scipy.optimize.minimize 运行时检查内部变量?

标签 python numpy optimization scipy minimization

我通常在程序运行时使用 pdb 检查我的变量。现在我希望通过在优化运行时检查变量来调试我的 scipy.optimize.minimize

result = scipy.optimize.minimize(fun, x0, method='dogleg')

我尝试使用 callback 选项,但它无法让我访问优化的内部参数,例如 Jacobian 矩阵等。

返回的 result 确实有我需要的所有信息,但它只在整个优化结束时返回,但我需要优化运行时的(中间)结果。

所以我的问题是:如何在 scipy.optimize.minimize 运行时返回中间优化参数(例如 Jacobian)?

最佳答案

简单的方法:

只需直接编辑本地的 scipy 副本,打印输出或直接登录源文件即可。无论如何,你说这只是为了调试。您可以修改回调以传递更多信息。或者,您可以将 pdb.set_trace() 直接插入到优化代码中并以交互方式环顾四周。要查找您应该破解的文件,请找到模块所在的位置:

scipy.optimize.__file__

然后沿着小路走。您可能想要删除任何卡在周围的 .pyc 文件。在当前的 scipy 中,它位于 here in _minimize_trust_region .

“聪明”的方式:

您可以使用内省(introspection)来后退一个框架并找到局部变量,而无需直接修改 scipy 源。使用 frame hack 是脆弱的并且依赖于实现,所以一定要用它来调试,但不要让这样的东西进入任何实际的库代码。

from scipy.optimize import minimize
import inspect

def fun(x):
    return (x - 42)**2

def jac(x):
    return 2*(x - 42)

def hess(x):
    return [[2]]

def vanilla_cb(x):
    print(x)

def callback_on_crack(x):
    print(inspect.currentframe().f_back.f_locals)
    print(x)

使用普通回调,从 x0=99 开始,我们在 6 次迭代后达到最小值 42:

>>> minimize(fun,x0=99,method='dogleg',jac=jac,hess=hess,callback=vanilla_cb)
[ 98.]
[ 96.]
[ 92.]
[ 84.]
[ 68.]
[ 42.]

使用增强的回调,您可以在字典中看到所有好的东西!

>>> minimize(fun,99,method='dogleg',jac=jac,hess=hess,callback=callback_on_crack)
{'disp': False, 'unknown_options': {}, 'm': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9d10>, 'm_proposed': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9d10>, 'return_all': False, 'hess': <function function_wrapper at 0x1248938>, 'callback': <function callback_on_crack at 0x12487d0>, 'nhessp': [0], 'njac': [1], 'predicted_reduction': array([ 113.]), 'subproblem': <class 'scipy.optimize._trustregion_dogleg.DoglegSubproblem'>, 'maxiter': 200, 'warnflag': 0, 'gtol': 0.0001, 'args': (), 'initial_trust_radius': 1.0, 'hits_boundary': True, 'trust_radius': 2.0, 'predicted_value': array([ 3136.]), 'rho': array([ 1.]), 'x': array([ 98.]), 'nhess': [1], 'x0': array([ 99.]), 'hessp': None, 'k': 0, 'actual_reduction': array([ 113.]), 'jac': <function function_wrapper at 0x12488c0>, 'p': array([-1.]), 'eta': 0.15, 'fun': <function function_wrapper at 0x1248848>, 'nfun': [2], 'max_trust_radius': 1000.0, 'x_proposed': array([ 98.])}
[ 98.]
{'disp': False, 'unknown_options': {}, 'm': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9d90>, 'm_proposed': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9d90>, 'return_all': False, 'hess': <function function_wrapper at 0x1248938>, 'callback': <function callback_on_crack at 0x12487d0>, 'nhessp': [0], 'njac': [2], 'predicted_reduction': array([ 220.]), 'subproblem': <class 'scipy.optimize._trustregion_dogleg.DoglegSubproblem'>, 'maxiter': 200, 'warnflag': 0, 'gtol': 0.0001, 'args': (), 'initial_trust_radius': 1.0, 'hits_boundary': True, 'trust_radius': 4.0, 'predicted_value': array([ 2916.]), 'rho': array([ 1.]), 'x': array([ 96.]), 'nhess': [2], 'x0': array([ 99.]), 'hessp': None, 'k': 1, 'actual_reduction': array([ 220.]), 'jac': <function function_wrapper at 0x12488c0>, 'p': array([-2.]), 'eta': 0.15, 'fun': <function function_wrapper at 0x1248848>, 'nfun': [3], 'max_trust_radius': 1000.0, 'x_proposed': array([ 96.])}
[ 96.]
{'disp': False, 'unknown_options': {}, 'm': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9e50>, 'm_proposed': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9e50>, 'return_all': False, 'hess': <function function_wrapper at 0x1248938>, 'callback': <function callback_on_crack at 0x12487d0>, 'nhessp': [0], 'njac': [3], 'predicted_reduction': array([ 416.]), 'subproblem': <class 'scipy.optimize._trustregion_dogleg.DoglegSubproblem'>, 'maxiter': 200, 'warnflag': 0, 'gtol': 0.0001, 'args': (), 'initial_trust_radius': 1.0, 'hits_boundary': True, 'trust_radius': 8.0, 'predicted_value': array([ 2500.]), 'rho': array([ 1.]), 'x': array([ 92.]), 'nhess': [3], 'x0': array([ 99.]), 'hessp': None, 'k': 2, 'actual_reduction': array([ 416.]), 'jac': <function function_wrapper at 0x12488c0>, 'p': array([-4.]), 'eta': 0.15, 'fun': <function function_wrapper at 0x1248848>, 'nfun': [4], 'max_trust_radius': 1000.0, 'x_proposed': array([ 92.])}
[ 92.]
{'disp': False, 'unknown_options': {}, 'm': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9dd0>, 'm_proposed': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9dd0>, 'return_all': False, 'hess': <function function_wrapper at 0x1248938>, 'callback': <function callback_on_crack at 0x12487d0>, 'nhessp': [0], 'njac': [4], 'predicted_reduction': array([ 736.]), 'subproblem': <class 'scipy.optimize._trustregion_dogleg.DoglegSubproblem'>, 'maxiter': 200, 'warnflag': 0, 'gtol': 0.0001, 'args': (), 'initial_trust_radius': 1.0, 'hits_boundary': True, 'trust_radius': 16.0, 'predicted_value': array([ 1764.]), 'rho': array([ 1.]), 'x': array([ 84.]), 'nhess': [4], 'x0': array([ 99.]), 'hessp': None, 'k': 3, 'actual_reduction': array([ 736.]), 'jac': <function function_wrapper at 0x12488c0>, 'p': array([-8.]), 'eta': 0.15, 'fun': <function function_wrapper at 0x1248848>, 'nfun': [5], 'max_trust_radius': 1000.0, 'x_proposed': array([ 84.])}
[ 84.]
{'disp': False, 'unknown_options': {}, 'm': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9e10>, 'm_proposed': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9e10>, 'return_all': False, 'hess': <function function_wrapper at 0x1248938>, 'callback': <function callback_on_crack at 0x12487d0>, 'nhessp': [0], 'njac': [5], 'predicted_reduction': array([ 1088.]), 'subproblem': <class 'scipy.optimize._trustregion_dogleg.DoglegSubproblem'>, 'maxiter': 200, 'warnflag': 0, 'gtol': 0.0001, 'args': (), 'initial_trust_radius': 1.0, 'hits_boundary': True, 'trust_radius': 32.0, 'predicted_value': array([ 676.]), 'rho': array([ 1.]), 'x': array([ 68.]), 'nhess': [5], 'x0': array([ 99.]), 'hessp': None, 'k': 4, 'actual_reduction': array([ 1088.]), 'jac': <function function_wrapper at 0x12488c0>, 'p': array([-16.]), 'eta': 0.15, 'fun': <function function_wrapper at 0x1248848>, 'nfun': [6], 'max_trust_radius': 1000.0, 'x_proposed': array([ 68.])}
[ 68.]
{'disp': False, 'unknown_options': {}, 'm': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9d50>, 'm_proposed': <scipy.optimize._trustregion_dogleg.DoglegSubproblem object at 0xdc9d50>, 'return_all': False, 'hess': <function function_wrapper at 0x1248938>, 'callback': <function callback_on_crack at 0x12487d0>, 'nhessp': [0], 'njac': [6], 'predicted_reduction': array([ 676.]), 'subproblem': <class 'scipy.optimize._trustregion_dogleg.DoglegSubproblem'>, 'maxiter': 200, 'warnflag': 0, 'gtol': 0.0001, 'args': (), 'initial_trust_radius': 1.0, 'hits_boundary': False, 'trust_radius': 32.0, 'predicted_value': array([ 0.]), 'rho': array([ 1.]), 'x': array([ 42.]), 'nhess': [6], 'x0': array([ 99.]), 'hessp': None, 'k': 5, 'actual_reduction': array([ 676.]), 'jac': <function function_wrapper at 0x12488c0>, 'p': array([-26.]), 'eta': 0.15, 'fun': <function function_wrapper at 0x1248848>, 'nfun': [7], 'max_trust_radius': 1000.0, 'x_proposed': array([ 42.])}
[ 42.]

关于python - 在 scipy.optimize.minimize 运行时检查内部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42657931/

相关文章:

python - 如何使用生成器参数验证 Python 模拟调用

python - 如何在人脸检测中创建一个计数器?

python - 优化 numpy 矩阵运算(当前使用 for 循环)

asp.net - MVC 4 中的运行时动态 bundle 和缩小

Java - SAM类型优化

python - 从 Pandas 数据框中提取非嵌套列表

python - 通过 Youtube API v3 将视频上传到多个 channel

python - 基于属性的测试和突变测试有什么区别?

python - 二维 numpy 数组组上最快的应用函数

python - numpy 数组,即 (n,1) 和 (n,)