检查内部变量scipy.optimize.minimize.最小化跑?

2024-09-27 09:29:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我尝试使用callback选项,但它不能让我访问优化的内部参数,例如雅可比矩阵等

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

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


Tags: 程序运行参数选项callback矩阵scipyresultmethod
1条回答
网友
1楼 · 发布于 2024-09-27 09:29:29

简单方法:

只需直接编辑scipy的本地副本,输入打印输出或直接登录到源文件。你说这只是为了调试。您可以修改回调以传递更多信息。或者,您可以直接在优化代码中插入pdb.set_trace(),并以交互方式四处查看。要找到应该在其中进行黑客攻击的文件,请找到该模块所在的位置:

scipy.optimize.__file__

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

“聪明”的方法:

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

^{pr2}$

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

^{3}$

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

>>> 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.]

相关问题 更多 >

    热门问题