Skip to content

Commit

Permalink
system cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
nicyun committed Jan 19, 2012
1 parent 4dd4614 commit 16f8360
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 210 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
.deps
Makefile
config.h
*.log
*.o
config.status
stamp-h1
test
train
Model
2 changes: 1 addition & 1 deletion src/dataManager.cpp
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
2 changes: 1 addition & 1 deletion src/dataManager.h
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
2 changes: 1 addition & 1 deletion src/event.h
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
2 changes: 1 addition & 1 deletion src/gisTrainer.cpp
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
50 changes: 25 additions & 25 deletions src/gisTrainer.h
@@ -1,38 +1,38 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
* Li Yun <nicyun@gmail.com>
* Chen Zhijie <ruoyu928@126.com>
* Yan Yuwen <ybbaigo@gmail.com>
*/
#ifndef __GIS_TRAINER__
#define __GIS_TRAINER__

#include "dataManager.h"
#include "maxEntTrainer.h"

namespace maxent{

class GisTrainer : public MaxEntTrainer
{
public:

#ifndef __GIS_TRAINER__
#define __GIS_TRAINER__

#include "dataManager.h"
#include "maxEntTrainer.h"

namespace maxent{

class GisTrainer : public MaxEntTrainer
{
public:

GisTrainer(
DataManager & _modelInfo,
size_t _iter,
double _tol,
double _sigma2,
double _alpha);

bool train();

private:
double _getSlowFactor();

};

}

#endif
double _alpha);

bool train();

private:
double _getSlowFactor();

};

}

#endif
44 changes: 22 additions & 22 deletions src/lbfgsTrainer.h
@@ -1,36 +1,36 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
* Li Yun <nicyun@gmail.com>
* Chen Zhijie <ruoyu928@126.com>
* Yan Yuwen <ybbaigo@gmail.com>
*/
#ifndef __LBFGS_TRAINER__
#define __LBFGS_TRAINER__

#include "dataManager.h"
#ifndef __LBFGS_TRAINER__
#define __LBFGS_TRAINER__

#include "dataManager.h"
#include "maxEntTrainer.h"
#include "lbfgs.h"

namespace maxent{

class LbfgsTrainer : public MaxEntTrainer
{
public:

#include "lbfgs.h"

namespace maxent{

class LbfgsTrainer : public MaxEntTrainer
{
public:

LbfgsTrainer(
DataManager & _modelInfo,
size_t _iter,
double _tol,
double _sigma2,
double _alpha);

~LbfgsTrainer();
~LbfgsTrainer();

public:


bool train();

private:
Expand Down Expand Up @@ -81,14 +81,14 @@ class LbfgsTrainer : public MaxEntTrainer
{
return reinterpret_cast<LbfgsTrainer*>(instance)->
progress(x, g, fx, xnorm, gnorm, step, n, k, ls);
}
}

private:
size_t mFfN;
lbfgsfloatval_t * mX;
lbfgs_parameter_t mLbfgsParam;
};

}

#endif
lbfgs_parameter_t mLbfgsParam;
};

}

#endif
2 changes: 1 addition & 1 deletion src/maxEntMap.cpp
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
48 changes: 24 additions & 24 deletions src/maxEntMap.h
@@ -1,34 +1,34 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
* Li Yun <nicyun@gmail.com>
* Chen Zhijie <ruoyu928@126.com>
* Yan Yuwen <ybbaigo@gmail.com>
*/
#ifndef __MAX_ENT_MAP__
#define __MAX_ENT_MAP__
#ifndef __MAX_ENT_MAP__
#define __MAX_ENT_MAP__
#undef __DEPRECATED

#include <ext/hash_map>
#include <vector>

namespace maxent
{

class MaxEntMap
{
public:


namespace maxent
{

class MaxEntMap
{
public:

MaxEntMap();
~MaxEntMap();

public:
size_t insertString(const std::string & str);
size_t str2num(const std::string & str) const;
~MaxEntMap();

public:
size_t insertString(const std::string & str);
size_t str2num(const std::string & str) const;
std::string & num2str(const size_t Id, std::string & str) const;


private:
struct string_hash {
size_t operator()(const std::string & str) const{
Expand All @@ -37,11 +37,11 @@ class MaxEntMap
};

private:
__gnu_cxx::hash_map<std::string, size_t, string_hash> mStr2Num;
std::vector<std::string> mNum2Str;
size_t mNumber; // auto increment
__gnu_cxx::hash_map<std::string, size_t, string_hash> mStr2Num;
std::vector<std::string> mNum2Str;
size_t mNumber; // auto increment
};

}

#endif

}

#endif
2 changes: 1 addition & 1 deletion src/maxEntModel.cpp
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
2 changes: 1 addition & 1 deletion src/maxEntModel.h
@@ -1,5 +1,5 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
Expand Down
111 changes: 56 additions & 55 deletions src/maxEntTrainer.cpp
@@ -1,64 +1,65 @@
/**
* maxent toolkit
* easyME -- a Maximum Entropy toolkit
*
* Copyright(C) 2009
*
* Li Yun <nicyun@gmail.com>
* Chen Zhijie <ruoyu928@126.com>
* Yan Yuwen <ybbaigo@gmail.com>
*/
#include <iostream>
#include <math.h>
#include "maxEntTrainer.h"

namespace maxent{

using namespace std;

MaxEntTrainer::MaxEntTrainer(DataManager & _modelInfo, size_t _iter,
double _tol, double _sigma2, double _alpha)
: mModelInfo(_modelInfo), mIter(_iter), mEps(_tol), mSigma2(_sigma2), mAlpha(_alpha)
{
_initTrainer();
}

void MaxEntTrainer::_initTrainer(void)
{
mEventNum = mModelInfo.getEventNum();
mMaxFid = mModelInfo.getFetNum();
#include <iostream>
#include <math.h>
#include "maxEntTrainer.h"

namespace maxent{

using namespace std;

MaxEntTrainer::MaxEntTrainer(DataManager & _modelInfo, size_t _iter,
double _tol, double _sigma2, double _alpha)
: mModelInfo(_modelInfo), mIter(_iter), mEps(_tol), mSigma2(_sigma2), mAlpha(_alpha)
{
_initTrainer();
}

void MaxEntTrainer::_initTrainer(void)
{
mEventNum = mModelInfo.getEventNum();
mMaxFid = mModelInfo.getFetNum();
mMaxCid = mModelInfo.getClassNum();
mTotEvent = mModelInfo.getAllEventFreq();
mModelInfo.getObserves(mObserved);
}

// Calculate the ith GIS parameter updates with Gaussian prior
// using Newton-Raphson method
// the update rule is the solution of the following equation:
// lambda_i + delta_i
// E_ref = E_q * exp (C * delta_i) + ------------------ * N
// sigma_i^2
// note: E_ref and E_q were not divided by N
double MaxEntTrainer::_newton(double f_q, double f_ref, double lambdaNow, double mEps)
{
size_t maxiter = 50;
double x0 = 0.0;
double x = 0.0;

for (size_t mIter = 1; mIter <= maxiter; ++mIter) {
double t = f_q * exp(mSlowF * x0);
double fval = t + mTotEvent * (lambdaNow + x0) / mSigma2 - f_ref;
double fpval = t * mSlowF + mTotEvent / mSigma2;
if (fpval == 0) {
cerr << "Warning: zero-derivative encountered in newton() method." << endl;
return x0;
}
x = x0 - fval/fpval;
if (fabs(x-x0) < mEps)
return x;
x0 = x;
}
cerr << "Failed to converge after 50 iterations in newton() method" << endl;
exit(-1);
}

}
mTotEvent = mModelInfo.getAllEventFreq();
mModelInfo.getObserves(mObserved);
}

// Calculate the ith GIS parameter updates with Gaussian prior
// using Newton-Raphson method
// the update rule is the solution of the following equation:
// lambda_i + delta_i
// E_ref = E_q * exp (C * delta_i) + ------------------ * N
// sigma_i^2
// note: E_ref and E_q were not divided by N
// this function is copied from Le Zhang's work
double MaxEntTrainer::_newton(double f_q, double f_ref, double lambdaNow, double mEps)
{
size_t maxiter = 50;
double x0 = 0.0;
double x = 0.0;

for (size_t mIter = 1; mIter <= maxiter; ++mIter) {
double t = f_q * exp(mSlowF * x0);
double fval = t + mTotEvent * (lambdaNow + x0) / mSigma2 - f_ref;
double fpval = t * mSlowF + mTotEvent / mSigma2;
if (fpval == 0) {
cerr << "Warning: zero-derivative encountered in newton() method." << endl;
return x0;
}
x = x0 - fval/fpval;
if (fabs(x-x0) < mEps)
return x;
x0 = x;
}
cerr << "Failed to converge after 50 iterations in newton() method" << endl;
exit(-1);
}

}

0 comments on commit 16f8360

Please sign in to comment.