Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/clementfarabet/lua---nnx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClement Farabet <clement.farabet@gmail.com>2011-10-05 02:24:53 +0400
committerClement Farabet <clement.farabet@gmail.com>2011-10-05 02:24:53 +0400
commit5176d884b2254d0fc317f2da28f0f62d821b8b29 (patch)
treef2e2f97cf25c54ef2b9b5b50b0b86ff267a8eb62
parentb14348d4e5206f5209d019ef68b1c3bc2421977a (diff)
parentbe2b2fbf9b19d322a4d7eab17610851742af9991 (diff)
Merge branch 'master' of github.com:clementfarabet/lua---nnx
-rw-r--r--lbfgs.c138
-rw-r--r--lbfgs.h228
-rw-r--r--test/test_cg.lua41
-rw-r--r--test/test_lbfgs.lua30
4 files changed, 339 insertions, 98 deletions
diff --git a/lbfgs.c b/lbfgs.c
index d27f79b..0874d07 100644
--- a/lbfgs.c
+++ b/lbfgs.c
@@ -85,9 +85,8 @@
#define max2(a, b) ((a) >= (b) ? (a) : (b))
#define max3(a, b, c) max2(max2((a), (b)), (c));
-/* extra globals */
+/* extra globals: counters, verbose flag */
static int nEvaluation = 0;
-static int maxEval = 0; /* maximum number of function evaluations */
static int nIteration = 0;
static int verbose = 0;
@@ -435,6 +434,10 @@ int lbfgs(
/* Evaluate the function value and its gradient. */
fx = cd.proc_evaluate(cd.instance, x, g, cd.n, 0);
+ if (verbose > 2){
+ printf("<lbfgs()>\n");
+ print_fxxdx(fx,x,g,cd.n);
+ }
if (0. != param.orthantwise_c) {
/* Compute the L1 norm of the variable and add it to the object value. */
xnorm = owlqn_x1norm(x, param.orthantwise_start, param.orthantwise_end);
@@ -505,7 +508,8 @@ int lbfgs(
veccpy(g, gp, n);
ret = ls;
if (verbose > 1){
- printf("Stopping b/c ls (%d) < 0\n", ls);
+ printf("<linesearch> Stopping b/c :\n");
+ print_lbfgs_error(ls);
}
goto lbfgs_exit;
}
@@ -522,7 +526,7 @@ int lbfgs(
if (cd.proc_progress) {
if ((ret = cd.proc_progress(cd.instance, x, g, fx, xnorm, gnorm, step, cd.n, k, ls))) {
if (verbose > 1){
- printf("Stopping b/c cd.proc_progress (%d)\n", ret);
+ printf("<lbfgs()> Stopping b/c cd.proc_progress (%d)\n", ret);
}
goto lbfgs_exit;
}
@@ -531,7 +535,7 @@ int lbfgs(
/* Count number of function evaluations */
if ((param.max_evaluations != 0)&&(nEvaluation > param.max_evaluations)) {
if (verbose > 1){
- printf("Stopping b/c exceeded max number of function evaluations\n");
+ printf("<lbfgs()> Stopping b/c exceeded max number of function evaluations\n");
}
ret = LBFGSERR_MAXIMUMEVALUATION;
goto lbfgs_exit;
@@ -544,7 +548,7 @@ int lbfgs(
if (xnorm < 1.0) xnorm = 1.0;
if (gnorm / xnorm <= param.epsilon) {
if (verbose > 1){
- printf("Stopping b/c gnorm(%f)/xnorm(%f) <= param.epsilon (%f)\n",
+ printf("<lbfgs()> Stopping b/c gnorm(%f)/xnorm(%f) <= param.epsilon (%f)\n",
gnorm, xnorm, param.epsilon);
}
/* Convergence. */
@@ -566,7 +570,7 @@ int lbfgs(
/* The stopping criterion. */
if (rate < param.delta) {
if (verbose > 1){
- printf("Stopping b/c rate (%f) < param.delta (%f)\n",
+ printf("<lbfgs()> Stopping b/c rate (%f) < param.delta (%f)\n",
rate, param.delta);
}
ret = LBFGS_STOP;
@@ -580,7 +584,7 @@ int lbfgs(
if (param.max_iterations != 0 && param.max_iterations < k+1) {
if (verbose > 1){
- printf("Stopping b/c param.max_iterations (%d) < k+1 (%d)\n",
+ printf("<lbfgs()> Stopping b/c param.max_iterations (%d) < k+1 (%d)\n",
param.max_iterations, k+1);
}
/* Maximum number of iterations. */
@@ -673,6 +677,10 @@ int lbfgs(
*ptr_fx = fx;
}
+ if(verbose){
+ printf("<lbfgs()>\n");
+ print_lbfgs_error(ret);
+ }
vecfree(pf);
/* Free memory blocks used by this function. */
@@ -762,7 +770,7 @@ int cg(
if (xp == NULL || g == NULL || gp == NULL ||
d == NULL || dp == NULL || w == NULL || tmp == NULL) {
ret = LBFGSERR_OUTOFMEMORY;
- goto lbfgs_exit;
+ goto cg_exit;
}
/* Allocate an array for storing previous values of the objective function. */
@@ -772,7 +780,10 @@ int cg(
/* Evaluate the function value and its gradient. */
fx = cd.proc_evaluate(cd.instance, x, g, cd.n, 0);
-
+ if (verbose > 2){
+ printf("<cg()>\n");
+ print_fxxdx(fx,x,g,cd.n);
+ }
/* used to compute the momentum term for CG */
vecdot(&gtg,g,g,n);
@@ -796,7 +807,7 @@ int cg(
if (xnorm < 1.0) xnorm = 1.0;
if (gnorm / xnorm <= param.epsilon) {
ret = LBFGS_ALREADY_MINIMIZED;
- goto lbfgs_exit;
+ goto cg_exit;
}
/* Compute the initial step:
@@ -824,9 +835,10 @@ int cg(
veccpy(g, gp, n);
ret = ls;
if (verbose > 1){
- printf("Stopping b/c ls (%d) < 0\n", ls);
+ printf("<linesearch()> Stopping b/c :\n");
+ print_lbfgs_error(ls);
}
- goto lbfgs_exit;
+ // goto cg_exit;
}
/* Compute x and g norms. */
@@ -837,18 +849,18 @@ int cg(
if (cd.proc_progress) {
if ((ret = cd.proc_progress(cd.instance, x, g, fx, xnorm, gnorm, step, cd.n, k, ls))) {
if (verbose > 1){
- printf("Stopping b/c cd.proc_progress (%d)\n", ret);
+ printf("<cg()> Stopping b/c cd.proc_progress (%d)\n", ret);
}
- goto lbfgs_exit;
+ goto cg_exit;
}
}
/* Count number of function evaluations */
- if ((maxEval != 0)&&(nEvaluation > maxEval)) {
+ if ((param.max_evaluations != 0)&&(nEvaluation > param.max_evaluations)) {
if (verbose > 1){
- printf("Stopping b/c exceeded max number of function evaluations\n");
+ printf("<cg()> Stopping b/c exceeded max number of function evaluations\n");
}
- goto lbfgs_exit;
+ goto cg_exit;
}
/*
Convergence test.
@@ -858,7 +870,7 @@ int cg(
if (xnorm < 1.0) xnorm = 1.0;
if (gnorm / xnorm <= param.epsilon) {
if (verbose > 1){
- printf("Stopping b/c gnorm(%f)/xnorm(%f) <= param.epsilon (%f)\n",
+ printf("<cg()> Stopping b/c gnorm(%f)/xnorm(%f) <= param.epsilon (%f)\n",
gnorm, xnorm, param.epsilon);
}
/* Convergence. */
@@ -880,7 +892,7 @@ int cg(
/* The stopping criterion. */
if (rate < param.delta) {
if (verbose > 1){
- printf("Stopping b/c rate (%f) < param.delta (%f)\n",
+ printf("<cg()> Stopping b/c rate (%f) < param.delta (%f)\n",
rate, param.delta);
}
ret = LBFGS_STOP;
@@ -894,7 +906,7 @@ int cg(
if (param.max_iterations != 0 && param.max_iterations < k+1) {
if (verbose > 1){
- printf("Stopping b/c param.max_iterations (%d) < k+1 (%d)\n",
+ printf("<cg()> Stopping b/c param.max_iterations (%d) < k+1 (%d)\n",
param.max_iterations, k+1);
}
/* Maximum number of iterations. */
@@ -965,12 +977,16 @@ int cg(
step = 1.0;
}
- lbfgs_exit:
+ cg_exit:
/* Return the final value of the objective function. */
if (ptr_fx != NULL) {
*ptr_fx = fx;
}
+ if(verbose){
+ print_lbfgs_error(ret);
+ }
+
vecfree(pf);
vecfree(pg);
vecfree(w);
@@ -1027,6 +1043,12 @@ static int line_search_backtracking(
/* Evaluate the function and gradient values. */
*f = cd->proc_evaluate(cd->instance, x, g, cd->n, *stp);
+ if (verbose > 2){
+ printf("<line_search_backtracking()>\n");
+ print_linesearch_type(param->linesearch);
+ print_fxxdx(*f,x,g,cd->n);
+ }
+
++count;
if (*f > finit + *stp * dgtest) {
@@ -1116,6 +1138,12 @@ static int line_search_backtracking_owlqn(
/* Evaluate the function and gradient values. */
*f = cd->proc_evaluate(cd->instance, x, g, cd->n, *stp);
+ if (verbose > 2){
+ printf("<line_search_backtracking_owlqn()>\n");
+ print_linesearch_type(param->linesearch);
+ print_fxxdx(*f,x,g,cd->n);
+ }
+
/* Compute the L1 norm of the variables and add it to the object value. */
norm = owlqn_x1norm(x, param->orthantwise_start, param->orthantwise_end);
*f += norm * param->orthantwise_c;
@@ -1243,6 +1271,13 @@ static int line_search_morethuente(
/* Evaluate the function and gradient values. */
*f = cd->proc_evaluate(cd->instance, x, g, cd->n, *stp);
+
+ if (verbose > 2){
+ printf("<line_search_morethuente()>\n");
+ print_linesearch_type(param->linesearch);
+ print_fxxdx(*f,x,g,cd->n);
+ }
+
vecdot(&dg, g, s, n);
ftest1 = finit + *stp * dgtest;
@@ -1793,9 +1828,11 @@ static int cg_progress(void *instance,
{
nIteration = k;
if (verbose > 1) {
- printf("<CGOptimization> iteration %d:\n", nIteration);
- printf(" + f(X) = %f\n", fx);
- printf(" + xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
+ printf("<cg()> iteration %d:\n", nIteration);
+ if (verbose > 2){
+ print_fxxdx(fx,x,g,n);
+ printf(" + xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
+ }
printf(" + nb evaluations = %d\n", nEvaluation);
}
return 0;
@@ -1814,9 +1851,11 @@ static int lbfgs_progress(void *instance,
{
nIteration = k;
if (verbose > 1) {
- printf("<LBFGSOptimization> iteration %d:\n", nIteration);
- printf(" + f(X) = %f\n", fx);
- printf(" + xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
+ printf("<lbfgs()> iteration %d:\n", nIteration);
+ if (verbose > 2){
+ print_fxxdx(fx,x,g,n);
+ printf(" + xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
+ }
printf(" + nb evaluations = %d\n", nEvaluation);
}
return 0;
@@ -1835,7 +1874,7 @@ int lbfgs_init(lua_State *L){
/* now load the common parameter and gradient vectors */
init(L);
- return 0;
+ return 0;
}
int cg_init(lua_State *L){
@@ -1917,6 +1956,31 @@ int clear(lua_State *L) {
lbfgs_free(x);
return 0;
}
+int print_fxxdx (lbfgsfloatval_t fx,
+ const lbfgsfloatval_t *x,
+ const lbfgsfloatval_t *dx,
+ int n){
+ printf(" + fx = %f\n", fx);
+ if (nParameter > 10) {
+ printf(" + x = [%f, %f, %f, ..., %f, %f ,%f]\n",
+ x[0],x[1],x[2],x[n-3],x[n-2],x[n-1]);
+ printf(" + dx = [%f, %f, %f, ..., %f, %f , %f]\n",
+ dx[0],dx[1],dx[2],dx[n-3],dx[n-2],dx[n-1]);
+ } else {
+ int i;
+ printf(" + x = [%f", x[0]);
+ for (i=1; i<n;i++) {
+ printf(", %f", x[i]);
+ }
+ printf("]\n");
+ printf(" + dx = [%f", dx[0]);
+ for (i=1; i<n;i++) {
+ printf(", %f", dx[i]);
+ }
+ printf("]\n");
+ }
+}
+
int lbfgs_run(lua_State *L) {
/* check existence of x */
@@ -1933,10 +1997,12 @@ int lbfgs_run(lua_State *L) {
/* verbose */
if (verbose) {
- printf("<LBFGSOptimization> batch optimized after %d iterations\n", nIteration);
- printf(" + f(X) = %f\n", fx);
- printf(" + X = [%f , ... %f]\n",x[0],x[nParameter-1]);
+ printf("<lbfgs_run()> batch optimized after %d iterations\n", nIteration);
printf(" + nb evaluations = %d\n", nEvaluation);
+ if (verbose > 1){
+ print_fxxdx(fx,x,gradParameters,nParameter);
+ print_linesearch_type(lbfgs_param.linesearch);
+ }
}
/* return current error */
@@ -1959,12 +2025,14 @@ int cg_run(lua_State *L) {
/* verbose */
if (verbose) {
- printf("<CGOptimization> batch optimized after %d iterations\n", nIteration);
- printf(" + f(X) = %f\n", fx);
- printf(" + X = [%f , ... %f]\n",x[0],x[nParameter-1]);
+ printf("<cg_run()> batch optimized after %d iterations\n", nIteration);
printf(" + nb evaluations = %d\n", nEvaluation);
printf(" + linesearch = %d , momentum = %d\n",
lbfgs_param.linesearch, lbfgs_param.momentum);
+ if (verbose > 1){
+ print_fxxdx(fx,x,gradParameters,nParameter);
+ print_linesearch_type(lbfgs_param.linesearch);
+ }
}
/* return current error */
diff --git a/lbfgs.h b/lbfgs.h
index 1addaf0..ecae23f 100644
--- a/lbfgs.h
+++ b/lbfgs.h
@@ -150,6 +150,193 @@ extern "C" {
CGERR_INVALID_MOMENTUM,
};
+ void print_lbfgs_error(int lbfgs_err) {
+ printf(" + error/return code : ");
+ if (lbfgs_err == LBFGS_SUCCESS) {
+ printf("LBFGS_SUCCESS and LBFGS_CONVERGENCE\n");
+ printf(" L-BFGS reaches convergence.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGS_STOP) {
+ printf("LBFGS_STOP\n");
+ return;
+ }
+ if (lbfgs_err == LBFGS_ALREADY_MINIMIZED) {
+ printf("LBFGS_ALREADY_MINIMIZED\n");
+ printf(" The initial variables already minimize the objective function.\n");
+ return;
+ }
+
+ if (lbfgs_err == LBFGSERR_UNKNOWNERROR) {
+ printf("LBFGSERR_UNKNOWNERROR\n");
+ printf(" Unknown error.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_LOGICERROR) {
+ printf("LBFGSERR_LOGICERROR\n");
+ printf(" Logic error.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_OUTOFMEMORY) {
+ printf("LBFGSERR_OUTOFMEMORY\n");
+ printf(" Insufficient memory.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_CANCELED) {
+ printf("LBFGSERR_CANCELED\n");
+ printf(" The minimization process has been canceled.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_N) {
+ printf("LBFGSERR_INVALID_N\n");
+ printf(" Invalid number of variables specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_N_SSE) {
+ printf("LBFGSERR_INVALID_N_SSE\n");
+ printf(" Invalid number of variables (for SSE) specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_X_SSE) {
+ printf("LBFGSERR_INVALID_X_SSE\n");
+ printf(" The array x must be aligned to 16 (for SSE).\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_EPSILON) {
+ printf("LBFGSERR_INVALID_EPSILON\n");
+ printf(" Invalid parameter lbfgs_parameter_t::epsilon specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_TESTPERIOD) {
+ printf("LBFGSERR_INVALID_TESTPERIOD\n");
+ printf(" Invalid parameter lbfgs_parameter_t::past specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_DELTA) {
+ printf("LBFGSERR_INVALID_DELTA\n");
+ printf(" Invalid parameter lbfgs_parameter_t::delta specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_LINESEARCH) {
+ printf("LBFGSERR_INVALID_LINESEARCH\n");
+ printf(" Invalid parameter lbfgs_parameter_t::linesearch specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_MINSTEP) {
+ printf("LBFGSERR_INVALID_MINSTEP\n");
+ printf(" Invalid parameter lbfgs_parameter_t::max_step specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_MAXSTEP) {
+ printf("LBFGSERR_INVALID_MAXSTEP\n");
+ printf(" Invalid parameter lbfgs_parameter_t::max_step specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_FTOL) {
+ printf("LBFGSERR_INVALID_FTOL\n");
+ printf(" Invalid parameter lbfgs_parameter_t::ftol specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_WOLFE) {
+ printf("LBFGSERR_INVALID_WOLFE\n");
+ printf(" Invalid parameter lbfgs_parameter_t::wolfe specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_GTOL) {
+ printf("LBFGSERR_INVALID_GTOL\n");
+ printf(" Invalid parameter lbfgs_parameter_t::gtol specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_XTOL) {
+ printf("LBFGSERR_INVALID_XTOL\n");
+ printf(" Invalid parameter lbfgs_parameter_t::xtol specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_MAXLINESEARCH) {
+ printf("LBFGSERR_INVALID_MAXLINESEARCH\n");
+ printf(" Invalid parameter lbfgs_parameter_t::max_linesearch specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_ORTHANTWISE) {
+ printf("LBFGSERR_INVALID_ORTHANTWISE\n");
+ printf(" Invalid parameter lbfgs_parameter_t::orthantwise_c specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_ORTHANTWISE_START) {
+ printf("LBFGSERR_INVALID_ORTHANTWISE_START\n");
+ printf(" Invalid parameter lbfgs_parameter_t::orthantwise_start specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALID_ORTHANTWISE_END) {
+ printf("LBFGSERR_INVALID_ORTHANTWISE_END\n");
+ printf(" Invalid parameter lbfgs_parameter_t::orthantwise_end specified.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_OUTOFINTERVAL) {
+ printf("LBFGSERR_OUTOFINTERVAL\n");
+ printf(" The line-search step went out of the interval of uncertainty.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INCORRECT_TMINMAX) {
+ printf("LBFGSERR_INCORRECT_TMINMAX\n");
+ printf(" A logic error occurred; alternatively, the interval of uncertainty became too small.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_ROUNDING_ERROR) {
+ printf("LBFGSERR_ROUNDING_ERROR\n");
+ printf(" A rounding error occurred; alternatively, no line-search step satisfies the sufficient decrease and curvature conditions.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_MINIMUMSTEP) {
+ printf("LBFGSERR_MINIMUMSTEP\n");
+ printf(" The line-search step became smaller than lbfgs_parameter_t::min_step.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_MAXIMUMSTEP) {
+ printf("LBFGSERR_MAXIMUMSTEP\n");
+ printf(" The line-search step became larger than lbfgs_parameter_t::max_step.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_MAXIMUMLINESEARCH) {
+ printf("LBFGSERR_MAXIMUMLINESEARCH\n");
+ printf(" The line-search routine reaches the maximum number of evaluations.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_MAXIMUMITERATION) {
+ printf("LBFGSERR_MAXIMUMITERATION\n");
+ printf(" The algorithm routine reaches the maximum number of iterations.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_MAXIMUMEVALUATION) {
+ printf("LBFGSERR_MAXIMUMEVALUATION\n");
+ printf(" The algorithm routine reaches the maximum number of iterations.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_WIDTHTOOSMALL) {
+ printf("LBFGSERR_WIDTHTOOSMALL\n");
+ printf(" Relative width of the interval of uncertainty is at most lbfgs_parameter_t::xtol.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INVALIDPARAMETERS) {
+ printf("LBFGSERR_INVALIDPARAMETERS\n");
+ printf(" A logic error (negative line-search step) occurred.\n");
+ return;
+ }
+ if (lbfgs_err == LBFGSERR_INCREASEGRADIENT) {
+ printf("LBFGSERR_INCREASEGRADIENT\n");
+ printf(" The current search direction increases the objective function value.\n");
+ return;
+ }
+ if (lbfgs_err == CGERR_INVALID_MOMENTUM) {
+ printf("CGERR_INVALID_MOMENTUM\n");
+ printf(" Invalid momentum type for CG\n");
+ return;
+ }
+ printf("(LBFGS|CG)ERR\n");
+ printf(" not known, not in enum\n");
+ return;
+ };
+
/**
* Line search algorithms.
*/
@@ -194,28 +381,29 @@ extern "C" {
LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 3,
};
+
/**
* CG momentum terms
*/
-
- enum {
+
+ enum {
/* beta = (g'*g)/(gotgo); */
CG_DEFAULT = 0,
CG_FLETCHER_REEVES = 0,
-
+
/* beta = (g'*(g-g_old)) /(gotgo);*/
CG_POLAK_RIBIERE = 1,
-
- /* beta = (g'*(g-g_old))/((g-g_old)'*d); */
+
+ /* beta = (g'*(g-g_old))/((g-g_old)'*d); */
CG_HESTENES_STIEFEL = 2,
-
+
/* beta_FR = (g'*(g-g_old)) /(gotgo); */
/* beta_PR = (g'*g-gtgo)/(gotgo); */
/* beta = max(-beta_FR,min(beta_PR,beta_FR)); */
CG_GILBERT_NOCEDAL = 3,
};
-
+
/**
* L-BFGS optimization parameters.
* Call lbfgs_parameter_init() function to initialize parameters to the
@@ -394,12 +582,34 @@ extern "C" {
/**
* momentum criteria : by what value do you multiply the previous
* gradient to the current: CG_FLETCHER_REEVES,etc.
- */
+ */
int momentum;
-
+
} lbfgs_parameter_t;
+ void print_linesearch_type (int ls_type){
+ printf(" + linesearch : ");
+ if (ls_type == LBFGS_LINESEARCH_MORETHUENTE ) {
+ printf("LBFGS_LINESEARCH_MORETHUENTE\n");
+ return;
+ }
+ if (ls_type == LBFGS_LINESEARCH_BACKTRACKING_ARMIJO ) {
+ printf("LBFGS_LINESEARCH_BACKTRACKING_ARMIJO\n");
+ return;
+ }
+ if (ls_type == LBFGS_LINESEARCH_BACKTRACKING_WOLFE ) {
+ printf("LBFGS_LINESEARCH_BACKTRACKING_WOLFE\n");
+ return;
+ }
+ if (ls_type == LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE ) {
+ printf("LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE\n");
+ return;
+ }
+ printf("LBFGS_LINESEARCH_UNKNOWN\n");
+ return;
+ }
+
/**
* Callback interface to provide objective function and gradient evaluations.
*
diff --git a/test/test_cg.lua b/test/test_cg.lua
index 3803628..a799f2e 100644
--- a/test/test_cg.lua
+++ b/test/test_cg.lua
@@ -2,50 +2,31 @@ dofile('rosenbrock.lua')
dofile('l2.lua')
require 'liblbfgs'
-neval = 0
+
maxIterations = 100
maxLineSearch = 40
-linesearch = 0
+maxEvaluation = 25
+linesearch = 2
momentum = 0
-verbose = 2
+verbose = 3
nparam = 2
+
local testfunc = rosenbrock
-local parameters = torch.Tensor(nparam):fill(0.1)
+local parameters = torch.Tensor(nparam):zero()
local gradParameters = torch.Tensor(nparam):zero()
output, gradParameters = testfunc(parameters,gradParameters)
-function printstats ()
- print('nEval: '..neval)
- print('+ fx: '..output)
- local xstring = string.format("%2.2f",parameters[1])
- for i = 2,parameters:size(1) do
- xstring = string.format("%s, %2.2f", xstring, parameters[i])
- end
- print('+ x: ['..xstring..']')
- local dxstring = string.format("%2.2f",gradParameters[1])
- for i = 2,gradParameters:size(1) do
- dxstring = string.format("%s, %2.2f", dxstring, gradParameters[i])
- end
-
- print('+ dx: ['..dxstring..']')
-end
-print('Starting:')
-printstats()
-lbfgs.evaluate
+lbfgs.evaluate
= function()
- output, gradParameters = testfunc(parameters,gradParameters)
- neval = neval + 1
- printstats()
- return output
+ output, gradParameters = testfunc(parameters,gradParameters)
+ return output
end
-- init CG state
cg.init(parameters, gradParameters,
- maxEvaluation, maxIterations, maxLineSearch,
- sparsity, linesearch, verbose)
+ maxEvaluation, maxIterations, maxLineSearch,
+ momentum, linesearch, verbose)
output = cg.run()
-
-printstats()
diff --git a/test/test_lbfgs.lua b/test/test_lbfgs.lua
index f882361..b6eea8f 100644
--- a/test/test_lbfgs.lua
+++ b/test/test_lbfgs.lua
@@ -2,42 +2,25 @@ dofile('rosenbrock.lua')
dofile('l2.lua')
require 'liblbfgs'
-neval = 0
maxIterations = 100
maxLineSearch = 40
+-- this is to compare with minFunc
+maxEvaluation = 25
linesearch = 2
sparsity = 0
-verbose = 2
-nparam = 8
+verbose = 3
+nparam = 2
+
local testfunc = rosenbrock
-local parameters = torch.Tensor(nparam):fill(0.1)
+local parameters = torch.Tensor(nparam):zero()
local gradParameters = torch.Tensor(nparam):zero()
output, gradParameters = testfunc(parameters,gradParameters)
-function printstats ()
- print('nEval: '..neval)
- print('+ fx: '..output)
- local xstring = ""
- for i = 1,parameters:size(1) do
- xstring = string.format("%s, %2.2f", xstring, parameters[i])
- end
- print('+ x: ['..xstring..']')
- local dxstring = ""
- for i = 1,gradParameters:size(1) do
- dxstring = string.format("%s, %2.2f", dxstring, gradParameters[i])
- end
-
- print('+ dx: ['..dxstring..']')
-end
-print('Starting:')
-printstats()
lbfgs.evaluate
= function()
output, gradParameters = testfunc(parameters,gradParameters)
- neval = neval + 1
- printstats()
return output
end
@@ -48,4 +31,3 @@ lbfgs.init(parameters, gradParameters,
output = lbfgs.run()
-printstats()