New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fmin_slsqp not converging to sensible solution #7519
Comments
I have been playing with your code, and it seems to fail for all optimizers, not just
The other interesting thing is that if you multiply your whole function by So I don't really think it is a bug in |
@jaimefrio - I haven't had this issue with all other optimizers. For example, if I just use import numpy as np
from scipy.optimize import fmin
w = np.linspace(1, 2, 100)
f = 1.2e-8 * np.exp(-0.5 * (w-1.4)**2 / 0.11**2)
def gaussian(p, x):
return p[0] * np.exp(-0.5 * (x - p[1])**2 / p[2]**2)
def errfunc(p, x, y):
return np.sum((gaussian(p, x) - y)**2)
p0 = [1.5e-8, 1.6, 0.2]
p_new = fmin(errfunc, x0=p0, args=(w, f))
print('p0 ', p0)
print('p_new', p_new) with the following output:
The I don't think this is a numerically ill-conditioned problem but more that some algorithms may have built-in absolute tolerances or other issues. I will try and look into it further. |
Several of the optimizers may have assumptions the problem is well
scaled. This is not necessarily only a question of absolute tolerances.
|
Namely, here it is the first variable that has a very different scale
than the rest. An algorithm using e.g. spherical trust regions will
encounter problems. I don't recall outright what SLSQP exactly does, but
maybe it does just that.
|
@pv - I'm reaching the same conclusion. I can get better (though still not ideal) results if I modify |
The scaling of the problem is pretty much extra information that you
pass to the solver --- you can pass it to the solver by ensuring typical
parameters and values are of order 1.
.
It is in principle possible to add some logic that tries to rescale the
problem, for example by looking at the jacobian etc. But this is
probably not always reliable, especially if the initial point is not
representative of the solution.
|
@pv - ok, that makes sense, thanks! I have a way to fix the scaling in an automated way, but I'm still running into issues even when the scales are appropriate and the initial parameters are a bit off. Consider the following example: import numpy as np
from scipy.optimize import fmin_slsqp
w = np.linspace(1, 2, 100)
f = np.exp(-0.5 * (w-1.4)**2 / 0.11**2)
def gaussian(p, x):
return p[0] * np.exp(-0.5 * (x - p[1])**2 / p[2]**2)
def errfunc(p, x, y):
return np.sum((gaussian(p, x) - y)**2)
p0 = [3, 0.85, 0.1]
p_new = fmin_slsqp(errfunc, x0=p0, args=(w, f))
print('p0 ', p0)
print('p_new', p_new)
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(w, f, 'k.')
ax.plot(w, gaussian(p0, w), 'r-')
ax.plot(w, gaussian(p_new, w), 'g-')
fig.savefig('fit.png') Is this also expected? Other optimizers (e.g. |
Re-running my test script with the data normalized to ~1., I found that SLSQP improves tremendously. It’s able to find the solution, and also appears to be relatively insensitive to the initial guesses for the parameters.
Other methods seem to not care at all for the small values of the dependent variable (~1.e-15), with the exception of L-M (the MINPACK implementation) which changes its behavior in a funky way depending on the data normalization. I still have to work more on that topic.
Ivo
… On Jun 23, 2017, at 9:50 AM, Thomas Robitaille ***@***.***> wrote:
@pv - ok, that makes sense, thanks! I have a way to fix the scaling in an automated way, but I'm still running into issues even when the scales are appropriate and the initial parameters are a bit off. Consider the following example:
import numpy as
np
from scipy.optimize import
fmin_slsqp
w
= np.linspace(1, 2, 100
)
f
= np.exp(-0.5 * (w-1.4)**2 / 0.11**2
)
def gaussian(p, x
):
return p[0] * np.exp(-0.5 * (x - p[1])**2 / p[2]**2
)
def errfunc(p, x, y
):
return np.sum((gaussian(p, x) - y)**2
)
p0
= [3, 0.85, 0.1
]
p_new
= fmin_slsqp(errfunc, x0=p0, args=
(w, f))
print('p0 '
, p0)
print('p_new'
, p_new)
import matplotlib.pyplot as
plt
fig
=
plt.figure()
ax
= fig.add_subplot(1,1,1
)
ax.plot(w, f,
'k.'
)
ax.plot(w, gaussian(p0, w),
'r-'
)
ax.plot(w, gaussian(p_new, w),
'g-'
)
fig.savefig(
'fit.png')
Is this also expected? Other optimizers (e.g. fmin) don't have any issue with such cases, but is this just a peculiarity of SLSQP that it is very sensitive to the initial conditions?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I am trying to use the
fmin_slsqp
function while fitting a Gaussian to fake data:However this does not converge to the expected solution despite the reasonable initial guess:
I suspected that this was due to the
acc
tolerance which may be absolute, so I changed it to:However while this now does many iterations instead of one, it still does not converge:
It seems like this could be a bug?
I'm using Python 3.6.1 with Scipy 0.19.0 and Numpy 1.11.3.
(cc @ibusko - just so you are in the loop)
The text was updated successfully, but these errors were encountered: