我通常在程序运行时用pdb
检查变量。现在我希望通过在优化运行时检查变量来调试我的scipy.optimize.minimize
。在
result = scipy.optimize.minimize(fun, x0, method='dogleg')
我尝试使用callback
选项,但它不能让我访问优化的内部参数,例如雅可比矩阵等
返回的result
包含了我需要的所有信息,但它只在整个优化结束时返回,但我需要优化运行时的(中间)结果。在
所以我的问题是:如何在scipy.optimize.minimize
运行时返回中间优化参数(例如Jacobian)?在
简单方法:
只需直接编辑scipy的本地副本,输入打印输出或直接登录到源文件。你说这只是为了调试。您可以修改回调以传递更多信息。或者,您可以直接在优化代码中插入
pdb.set_trace()
,并以交互方式四处查看。要找到应该在其中进行黑客攻击的文件,请找到该模块所在的位置:然后沿着小路走。您可能想删除任何} 。在
.pyc
文件。在当前的scipy
中,它位于here in ^{“聪明”的方法:
您可以使用自省来后退一个框架并找到局部变量,而不需要直接修改scipy源代码。使用frame hacks是脆弱的,并且依赖于实现,所以一定要用它进行调试,但是不要让这样的东西进入任何实际的库代码中。在
^{pr2}$使用普通回调,从
^{3}$x0=99
开始,经过6次迭代,我们达到最小值42:使用增强的回调,你可以在dict中看到所有的好东西!在
相关问题 更多 >
编程相关推荐