summaryrefslogtreecommitdiff
path: root/training/liblbfgs
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2012-05-08 19:45:10 -0400
committerChris Dyer <cdyer@cs.cmu.edu>2012-05-08 19:45:10 -0400
commit33d4601da5e2e715260619a38f5899645d157952 (patch)
treef5828cf0906e907c88dd9c3ed7643e9983f62a56 /training/liblbfgs
parentc168daa1e801a8a0be4d0c16311ae30b06a43b82 (diff)
switch to liblbfgs for pro
Diffstat (limited to 'training/liblbfgs')
-rw-r--r--training/liblbfgs/lbfgs++.h36
-rw-r--r--training/liblbfgs/ll_test.cc9
2 files changed, 26 insertions, 19 deletions
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 <vector>
+#include <cassert>
#include "liblbfgs/lbfgs.h"
-// Function must be lbfgsfloatval_t f(const double* x_start, double* g_start)
+// Function must be double f(const vector<double>& x_start, double* g_start)
template <typename Function>
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, &param);
- 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<lbfgsfloatval_t>* p_x;
const bool owned;
std::vector<lbfgsfloatval_t>& m_x;
const Function& func;
lbfgs_parameter_t param;
-
+ bool silence;
};
#endif
diff --git a/training/liblbfgs/ll_test.cc b/training/liblbfgs/ll_test.cc
index 058db716..43c0f214 100644
--- a/training/liblbfgs/ll_test.cc
+++ b/training/liblbfgs/ll_test.cc
@@ -4,12 +4,11 @@
using namespace std;
// Function must be lbfgsfloatval_t f(x.begin, x.end, g.begin)
-lbfgsfloatval_t func(const lbfgsfloatval_t* x, lbfgsfloatval_t* g) {
+lbfgsfloatval_t func(const vector<lbfgsfloatval_t>& x, lbfgsfloatval_t* g) {
int i;
lbfgsfloatval_t fx = 0.0;
- int n = 4;
- for (i = 0;i < n;i += 2) {
+ for (i = 0;i < x.size();i += 2) {
lbfgsfloatval_t t1 = 1.0 - x[i];
lbfgsfloatval_t t2 = 10.0 * (x[i+1] - x[i] * x[i]);
g[i+1] = 20.0 * t2;
@@ -21,8 +20,8 @@ lbfgsfloatval_t func(const lbfgsfloatval_t* x, lbfgsfloatval_t* g) {
template<typename F>
void Opt(F& f) {
- LBFGS<F> lbfgs(4, f, 1.0);
- lbfgs.Optimize();
+ LBFGS<F> lbfgs(4, f);
+ lbfgs.MinimizeFunction();
}
int main(int argc, char** argv) {