使用非常大或非常小的数字时 scipy.optimize.fmin_slsqp 出现问题

发布于 2024-10-17 12:44:34 字数 518 浏览 7 评论 0原文

有没有人仅在使用非常大或非常小的数字时遇到过 fmin_slsqp (或 scipy.optimize 中的其他任何内容)的问题?

我正在编写一些Python代码来获取灰度图像和掩模,生成直方图,然后将多个高斯拟合到直方图。为了开发代码,我使用了一个小示例图像,经过一些工作后,代码运行得非常好。但是,当我首先对直方图进行标准化,生成 bin 值 << 1 时,或者当我对巨大图像进行直方图绘制时,生成数十万的 bin 值,fmin_slsqp() 开始偶尔失败。它仅在大约 5 次迭代后退出,通常只返回我给出的初始猜测的稍微修改的版本,并返回退出模式 8,这意味着“线性搜索的正方向导数”。如果我在开始时检查 bin 计数的大小并将其缩放到 ~100-1000 附近,则 fmin_slsqp() 会照常工作。我只是在返回结果之前取消缩放。我想我可以就这样保留它,但这感觉就像一个黑客。

我环顾四周,发现人们在谈论 epsilon 值,它基本上是用于近似导数的 dx,但调整并没有帮助。除此之外我还没有发现任何有用的东西。任何想法将不胜感激。提前致谢。

詹姆斯

Has anybody ever encountered problems with fmin_slsqp (or anything else in scipy.optimize) only when using very large or very small numbers?

I am working on some python code to take a grayscale image and a mask, generate a histogram, then fit multiple gaussians to the histogram. To develop the code I used a small sample image, and after some work the code was working brilliantly. However, when I normalize the histogram first, generating bin values <<1, or when I histogram huge images, generating bin values in the hundreds of thousands, fmin_slsqp() starts failing sporadically. It quits after only ~5 iterations, usually just returning a slightly modified version of the initial guess I gave it, and returns exit mode 8, which means "Positive directional derivative for linesearch." If I check the size of the bin counts at the beginning and scale them into the neighborhood of ~100-1000, fmin_slsqp() works as usual. I just un-scale things before returning the results. I guess I could leave it like that, but it feels like a hack.

I have looked around and found folks talking about the epsilon value, which is basically the dx used for approximating derivatives, but tweaking that has not helped. Other than that I haven't found anything useful yet. Any ideas would be greatly appreciated. Thanks in advance.

james

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-10-24 12:44:34

我也遇到过类似的问题optimize.leastsq。我需要处理的数据通常非常小,例如 1e-18 等,并且我注意到在这些情况下,leastsq 不会收敛到最佳拟合参数。只有当我将数据缩放到更常见的程度(例如数百、数千等,可以用整数维持分辨率和动态范围的东西)时,我才能让leastsq收敛到非常合理的东西。

我一直在尝试使用这些可选的容差参数,这样我就不必在优化之前缩放数据,但没有太多运气...

有谁知道一个好的通用方法来避免函数出现此问题在 scipy.optimize 包中?我很感激你能分享...我认为根源与OP的问题相同。

I've had similar problems optimize.leastsq. The data I need to deal with often are very small, like 1e-18 and such, and I noticed that leastsq doesn't converge to best fit parameters in those cases. Only when I scale the data to something more common (like in hundreds, thousands, etc., something you can maintain resolution and dynamic range with integers), I can let leastsq converge to something very reasonable.

I've been trying around with those optional tolerance parameters so that I don't have to scale data before optimizing, but haven't had much luck with it...

Does anyone know a good general approach to avoid this problem with the functions in the scipy.optimize package? I'd appreciate you could share... I think the root is the same problem with the OP's.

≈。彩虹 2024-10-24 12:44:34

当您的基础数据规模急剧变化时,您是否会更新您的初始猜测(“x0”)?对于任何迭代线性优化问题,如果您的初始猜测与您尝试拟合的数据相距甚远,就会出现这些问题。它更像是一个优化问题,而不是一个 scipy 问题。

Are you updating your initial guess ("x0") when your underlying data changes scale dramatically? for any iterative linear optimization problem, these problems will occur if your initial guess is far from the data you're trying to fit. It's more of a optimization problem than a scipy problem.

余生再见 2024-10-24 12:44:34

我也遇到了这个问题,但是我在我的项目中解决了它。我不确定这是否是通用解决方案。

原因是当参数jac设置为False或默认值时,scipy.optimize.fmin_slsqp通过近似方法计算梯度。近似方法产生的梯度未标准化(大尺度)。在计算步长时,较大的梯度值会影响线搜索的性能和精度。这可能就是我们得到线性搜索正向导数的原因。

您可以尝试将雅可比矩阵的闭合形式实现为目标函数,并将其传递给 jac 参数。更重要的是,您应该重新调整雅可比矩阵的值(如归一化)以避免影响线搜索。

最好的。

I got in trouble with this issue too, but I solved it in my project. I'm not sure if this is a general solution.

The reason was that the scipy.optimize.fmin_slsqp calculated the gradient by an approximate approach when the argument jac is set by False or default. The gradient produced from the approximate approach was not normalized (with large scale). When calculating the step length, large value of gradient would influence the performance and precision of line search. This might be the reason why we got Positive directional derivative for linesearch.

You can try to implement the closed form of the Jacobian matrix to the object function and pass it to the jac argument. More importantly, you should rescale the value of Jacobian matrix (like normalization) to avoid affecting line search.

Best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文