问题描述
我正在使用scipy.optimize.minimize为我的目标函数找到最佳参数.
I am using scipy.optimize.minimize to find optimal parameters for my objective function.
我的代码:
import numpy as np
from scipy.optimize import minimize
from scipy.optimize import Bounds
bounds = Bounds([26,26,8,6,400,100,0,25,2],[36,38,28,28,1800,800,100,50,7])
energy_history = []
x_values = []
def objective(x):
x_trail = x.reshape(1,-1)
x_trail = sc_X.transform(x_trail)
y_trail = regressorSVR.predict(x_trail)
y_trail = y_trail.reshape(1,-1)
y_trail = sc_Y.inverse_transform(y_trail)
return y_trail[0]
def callback(x,y):
fobj = objective(x)
energy_history.append(fobj)
x_values.append(x)
x0 = np.array([26,28,15,7,400,377,40,43,4.3])
res = minimize(objective, x0, method='trust-constr',
options={'verbose': 1}, bounds=bounds,callback=callback)
optimal_values = res.x
energy = res.fun
在给定初始值的情况下,我得到的最小值(res.fun)为-7.1.我正在创建一个列表(energy_history),以查看它如何达到此值.我在该列表中看到了一些小于-7.1的值,但是为什么-7.1被作为最小值返回.
With the initial values given, the minimized value(res.fun) that I get is -7.1. I am creating a list(energy_history) to see how it is reaching this value. I see some values which are less than -7.1 in that list, but still, why is -7.1 being returned as the minimal value.
在很多时候目标函数达到-21的值,但是为什么仍然返回-7的最小值?
There are multiple times where objective function reached a value of -21, but why is still -7 being returned as a minimum ?
推荐答案
如果我们看一下 scipy.optimization
文档,我们可以看到scipy.optimize.minimize
列在本地优化下.主要问题是您的问题是非凸的,因此scipy.optimize.minimize
无法保证适当的收敛.由于它也是不可微的,因此许多算法根本不适用.
If we take a look at the scipy.optimization
documentation we can see that scipy.optimize.minimize
is listed under local optimization.The main problem is that your problem is non-convex and thus scipy.optimize.minimize
cannot guarantee the proper convergence. As it's also very much non-differentiable, many algorithms won't be suited at all.
scipy.optimize
确实提供了一些全局优化算法,尽管可以在文档页面上的全局优化下找到它们,即basinhopping
,brute
和differential_evolution
.请查看此答案以获取一些简短说明.
scipy.optimize
does provide some global optimization algorithms though that can be found on the documentation page under global optimization, namely basinhopping
, brute
, and differential_evolution
. Look at this answer for some short explanation.
基本上,您可以先尝试brute
,仅查看任何系统性问题.从根本上讲,这是一种蛮力解决方案,并且会很慢,但要找到最低要求.更复杂的方法是使用differential_evolution
.由于您的功能并不是很流畅,因此basinhopping
可能无法正常工作,但是仍然值得一试,并且收敛速度最快.
Basically you can try brute
first, just to see any systematic problems. It's basically a brute force solution and will be slow, but find your minimum. The more sophisticated method would be using differential_evolution
. Since your function isn't really smooth, basinhopping
might not work, but it's still worth a shot and would probably converge the fastest.
这篇关于即使Scipy.optimize.minimize看到最小值,它也不会给出最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!