2012-02-20 04:46:08 +04:00
|
|
|
#ifndef MERT_OPTIMIZER_H_
|
|
|
|
#define MERT_OPTIMIZER_H_
|
2011-11-12 02:59:50 +04:00
|
|
|
|
2008-05-15 20:03:49 +04:00
|
|
|
#include <vector>
|
2011-11-14 10:15:30 +04:00
|
|
|
#include <string>
|
2012-02-08 21:11:56 +04:00
|
|
|
#include "Data.h"
|
2008-05-14 15:00:59 +04:00
|
|
|
#include "FeatureData.h"
|
2008-05-14 16:49:45 +04:00
|
|
|
#include "Scorer.h"
|
2008-05-15 23:09:01 +04:00
|
|
|
#include "Types.h"
|
2008-05-14 15:00:59 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
static const float kMaxFloat = std::numeric_limits<float>::max();
|
2012-04-12 00:19:11 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2011-11-14 10:15:30 +04:00
|
|
|
class Point;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract optimizer class.
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
class Optimizer
|
|
|
|
{
|
|
|
|
protected:
|
2012-03-10 12:12:34 +04:00
|
|
|
Scorer *m_scorer; // no accessor for them only child can use them
|
|
|
|
FeatureDataHandle m_feature_data; // no accessor for them only child can use them
|
|
|
|
unsigned int m_num_random_directions;
|
2011-11-11 15:40:59 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
const std::vector<bool>& m_positive;
|
2012-03-30 13:50:23 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
public:
|
2012-05-10 02:51:05 +04:00
|
|
|
Optimizer(unsigned Pd, const std::vector<unsigned>& i2O, const std::vector<bool>& positive, const std::vector<parameter_t>& start, unsigned int nrandom);
|
2012-03-10 12:12:34 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
void SetScorer(Scorer *scorer) {
|
|
|
|
m_scorer = scorer;
|
|
|
|
}
|
|
|
|
void SetFeatureData(FeatureDataHandle feature_data) {
|
|
|
|
m_feature_data = feature_data;
|
|
|
|
}
|
2008-05-15 14:57:20 +04:00
|
|
|
virtual ~Optimizer();
|
|
|
|
|
2011-11-12 13:47:31 +04:00
|
|
|
unsigned size() const {
|
2012-03-10 12:12:34 +04:00
|
|
|
return m_feature_data ? m_feature_data->size() : 0;
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic wrapper around TrueRun to check a few things. Non virtual.
|
|
|
|
*/
|
2011-11-12 13:47:31 +04:00
|
|
|
statscore_t Run(Point&) const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main function that performs an optimization.
|
|
|
|
*/
|
2011-11-12 13:47:31 +04:00
|
|
|
virtual statscore_t TrueRun(Point&) const = 0;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of lambdas, get the nbest for each sentence.
|
|
|
|
*/
|
2012-05-10 02:51:05 +04:00
|
|
|
void Get1bests(const Point& param,std::vector<unsigned>& bests) const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of nbests, get the Statistical score.
|
|
|
|
*/
|
2012-05-10 02:51:05 +04:00
|
|
|
statscore_t GetStatScore(const std::vector<unsigned>& nbests) const {
|
2012-03-10 12:12:34 +04:00
|
|
|
return m_scorer->score(nbests);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
|
|
|
|
2011-11-12 13:47:31 +04:00
|
|
|
statscore_t GetStatScore(const Point& param) const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
std::vector<statscore_t> GetIncStatScore(const std::vector<unsigned>& ref, const std::vector<std::vector<std::pair<unsigned,unsigned> > >& diffs) const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the optimal Lambda and the best score in a particular direction from a given Point.
|
|
|
|
*/
|
2011-11-12 13:47:31 +04:00
|
|
|
statscore_t LineOptimize(const Point& start, const Point& direction, Point& best) const;
|
2008-05-14 18:25:07 +04:00
|
|
|
};
|
2008-05-14 15:00:59 +04:00
|
|
|
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Default basic optimizer.
|
|
|
|
* This class implements Powell's method.
|
|
|
|
*/
|
2011-11-12 13:47:31 +04:00
|
|
|
class SimpleOptimizer : public Optimizer
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2008-05-15 23:09:01 +04:00
|
|
|
private:
|
2011-11-12 05:16:31 +04:00
|
|
|
const float kEPS;
|
2008-05-14 15:00:59 +04:00
|
|
|
public:
|
2012-05-10 02:51:05 +04:00
|
|
|
SimpleOptimizer(unsigned dim, const std::vector<unsigned>& i2O, const std::vector<bool>& positive,
|
|
|
|
const std::vector<parameter_t>& start, unsigned int nrandom)
|
|
|
|
: Optimizer(dim, i2O, positive, start,nrandom), kEPS(0.0001f) {}
|
2011-11-12 13:47:31 +04:00
|
|
|
virtual statscore_t TrueRun(Point&) const;
|
2008-05-14 18:25:07 +04:00
|
|
|
};
|
2008-05-14 15:00:59 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* An optimizer with random directions.
|
|
|
|
*/
|
2011-11-12 13:47:31 +04:00
|
|
|
class RandomDirectionOptimizer : public Optimizer
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2011-07-23 04:24:45 +04:00
|
|
|
private:
|
2011-11-12 05:16:31 +04:00
|
|
|
const float kEPS;
|
2008-05-16 14:57:24 +04:00
|
|
|
public:
|
2012-05-10 02:51:05 +04:00
|
|
|
RandomDirectionOptimizer(unsigned dim, const std::vector<unsigned>& i2O, const std::vector<bool>& positive,
|
|
|
|
const std::vector<parameter_t>& start, unsigned int nrandom)
|
2013-05-29 21:16:15 +04:00
|
|
|
: Optimizer(dim, i2O, positive, start, nrandom), kEPS(0.0001f) {}
|
2011-11-12 13:47:31 +04:00
|
|
|
virtual statscore_t TrueRun(Point&) const;
|
2008-05-16 14:57:24 +04:00
|
|
|
};
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Dumb baseline optimizer: just picks a random point and quits.
|
|
|
|
*/
|
2011-11-12 13:47:31 +04:00
|
|
|
class RandomOptimizer : public Optimizer
|
2011-07-23 04:24:45 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-05-10 02:51:05 +04:00
|
|
|
RandomOptimizer(unsigned dim, const std::vector<unsigned>& i2O, const std::vector<bool>& positive,
|
|
|
|
const std::vector<parameter_t>& start, unsigned int nrandom)
|
2013-05-29 21:16:15 +04:00
|
|
|
: Optimizer(dim, i2O, positive, start, nrandom) {}
|
2011-11-12 13:47:31 +04:00
|
|
|
virtual statscore_t TrueRun(Point&) const;
|
2011-07-23 04:24:45 +04:00
|
|
|
};
|
2008-05-16 14:57:24 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
}
|
|
|
|
|
2011-11-12 02:59:50 +04:00
|
|
|
#endif // OPTIMIZER_H
|