diff options
author | Marco Scoffier <github@metm.org> | 2011-09-29 06:59:18 +0400 |
---|---|---|
committer | Marco Scoffier <github@metm.org> | 2011-09-29 06:59:18 +0400 |
commit | 45ecd9e486f8cc58a84a6ee9eff0d27db3070b0f (patch) | |
tree | 67ee2f91285037f2edc1994b15d60784f5777f0c | |
parent | cec0ceb6b3f4e5c68e44e6935a61bca6641b9101 (diff) |
added flags for linesearch
-rw-r--r-- | CGOptimization.lua | 39 | ||||
-rw-r--r-- | LBFGSOptimization.lua | 39 | ||||
-rw-r--r-- | lbfgs.c | 48 |
3 files changed, 76 insertions, 50 deletions
diff --git a/CGOptimization.lua b/CGOptimization.lua index 6eee58c..31d5e89 100644 --- a/CGOptimization.lua +++ b/CGOptimization.lua @@ -4,22 +4,35 @@ function CG:__init(...) require 'liblbfgs' parent.__init(self, ...) xlua.unpack_class(self, {...}, - 'CGOptimization', nil, - {arg='maxEvaluation', type='number', - help='maximum nb of function evaluations per pass (0 = no max)', default=0}, - {arg='maxIterations', type='number', - help='maximum nb of iterations per pass (0 = no max)', default=0}, - {arg='maxLineSearch', type='number', - help='maximum nb of steps in line search', default=20}, - {arg='sparsity', type='number', - help='sparsity coef (Orthantwise C)', default=0}, - {arg='parallelize', type='number', - help='parallelize onto N cores (experimental!)', default=1} - ) + 'CGOptimization', nil, + {arg='maxEvaluation', type='number', + help='maximum nb of function evaluations per pass (0 = no max)', default=0}, + {arg='maxIterations', type='number', + help='maximum nb of iterations per pass (0 = no max)', default=0}, + {arg='maxLineSearch', type='number', + help='maximum nb of steps in line search', default=20}, + {arg='sparsity', type='number', + help='sparsity coef (Orthantwise C)', default=0}, + {arg='linesearch', type='string', + help='type of linesearch used: morethuente, armijo, wolfe, strong_wolfe', + default='wolfe'}, + {arg='parallelize', type='number', + help='parallelize onto N cores (experimental!)', default=1} + ) + local linesearch = 2 + if not (self.linesearch == 'wolfe') then + if self.linesearch == 'morethuente' then + linesearch = 0 + elseif self.linesearch == 'armijo' then + linesearch = 1 + elseif self.linesearch == 'strong_wolfe' then + linesearch = 3 + end + end -- init CG state cg.init(self.parameters, self.gradParameters, self.maxEvaluation, self.maxIterations, self.maxLineSearch, - self.sparsity, self.verbose) + self.sparsity, linesearch, self.verbose) end function CG:optimize() diff --git a/LBFGSOptimization.lua b/LBFGSOptimization.lua index 9fd0a3f..53dfe70 100644 --- a/LBFGSOptimization.lua +++ b/LBFGSOptimization.lua @@ -4,22 +4,35 @@ function LBFGS:__init(...) require 'liblbfgs' parent.__init(self, ...) xlua.unpack_class(self, {...}, - 'LBFGSOptimization', nil, - {arg='maxEvaluation', type='number', - help='maximum nb of function evaluations per pass (0 = no max)', default=0}, - {arg='maxIterations', type='number', - help='maximum nb of iterations per pass (0 = no max)', default=0}, - {arg='maxLineSearch', type='number', - help='maximum nb of steps in line search', default=20}, - {arg='sparsity', type='number', - help='sparsity coef (Orthantwise C)', default=0}, - {arg='parallelize', type='number', - help='parallelize onto N cores (experimental!)', default=1} - ) + 'LBFGSOptimization', nil, + {arg='maxEvaluation', type='number', + help='maximum nb of function evaluations per pass (0 = no max)', default=0}, + {arg='maxIterations', type='number', + help='maximum nb of iterations per pass (0 = no max)', default=0}, + {arg='maxLineSearch', type='number', + help='maximum nb of steps in line search', default=20}, + {arg='sparsity', type='number', + help='sparsity coef (Orthantwise C)', default=0}, + {arg='linesearch', type='string', + help='type of linesearch used: morethuente, armijo, wolfe, strong_wolfe', + default='wolfe'}, + {arg='parallelize', type='number', + help='parallelize onto N cores (experimental!)', default=1} + ) + local linesearch = 2 + if not (self.linesearch == 'wolfe') then + if self.linesearch == 'morethuente' then + linesearch = 0 + elseif self.linesearch == 'armijo' then + linesearch = 1 + elseif self.linesearch == 'strong_wolfe' then + linesearch = 3 + end + end -- init LBFGS state lbfgs.init(self.parameters, self.gradParameters, self.maxEvaluation, self.maxIterations, self.maxLineSearch, - self.sparsity, self.verbose) + self.sparsity, linesearch, self.verbose) end function LBFGS:optimize() @@ -945,7 +945,7 @@ int cg( /* compute 'momentum' term B = (g1'*g1)/(g0'*g0) */ vecdot(&g1dot, g, g, n); - B = g1dot / g0dot; + B = g1dot / g0dot; /* store val for next iteration */ g0dot = g1dot; @@ -956,9 +956,9 @@ int cg( /* add the 'momentum' term */ /* d_1 = -g_1 + B*d_0 */ vecadd(d, dp, B, n); - + /* increment the number of iterations */ - ++k; + ++k; /* Now the search direction d is ready. We try step = 1 first. @@ -1787,15 +1787,15 @@ static lbfgsfloatval_t evaluate(void *instance, } static int cg_progress(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) + 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) { nIteration = k; if (verbose > 1) { @@ -1808,15 +1808,15 @@ static int cg_progress(void *instance, } static int lbfgs_progress(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) + 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) { nIteration = k; if (verbose > 1) { @@ -1884,10 +1884,10 @@ int init(lua_State *L) { maxEval = lua_tonumber(L,3); lbfgs_param.max_iterations = lua_tonumber(L, 4); lbfgs_param.max_linesearch = lua_tonumber(L, 5); - lbfgs_param.linesearch = LBFGS_LINESEARCH_BACKTRACKING; - lbfgs_param.orthantwise_c = lua_tonumber(L, 6); + lbfgs_param.orthantwise_c = lua_tonumber(L, 6); + lbfgs_param.linesearch = lua_tonumber(L, 7); /* get verbose level */ - verbose = lua_tonumber(L,7); + verbose = lua_tonumber(L,8); /* done */ return 0; |