2017-03-07 37 views
4

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

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

我尝试使用callback选项,但它并没有给我访问优化的内部参数,如雅可比矩阵等

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

所以我的问题:我如何返回中间优化参数(如雅可比)作为scipy.optimize.minimize运行?

+0

我编辑了我的原始回复,它可能适合你 –

回答

2

最简单的方法:

只需直接编辑您的本地SciPy的副本,夹紧在打印或登录到源文件直接。无论如何,你说这只是为了调试。您可以修改回调以传递更多信息。或者,您可以直接将pdb.set_trace()插入到优化代码中并以交互方式查看。要找到哪个文件,你应该在黑客周围,找到模块的位置:

scipy.optimize.__file__ 

然后跟踪。您可能想要删除任何挂着的.pyc文件。在目前的scipy,它位于here in _minimize_trust_region

的“聪明”的方法:

您可以使用内省退一步框架和发现的局部变量,而无需直接修改SciPy的来源。使用框架攻击是脆弱的和依赖于实现的,所以通过一切手段试验它来进行调试,但不要让这样的东西变成任何实际的库代码。

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开始,我们达到最低42在6次迭代:

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

谢谢。嗯,这个工作,当我没有自己计算'jac'吗?当我打印这个东西时,'jac'不是其中的一个键,它只有:'callback','obj'和'labels'。 –

+0

明白了。谢谢!在我的具体情况中,雅可比包裹在'obj'中。 –