📄 lbfgs.h
字号:
const int n, const lbfgsfloatval_t step );/** * Callback interface to receive the progress of the optimization process. * * The lbfgs() function call this function for each iteration. Implementing * this function, a client program can store or display the current progress * of the optimization process. * * @param instance The user data sent for lbfgs() function by the client. * @param x The current values of variables. * @param g The current gradient values of variables. * @param fx The current value of the objective function. * @param xnorm The Euclidean norm of the variables. * @param gnorm The Euclidean norm of the gradients. * @param step The line-search step used for this iteration. * @param n The number of variables. * @param k The iteration count. * @param ls The number of evaluations called for this iteration. * @retval int Zero to continue the optimization process. Returning a * non-zero value will cancel the optimization process. */typedef int (*lbfgs_progress_t)( void *instance, const lbfgsfloatval_t *x, const lbfgsfloatval_t *g, const lbfgsfloatval_t fx, const lbfgsfloatval_t xnorm, const lbfgsfloatval_t gnorm, const lbfgsfloatval_t step, int n, int k, int ls );/*A user must implement a function compatible with ::lbfgs_evaluate_t (evaluationcallback) and pass the pointer to the callback function to lbfgs() arguments.Similarly, a user can implement a function compatible with ::lbfgs_progress_t(progress callback) to obtain the current progress (e.g., variables, functionvalue, ||G||, etc) and to cancel the iteration process if necessary.Implementation of a progress callback is optional: a user can pass \c NULL ifprogress notification is not necessary.In addition, a user must preserve two requirements: - The number of variables must be multiples of 16 (this is not 4). - The memory block of variable array ::x must be aligned to 16.This algorithm terminates an optimizationwhen: ||G|| < \epsilon \cdot \max(1, ||x||) .In this formula, ||.|| denotes the Euclidean norm.*//** * Start a L-BFGS optimization. * * @param n The number of variables. * @param x The array of variables. A client program can set * default values for the optimization and receive the * optimization result through this array. This array * must be allocated by ::lbfgs_malloc function * for libLBFGS built with SSE/SSE2 optimization routine * enabled. The library built without SSE/SSE2 * optimization does not have such a requirement. * @param ptr_fx The pointer to the variable that receives the final * value of the objective function for the variables. * This argument can be set to \c NULL if the final * value of the objective function is unnecessary. * @param proc_evaluate The callback function to provide function and * gradient evaluations given a current values of * variables. A client program must implement a * callback function compatible with \ref * lbfgs_evaluate_t and pass the pointer to the * callback function. * @param proc_progress The callback function to receive the progress * (the number of iterations, the current value of * the objective function) of the minimization * process. This argument can be set to \c NULL if * a progress report is unnecessary. * @param instance A user data for the client program. The callback * functions will receive the value of this argument. * @param param The pointer to a structure representing parameters for * L-BFGS optimization. A client program can set this * parameter to \c NULL to use the default parameters. * Call lbfgs_parameter_init() function to fill a * structure with the default values. * @retval int The status code. This function returns zero if the * minimization process terminates without an error. A * non-zero value indicates an error. */int lbfgs( int n, lbfgsfloatval_t *x, lbfgsfloatval_t *ptr_fx, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress, void *instance, lbfgs_parameter_t *param );/** * Initialize L-BFGS parameters to the default values. * * Call this function to fill a parameter structure with the default values * and overwrite parameter values if necessary. * * @param param The pointer to the parameter structure. */void lbfgs_parameter_init(lbfgs_parameter_t *param);/** * Allocate an array for variables. * * This function allocates an array of variables for the convenience of * ::lbfgs function; the function has a requreiemt for a variable array * when libLBFGS is built with SSE/SSE2 optimization routines. A user does * not have to use this function for libLBFGS built without SSE/SSE2 * optimization. * * @param n The number of variables. */lbfgsfloatval_t* lbfgs_malloc(int n);/** * Free an array of variables. * * @param x The array of variables allocated by ::lbfgs_malloc * function. */void lbfgs_free(lbfgsfloatval_t *x);/** @} */#ifdef __cplusplus}#endif/*__cplusplus*//**@mainpage libLBFGS: a library of Limited-memory Broyden-Fletcher-Goldfarb-Shanno (L-BFGS)@section intro IntroductionThis library is a C port of the implementation of Limited-memoryBroyden-Fletcher-Goldfarb-Shanno (L-BFGS) method written by Jorge Nocedal.The original FORTRAN source code is available at:http://www.ece.northwestern.edu/~nocedal/lbfgs.htmlThe L-BFGS method solves the unconstrainted minimization problem,<pre> minimize F(x), x = (x1, x2, ..., xN),</pre>only if the objective function F(x) and its gradient G(x) are computable. Thewell-known Newton's method requires computation of the inverse of the hessianmatrix of the objective function. However, the computational cost for theinverse hessian matrix is expensive especially when the objective functiontakes a large number of variables. The L-BFGS method iteratively finds aminimizer by approximating the inverse hessian matrix by information from lastm iterations. This innovation saves the memory storage and computational timedrastically for large-scaled problems.Among the various ports of L-BFGS, this library provides several features:- <b>Optimization with L1-norm (Orthant-Wise Limited-memory Quasi-Newton (OWL-QN) method)</b>: In addition to standard minimization problems, the library can minimize a function F(x) combined with L1-norm |x| of the variables, {F(x) + C |x|}, where C is a constant scalar parameter. This feature is useful for estimating parameters of sparse log-linear models (e.g., logistic regression and maximum entropy) with L1-regularization (or Laplacian prior).- <b>Clean C code</b>: Unlike C codes generated automatically by f2c (Fortran 77 into C converter), this port includes changes based on my interpretations, improvements, optimizations, and clean-ups so that the ported code would be well-suited for a C code. In addition to comments inherited from the original code, a number of comments were added through my interpretations.- <b>Callback interface</b>: The library receives function and gradient values via a callback interface. The library also notifies the progress of the optimization by invoking a callback function. In the original implementation, a user had to set function and gradient values every time the function returns for obtaining updated values.- <b>Thread safe</b>: The library is thread-safe, which is the secondary gain from the callback interface.- <b>Cross platform.</b> The source code can be compiled on Microsoft Visual Studio 2005, GNU C Compiler (gcc), etc.- <b>Configurable precision</b>: A user can choose single-precision (float) or double-precision (double) accuracy by changing ::LBFGS_FLOAT macro.- <b>SSE/SSE2 optimization</b>: This library includes SSE/SSE2 optimization (written in compiler intrinsics) for vector arithmetic operations on Intel/AMD processors. The library uses SSE for float values and SSE2 for double values. The SSE/SSE2 optimization routine is disabled by default.This library is used by:- <a href="http://www.chokkan.org/software/crfsuite/">CRFsuite: A fast implementation of Conditional Random Fields (CRFs)</a>- <a href="http://www.public.iastate.edu/~gdancik/mlegp/">mlegp: an R package for maximum likelihood estimates for Gaussian processes</a>- <a href="http://infmath.uibk.ac.at/~matthiasf/imaging2/">imaging2: the imaging2 class library</a>- <a href="http://search.cpan.org/~laye/Algorithm-LBFGS-0.16/">Algorithm::LBFGS - Perl extension for L-BFGS</a>@section download Download- <a href="http://www.chokkan.org/software/dist/liblbfgs-1.7.tar.gz">Source code</a>libLBFGS is distributed under the term of the<a href="http://opensource.org/licenses/mit-license.php">MIT license</a>.@section changelog History- Version 1.7 (2009-02-28): - Improved OWL-QN routines for stability. - Removed the support of OWL-QN method in MoreThuente algorithm because it accidentally fails in early stages of iterations for some objectives. Because of this change, <b>the OW-LQN method must be used with the backtracking algorithm (::LBFGS_LINESEARCH_BACKTRACKING)</b>, or the library returns ::LBFGSERR_INVALID_LINESEARCH. - Renamed line search algorithms as follows: - ::LBFGS_LINESEARCH_BACKTRACKING: regular Wolfe condition. - ::LBFGS_LINESEARCH_BACKTRACKING_LOOSE: regular Wolfe condition. - ::LBFGS_LINESEARCH_BACKTRACKING_STRONG: strong Wolfe condition. - Source code clean-up.- Version 1.6 (2008-11-02): - Improved line-search algorithm with strong Wolfe condition, which was contributed by Takashi Imamichi. This routine is now default for ::LBFGS_LINESEARCH_BACKTRACKING. The previous line search algorithm with regular Wolfe condition is still available as ::LBFGS_LINESEARCH_BACKTRACKING_LOOSE. - Configurable stop index for L1-norm computation. A member variable ::lbfgs_parameter_t::orthantwise_end was added to specify the index number at which the library stops computing the L1 norm of the variables. This is useful to prevent some variables from being regularized by the OW-LQN method. - A sample program written in C++ (sample/sample.cpp).- Version 1.5 (2008-07-10): - Configurable starting index for L1-norm computation. A member variable ::lbfgs_parameter_t::orthantwise_start was added to specify the index number from which the library computes the L1 norm of the variables. This is useful to prevent some variables from being regularized by the OWL-QN method. - Fixed a zero-division error when the initial variables have already been a minimizer (reported by Takashi Imamichi). In this case, the library returns ::LBFGS_ALREADY_MINIMIZED status code. - Defined ::LBFGS_SUCCESS status code as zero; removed unused constants, LBFGSFALSE and LBFGSTRUE. - Fixed a compile error in an implicit down-cast.- Version 1.4 (2008-04-25): - Configurable line search algorithms. A member variable ::lbfgs_parameter_t::linesearch was added to choose either MoreThuente method (::LBFGS_LINESEARCH_MORETHUENTE) or backtracking algorithm (::LBFGS_LINESEARCH_BACKTRACKING). - Fixed a bug: the previous version did not compute psuedo-gradients properly in the line search routines for OWL-QN. This bug might quit an iteration process too early when the OWL-QN routine was activated (0 < ::lbfgs_parameter_t::orthantwise_c). - Configure script for POSIX environments. - SSE/SSE2 optimizations with GCC. - New functions ::lbfgs_malloc and ::lbfgs_free to use SSE/SSE2 routines transparently. It is uncessary to use these functions for libLBFGS built without SSE/SSE2 routines; you can still use any memory allocators if SSE/SSE2 routines are disabled in libLBFGS.- Version 1.3 (2007-12-16): - An API change. An argument was added to lbfgs() function to receive the final value of the objective function. This argument can be set to \c NULL if the final value is unnecessary. - Fixed a null-pointer bug in the sample code (reported by Takashi Imamichi). - Added build scripts for Microsoft Visual Studio 2005 and GCC. - Added README file.- Version 1.2 (2007-12-13): - Fixed a serious bug in orthant-wise L-BFGS. An important variable was used without initialization.- Version 1.1 (2007-12-01): - Implemented orthant-wise L-BFGS. - Implemented lbfgs_parameter_init() function. - Fixed several bugs. - API documentation.- Version 1.0 (2007-09-20): - Initial release.@section api Documentation- @ref liblbfgs_api "libLBFGS API"@section sample Sample code@include sample.c@section ack AcknowledgementsThe L-BFGS algorithm is described in: - Jorge Nocedal. Updating Quasi-Newton Matrices with Limited Storage. <i>Mathematics of Computation</i>, Vol. 35, No. 151, pp. 773--782, 1980. - Dong C. Liu and Jorge Nocedal. On the limited memory BFGS method for large scale optimization. <i>Mathematical Programming</i> B, Vol. 45, No. 3, pp. 503-528, 1989.The line search algorithms used in this implementation are described in: - John E. Dennis and Robert B. Schnabel. <i>Numerical Methods for Unconstrained Optimization and Nonlinear Equations</i>, Englewood Cliffs, 1983. - Jorge J. More and David J. Thuente. Line search algorithm with guaranteed sufficient decrease. <i>ACM Transactions on Mathematical Software (TOMS)</i>, Vol. 20, No. 3, pp. 286-307, 1994.This library also implements Orthant-Wise Limited-memory Quasi-Newton (OWL-QN)method presented in: - Galen Andrew and Jianfeng Gao. Scalable training of L1-regularized log-linear models. In <i>Proceedings of the 24th International Conference on Machine Learning (ICML 2007)</i>, pp. 33-40, 2007.Special thanks go to Yoshimasa Tsuruoka and Daisuke Okanohara for technicalinformation about OWL-QN.Finally I would like to thank the original author, Jorge Nocedal, who has beendistributing the effieicnt and explanatory implementation in an open sourcelicence.@section reference Reference- <a href="http://www.ece.northwestern.edu/~nocedal/lbfgs.html">L-BFGS</a> by Jorge Nocedal.- <a href="http://research.microsoft.com/en-us/downloads/b1eb1016-1738-4bd5-83a9-370c9d498a03/default.aspx">Orthant-Wise Limited-memory Quasi-Newton Optimizer for L1-regularized Objectives</a> by Galen Andrew.- <a href="http://chasen.org/~taku/software/misc/lbfgs/">C port (via f2c)</a> by Taku Kudo.- <a href="http://www.alglib.net/optimization/lbfgs.php">C#/C++/Delphi/VisualBasic6 port</a> in ALGLIB.- <a href="http://cctbx.sourceforge.net/">Computational Crystallography Toolbox</a> includes <a href="http://cctbx.sourceforge.net/current_cvs/c_plus_plus/namespacescitbx_1_1lbfgs.html">scitbx::lbfgs</a>.*/#endif/*__LBFGS_H__*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -