简体   繁体   English

Matlab 的 fminunc 函数的 R 等价物是多少?

[英]What is the R equivalent of Matlab's fminunc function?

In order to compute the optimal theta eg in logistic regression, I have to create a costFunction (the function to be minimized) which is then passed to fminunc in order to obtain the optimal theta.为了计算最佳 theta,例如在逻辑回归中,我必须创建一个 costFunction(要最小化的函数),然后将其传递给 fminunc 以获得最佳 theta。 Also, if the gradient of costFunction can be computed, I set the 'GradObj' option to 'on' using此外,如果可以计算 costFunction 的梯度,我将“GradObj”选项设置为“on”使用

options = optimset('GradObj','on');

and code the costFunction so that it returns, as a second output argument, the gradient value g of X. Then I give并对 costFunction 进行编码,使其作为第二个输出参数返回 X 的梯度值 g。然后我给出

[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

where X is the data matrix and y the response.其中 X 是数据矩阵, y 是响应。 How can I implement the above in R?如何在 R 中实现上述内容?

Take a look at the optim function.看看optim函数。 It can do unconstrained minimization using method = 'L-BFGS-B' and you can specify an analytical function to compute the gradient as well它可以使用method = 'L-BFGS-B'进行无约束最小化,您也可以指定一个分析函数来计算梯度

EDIT.编辑。 As Ben has pointed out correctly, fminunc does unconstrained optimization, which can also be achieved using the optim function choosing Nelder-Mead or BFGS .正如 Ben 正确指出的那样, fminunc无约束优化,这也可以使用optim函数选择Nelder-MeadBFGS Moreover, I also noticed from the documentation of fminunc that it does large-scale optimization using trust region methods.此外,我还从fminunc的文档中注意到它使用trust域方法进行大规模优化。 There is an R package trust that I believe does the same thing.我相信有一个 R 包trust可以做同样的事情。 I would recommend taking a look at the optimization task view of R.我建议看一下 R 的optimization任务视图

In the R, you can use the function nlminb in the R to do constrained optimization!在R中,可以使用R中的函数nlminb做约束优化!

nlminb(start, objective, gradient = NULL, hessian = NULL, ..., scale = 1, control = list(), lower = -Inf, upper = Inf)

The start is a vector include all initial value of parameters.开始是一个向量,包括所有参数的初始值。 objective is the cost function or any other function that you want to minimize.目标是成本函数或您想要最小化的任何其他函数。

Have you tried the ucminf function of the ucminf package?你试过ucminf包的ucminf功能了吗? If yours is an unconstrained problem (and in many cases you can easily convert constrained problems into unconstrained ones), the ucminf is quite similar to Matlab's fmincon .如果您的问题是不受约束的问题(并且在许多情况下,您可以轻松地将受约束的问题转换为不受约束的问题),则ucminf与 Matlab 的fmincon非常相似。 The two are similar in the sense that both use a trust-region type monitoring.两者在使用信任区域类型监控的意义上是相似的。 Unlike fmincon (that relies on the interior-point algorithm), ucminf is based on a quasi-Newton type of algorithm.fmincon (依赖于内点算法)不同, ucminf基于拟牛顿类型的算法。 However, usminf provides you with the same types of controls as fmincon .但是, usminf为您提供了与fmincon相同类型的控件。 From my experience, ucminf is pretty good at replicating fmincon 's output, give it a shot.根据我的经验, ucminf非常擅长复制fmincon的输出, ucminf

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM