forked from fandywang/mltk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
758 additions
and
506 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
// Copyright (c) 2013 MLTK Project. | ||
// Author: Lifeng Wang ([email protected]) | ||
// | ||
// Implementation of LBFGS algorithm. | ||
// | ||
// Pls refer to 'Jorge Nocedal, "Updating Quasi-Newton Matrices With Limited | ||
// Storage", Mathematics of Computation, 1980.' | ||
|
||
#include "mltk/maxent/maxent.h" | ||
#include "mltk/maxent/lbfgs.h" | ||
|
||
#include <assert.h> | ||
#include <math.h> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "mltk/common/double_vector.h" | ||
#include "mltk/common/instance.h" | ||
#include "mltk/common/model_data.h" | ||
|
||
namespace mltk { | ||
namespace maxent { | ||
|
||
using mltk::common::DoubleVector; | ||
using mltk::common::Instance; | ||
using mltk::common::ModelData; | ||
|
||
const static int32_t LBFGS_M = 10; | ||
const static double LINE_SEARCH_ALPHA = 0.1; | ||
|
@@ -27,43 +27,29 @@ const static double LINE_SEARCH_BETA = 0.5; | |
const static int32_t LBFGS_MAX_ITER = 300; | ||
const static double MIN_GRAD_NORM = 0.0001; | ||
|
||
DoubleVector ApproximateHg(const int32_t iter, | ||
const DoubleVector& grad, | ||
const DoubleVector s[], | ||
const DoubleVector y[], | ||
const double z[]) { | ||
int32_t offset, bound; | ||
if (iter <= LBFGS_M) { | ||
offset = 0; | ||
bound = iter; | ||
} | ||
else { | ||
offset = iter - LBFGS_M; | ||
bound = LBFGS_M; | ||
void LBFGS::EstimateParamater(const std::vector<Instance>& instances, | ||
int32_t num_heldout, | ||
ModelData* model_data) { | ||
std::cerr << "performing LBFGS" << std::endl; | ||
if (l1reg_ > 0) { | ||
std::cerr << "error: L1 regularization is not supported in LBFGS," | ||
<< "you can use OWLQN method instead." << std::endl; | ||
exit(1); | ||
} | ||
|
||
DoubleVector q = grad; | ||
double alpha[LBFGS_M], beta[LBFGS_M]; | ||
for (int32_t i = bound - 1; i >= 0; --i) { | ||
const int32_t j = (i + offset) % LBFGS_M; | ||
alpha[i] = z[j] * DotProduct(s[j], q); | ||
q += -alpha[i] * y[j]; | ||
} | ||
if (iter > 0) { | ||
const int32_t j = (iter - 1) % LBFGS_M; | ||
const double gamma = ((1.0 / z[j]) / DotProduct(y[j], y[j])); | ||
q *= gamma; | ||
} | ||
for (int32_t i = 0; i <= bound - 1; ++i) { | ||
const int32_t j = (i + offset) % LBFGS_M; | ||
beta[i] = z[j] * DotProduct(y[j], q); | ||
q += s[j] * (alpha[i] - beta[i]); | ||
InitFromInstances(instances, num_heldout, model_data); | ||
|
||
const std::vector<double> lambdas = model_data_->Lambdas(); | ||
std::vector<double> x0(model_data_->NumFeatures()); | ||
for (int32_t i = 0; i < model_data_->NumFeatures(); ++i) { | ||
x0[i] = lambdas[i]; | ||
} | ||
|
||
return q; | ||
std::vector<double> x = PerformLBFGS(x0); | ||
model_data_->UpdateLambdas(x); | ||
} | ||
|
||
std::vector<double> MaxEnt::PerformLBFGS(const std::vector<double>& x0) { | ||
std::vector<double> LBFGS::PerformLBFGS(const std::vector<double>& x0) { | ||
const size_t dim = x0.size(); | ||
DoubleVector x(x0); | ||
DoubleVector grad(dim), dx(dim); | ||
|
@@ -79,7 +65,7 @@ std::vector<double> MaxEnt::PerformLBFGS(const std::vector<double>& x0) { | |
<< ", obj(err) = " << f | ||
<< ", accuracy = " << train_accuracy_ << std::endl; | ||
|
||
if (heldout_.size() > 0) { | ||
if (heldout_data_.size() > 0) { | ||
const double heldout_logl = CalcHeldoutLikelihood(); | ||
std::cerr << "\theldout_logl(err) = " << -1 * heldout_logl | ||
<< ", accuracy = " << heldout_accuracy_ << std::endl; | ||
|
@@ -103,12 +89,48 @@ std::vector<double> MaxEnt::PerformLBFGS(const std::vector<double>& x0) { | |
return x.STLVector(); | ||
} | ||
|
||
double MaxEnt::BacktrackingLineSearch(const DoubleVector& x0, | ||
const DoubleVector& grad0, | ||
const double f0, | ||
const DoubleVector& dx, | ||
DoubleVector* x, | ||
DoubleVector* grad1) { | ||
DoubleVector LBFGS::ApproximateHg(const int32_t iter, | ||
const DoubleVector& grad, | ||
const DoubleVector s[], | ||
const DoubleVector y[], | ||
const double z[]) { | ||
int32_t offset, bound; | ||
if (iter <= LBFGS_M) { | ||
offset = 0; | ||
bound = iter; | ||
} | ||
else { | ||
offset = iter - LBFGS_M; | ||
bound = LBFGS_M; | ||
} | ||
|
||
DoubleVector q = grad; | ||
double alpha[LBFGS_M], beta[LBFGS_M]; | ||
for (int32_t i = bound - 1; i >= 0; --i) { | ||
const int32_t j = (i + offset) % LBFGS_M; | ||
alpha[i] = z[j] * DotProduct(s[j], q); | ||
q += -alpha[i] * y[j]; | ||
} | ||
if (iter > 0) { | ||
const int32_t j = (iter - 1) % LBFGS_M; | ||
const double gamma = ((1.0 / z[j]) / DotProduct(y[j], y[j])); | ||
q *= gamma; | ||
} | ||
for (int32_t i = 0; i <= bound - 1; ++i) { | ||
const int32_t j = (i + offset) % LBFGS_M; | ||
beta[i] = z[j] * DotProduct(y[j], q); | ||
q += s[j] * (alpha[i] - beta[i]); | ||
} | ||
|
||
return q; | ||
} | ||
|
||
double LBFGS::BacktrackingLineSearch(const DoubleVector& x0, | ||
const DoubleVector& grad0, | ||
const double f0, | ||
const DoubleVector& dx, | ||
DoubleVector* x, | ||
DoubleVector* grad1) { | ||
double t = 1.0 / LINE_SEARCH_BETA; | ||
double f; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2013 MLTK Project. | ||
// Author: Lifeng Wang ([email protected]) | ||
// | ||
// Implementation of LBFGS algorithm. | ||
// | ||
// Pls refer to 'Jorge Nocedal, "Updating Quasi-Newton Matrices With Limited | ||
// Storage", Mathematics of Computation, 1980.' | ||
|
||
#ifndef MLTK_MAXENT_LBFGS_H_ | ||
#define MLTK_MAXENT_LBFGS_H_ | ||
|
||
#include "mltk/maxent/optimizer.h" | ||
|
||
#include <vector> | ||
|
||
namespace mltk { | ||
|
||
namespace common { | ||
class DoubleVector; | ||
class Instance; | ||
class ModelData; | ||
} // namespace common | ||
|
||
namespace maxent { | ||
|
||
class LBFGS : public Optimizer { | ||
public: | ||
LBFGS() {} | ||
virtual ~LBFGS() {} | ||
|
||
virtual void EstimateParamater(const std::vector<common::Instance>& instances, | ||
int32_t num_heldout, | ||
common::ModelData* model_data); | ||
|
||
private: | ||
std::vector<double> PerformLBFGS(const std::vector<double>& x0); | ||
|
||
common::DoubleVector ApproximateHg(const int32_t iter, | ||
const common::DoubleVector& grad, | ||
const common::DoubleVector s[], | ||
const common::DoubleVector y[], | ||
const double z[]); | ||
|
||
double BacktrackingLineSearch(const common::DoubleVector& x0, | ||
const common::DoubleVector& grad0, | ||
const double f0, | ||
const common::DoubleVector& dx, | ||
common::DoubleVector* x, | ||
common::DoubleVector* grad1); | ||
}; | ||
|
||
} // namespace maxent | ||
} // namespace mltk | ||
|
||
#endif // MLTK_MAXENT_LBFGS_H_ | ||
|
Oops, something went wrong.