From 33d4601da5e2e715260619a38f5899645d157952 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Tue, 8 May 2012 19:45:10 -0400 Subject: switch to liblbfgs for pro --- training/liblbfgs/lbfgs++.h | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'training/liblbfgs/lbfgs++.h') diff --git a/training/liblbfgs/lbfgs++.h b/training/liblbfgs/lbfgs++.h index 119511e5..6c4d1807 100644 --- a/training/liblbfgs/lbfgs++.h +++ b/training/liblbfgs/lbfgs++.h @@ -9,9 +9,10 @@ #define __LBFGSPP_H__ #include +#include #include "liblbfgs/lbfgs.h" -// Function must be lbfgsfloatval_t f(const double* x_start, double* g_start) +// Function must be double f(const vector& x_start, double* g_start) template class LBFGS { public: @@ -46,11 +47,14 @@ class LBFGS { lbfgsfloatval_t& operator[](size_t i) { return m_x[i]; } size_t size() const { return m_x.size(); } - int Optimize() { + int MinimizeFunction(bool s = false) { + silence = s; lbfgsfloatval_t fx; int ret = lbfgs(m_x.size(), &m_x[0], &fx, _evaluate, _progress, this, ¶m); - std::cerr << "L-BFGS optimization terminated with status code = " << ret << std::endl; - std::cerr << " fx = " << fx << std::endl; + if (!silence) { + std::cerr << "L-BFGS optimization terminated with status code = " << ret << std::endl; + std::cerr << " fx = " << fx << std::endl; + } return ret; } @@ -62,6 +66,7 @@ class LBFGS { param.linesearch = LBFGS_LINESEARCH_BACKTRACKING; param.orthantwise_c = 1.0; } + silence = false; } static lbfgsfloatval_t _evaluate( @@ -79,7 +84,8 @@ class LBFGS { const lbfgsfloatval_t step) { (void) n; (void) step; - return func(x, g); + assert(x == &m_x[0]); // sanity check, ensures pass m_x is okay + return func(m_x, g); } static int _progress( @@ -109,21 +115,23 @@ class LBFGS { int n, int k, int ls - ) - { - (void) n; - (void) k; - std::cerr << "Iteration " << k << ':' << std::endl; - std::cerr << " fx = " << fx << ", x[0] = " << x[0] << ", x[1] = " << x[1] << std::endl; - std::cerr << " xnorm = " << xnorm << ", gnorm = " << gnorm << ", step = " << step << std::endl << std::endl; - return 0; + ) { + (void) x; + (void) g; + (void) n; + (void) ls; + if (!silence) { + std::cerr << "Iteration " << k << ':' << "\tfx = " << fx << "\t" + << " xnorm = " << xnorm << ", gnorm = " << gnorm << ", step = " << step << std::endl; } + return 0; + } std::vector* p_x; const bool owned; std::vector& m_x; const Function& func; lbfgs_parameter_t param; - + bool silence; }; #endif -- cgit v1.2.3