简体   繁体   English

R 中的优化:Levenberg-Marquardt 在 minpack.lm 中使用 nls.lm:将 `maxiter' 重置为 1024

[英]Optimization in R: Levenberg-Marquardt using nls.lm in minpack.lm: resetting `maxiter' to 1024

I am trying to learn how to work with nls.lm in the R library minpack.lm by using the Rosenbrock function to see if the algorithm converges to the global minimum at f(x,y) = (1,1).我想学习如何与工作nls.lm在R库minpack.lm使用Rosenbrock函数,看是否算法收敛到全局最小值在f(X,Y)=(1,1)。 I do so both with and without the analytic Jacobian.无论有没有解析雅可比行列式,我都是这样做的。 In both instances, I get a warning telling me that the algorithm has decided to revert the maximum number of iterations specified in the call to nls.lm to 1024:在这两种情况下,我都会收到一条警告,告诉我算法已决定将调用nls.lm指定的最大迭代次数恢复为 1024:

Warning messages:
1: In nls.lm(par = initpar, fn = objective_rosenbrock, jac = gradient_rosenbrock,  :
  resetting `maxiter' to 1024!
2: In nls.lm(par = initpar, fn = objective_rosenbrock, jac = gradient_rosenbrock,  :
  lmder: info = -1. Number of iterations has reached `maxiter' == 1024.

The algorithm never quite reaches (1,1) as a result given my initial guess of (-1.2, 1.0).考虑到我对 (-1.2, 1.0) 的初始猜测,该算法永远不会完全达到 (1,1)。 I found the source code for the library on GitHub and the following lines of code are pertinent here:我在 GitHub 上找到了该库的源代码,以下代码行与此处相关:

https://github.com/cran/minpack.lm/blob/master/src/nls_lm.c https://github.com/cran/minpack.lm/blob/master/src/nls_lm.c

OS->maxiter = INTEGER_VALUE(getListElement(control, "maxiter"));
if(OS->maxiter > 1024) {
  OS->maxiter = 1024;
  warning("resetting `maxiter' to 1024!");
}

Is there any logic to why the maximum number of iterations is capped to 1024?为什么最大迭代次数上限为 1024 有什么逻辑吗? Something with bits and 2^10?有位和 2^10 的东西? I would like to use the library for a different application, but this cap on iterations might prevent that.我想将该库用于不同的应用程序,但此迭代上限可能会阻止这种情况。 Any insight would be appreciated.任何见解将不胜感激。

Git blame says that this code limiting the max iterations was introduced in version 1.1-0, in 2008. The NEWS file for the package only goes back as far as version 1.1-6. Gitblame说这个限制最大迭代次数的代码是在 2008 年的 1.1-0 版中引入的。该NEWS 文件只能追溯到 1.1-6 版。 I can't find the code in any public repo other than the one you point to (which is only a CRAN mirror; it doesn't contain any comments/commit messages/etc. from developers that might give us clues.)除了您指向的那个之外,我在任何公共 repo 中都找不到代码(这只是一个 CRAN 镜像;它不包含来自开发人员的任何评论/提交消息/等,它们可能会给我们提供线索。)

Other than contacting the maintainer I think it's going to be hard to figure out what the rationale is for this limit.除了联系维护者之外,我认为很难弄清楚这个限制的基本原理是什么。

I do have some guesses though.不过我确实有一些猜测。

The only places that maxiter is actually used in the code are here and here - in R code, not Fortran or C code, so it seems extremely unlikely that we are dealing with something like a 10-bit unsigned integer type (which seems an unlikely choice in any case).在代码中实际使用maxiter的唯一地方是这里这里- 在 R 代码中,而不是 Fortran 或 C 代码中,因此我们处理诸如 10 位无符号整数类型之类的东西似乎极不可能(这似乎不太可能)任何情况下的选择)。 I think the limitation is there because we also have a buffer defined for holding trace information here :我认为,限制是有,因为我们也有保持跟踪信息确定的缓冲器这里

  double rsstrace[1024];

which, as you can see, is hard-coded to a length of 1024. Presumably bad things would happen if we tried to stuff 1025 iterations'-worth of tracing information into this array ...正如你所看到的,它被硬编码为 1024 的长度。如果我们试图将 1025 次迭代的跟踪信息填充到这个数组中,大概会发生不好的事情......

My suggestions:我的建议:

  • change all instances of '1024' in the code to something larger and see what happens.将代码中“1024”的所有实例更改为更大的内容,看看会发生什么。 There are only four:只有四种:
$ find . -type f -exec grep -Hn 1024 {} \;
./src/nls_lm.c:141:    if(OS->maxiter > 1024) {
./src/nls_lm.c:142:      OS->maxiter = 1024;
./src/nls_lm.c:143:      warning("resetting `maxiter' to 1024!");
./src/minpack_lm.h:20:  double rsstrace[1024];
  • it would be best to #define MAXITER 2048 (or whatever) in src/minpack_lm.h and use that instead of the numerical value.最好在src/minpack_lm.h #define MAXITER 2048 (或其他)并使用它而不是数值。
  • Contact the maintainer ( maintainer("minpack.lm") ) and ask them about this issue.联系维护者( maintainer("minpack.lm") )并询问他们这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R:minpack.lm :: nls.lm失败,结果很好 - R: minpack.lm::nls.lm failed with good results 有关在minpack.lm软件包中使用NLS.LM的问题 - Questions about using NLS.LM in the minpack.lm package 可能的 Package 与用于 nlsLM 优化的 minpack.lm 冲突 - Possible Package conflicts with minpack.lm for nlsLM optimization 在R中使用`nls.lm`时如何处理边界约束 - How to handle boundary constraints when using `nls.lm` in R 在“R”中更改nls.lm()中的目标函数 - Change Objective Function in nls.lm() in “R” 在R中使用nls.lm进行模型拟合,“错误:未使用的参数” - Model fitting with nls.lm in R, “Error: unused argument” 将罚分 function 应用于非线性最小二乘估计收敛与 R - package minpack.lm - Apply penalty function to non-linear least square estimation convergence with bounds in R - package minpack.lm 在 R 中实现 Levenberg-Marquardt 算法 - Implement Levenberg-Marquardt algorithm in R 使用加权 function (minpack.lm::wfct()) 进行非线性回归时,仅 try() 或 trycatch() 会出错 - Error only with try() or trycatch() when using Weighting function (minpack.lm::wfct()) for non linear regression 无法在 Mac M1 上安装(编译)minpack.lm - Failing to install (compile) minpack.lm on Mac M1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM