2010-09-15 19:38:46 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2010 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
#ifndef _MIRA_OPTIMISER_H_
|
|
|
|
#define _MIRA_OPTIMISER_H_
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "ScoreComponentCollection.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Mira {
|
|
|
|
|
|
|
|
class Optimiser {
|
|
|
|
public:
|
2010-09-16 21:22:52 +04:00
|
|
|
Optimiser() {}
|
2010-11-30 20:26:34 +03:00
|
|
|
virtual int updateWeights(Moses::ScoreComponentCollection& weights,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector<std::vector<Moses::ScoreComponentCollection> >& featureValues,
|
2010-09-17 13:17:05 +04:00
|
|
|
const std::vector<std::vector<float> >& losses,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector<std::vector<float> >& bleuScores,
|
2010-12-09 14:49:52 +03:00
|
|
|
const std::vector<Moses::ScoreComponentCollection>& oracleFeatureValues,
|
|
|
|
const std::vector< size_t> dummy) = 0;
|
2010-09-15 19:38:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class DummyOptimiser : public Optimiser {
|
|
|
|
public:
|
2010-11-30 20:26:34 +03:00
|
|
|
virtual int updateWeights(Moses::ScoreComponentCollection& weights,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector< std::vector<Moses::ScoreComponentCollection> >& featureValues,
|
2010-09-17 13:17:05 +04:00
|
|
|
const std::vector< std::vector<float> >& losses,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector<std::vector<float> >& bleuScores,
|
2010-12-09 14:49:52 +03:00
|
|
|
const std::vector<Moses::ScoreComponentCollection>& oracleFeatureValues,
|
|
|
|
const std::vector< size_t> dummy)
|
2010-12-01 21:09:49 +03:00
|
|
|
{ return 0; }
|
2010-09-15 19:38:46 +04:00
|
|
|
};
|
2010-09-17 13:17:05 +04:00
|
|
|
|
|
|
|
class Perceptron : public Optimiser {
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
2010-11-30 20:26:34 +03:00
|
|
|
virtual int updateWeights(Moses::ScoreComponentCollection& weights,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector< std::vector<Moses::ScoreComponentCollection> >& featureValues,
|
2010-09-17 21:07:03 +04:00
|
|
|
const std::vector< std::vector<float> >& losses,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector<std::vector<float> >& bleuScores,
|
2010-12-09 14:49:52 +03:00
|
|
|
const std::vector<Moses::ScoreComponentCollection>& oracleFeatureValues,
|
|
|
|
const std::vector< size_t> dummy);
|
2010-09-17 13:17:05 +04:00
|
|
|
};
|
|
|
|
|
2010-09-16 21:22:52 +04:00
|
|
|
class MiraOptimiser : public Optimiser {
|
|
|
|
public:
|
2010-10-25 16:22:35 +04:00
|
|
|
MiraOptimiser() :
|
|
|
|
Optimiser() { }
|
|
|
|
|
2010-12-09 21:55:52 +03:00
|
|
|
MiraOptimiser(size_t n, bool hildreth, float marginScaleFactor, bool onlyViolatedConstraints, float clipping, bool fixedClipping, bool regulariseHildrethUpdates, bool weightedLossFunction, bool accumulateOracles, bool accumulateMostViolatedConstraints, bool pastAndCurrentConstraints, size_t exampleSize) :
|
2010-10-25 16:22:35 +04:00
|
|
|
Optimiser(),
|
|
|
|
m_n(n),
|
2010-10-29 19:41:37 +04:00
|
|
|
m_hildreth(hildreth),
|
2010-11-16 14:44:44 +03:00
|
|
|
m_marginScaleFactor(marginScaleFactor),
|
2010-11-16 21:52:08 +03:00
|
|
|
m_onlyViolatedConstraints(onlyViolatedConstraints),
|
2010-11-18 19:24:51 +03:00
|
|
|
m_c(clipping),
|
2010-11-29 21:42:18 +03:00
|
|
|
m_fixedClipping(fixedClipping),
|
2010-12-06 22:32:59 +03:00
|
|
|
m_regulariseHildrethUpdates(regulariseHildrethUpdates),
|
2010-12-09 14:49:52 +03:00
|
|
|
m_weightedLossFunction(weightedLossFunction),
|
|
|
|
m_accumulateOracles(accumulateOracles),
|
|
|
|
m_accumulateMostViolatedConstraints(accumulateMostViolatedConstraints),
|
2010-12-09 21:55:52 +03:00
|
|
|
m_pastAndCurrentConstraints(pastAndCurrentConstraints),
|
2010-12-09 14:49:52 +03:00
|
|
|
m_oracles(exampleSize) { }
|
2010-09-16 21:22:52 +04:00
|
|
|
|
2010-10-25 16:22:35 +04:00
|
|
|
~MiraOptimiser() {}
|
2010-09-17 21:07:03 +04:00
|
|
|
|
2010-11-30 20:26:34 +03:00
|
|
|
virtual int updateWeights(Moses::ScoreComponentCollection& weights,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector< std::vector<Moses::ScoreComponentCollection> >& featureValues,
|
2010-10-25 16:22:35 +04:00
|
|
|
const std::vector< std::vector<float> >& losses,
|
2010-12-06 22:32:59 +03:00
|
|
|
const std::vector<std::vector<float> >& bleuScores,
|
2010-12-09 14:49:52 +03:00
|
|
|
const std::vector< Moses::ScoreComponentCollection>& oracleFeatureValues,
|
|
|
|
const std::vector< size_t> sentenceId);
|
2010-10-25 16:22:35 +04:00
|
|
|
float computeDelta(Moses::ScoreComponentCollection& currWeights,
|
2010-11-19 14:35:16 +03:00
|
|
|
const Moses::ScoreComponentCollection featureValuesDiff,
|
|
|
|
float loss_jk,
|
|
|
|
float j,
|
|
|
|
float k,
|
|
|
|
std::vector< float>& alphas);
|
2010-10-25 16:22:35 +04:00
|
|
|
void update(Moses::ScoreComponentCollection& currWeights, Moses::ScoreComponentCollection& featureValueDiffs, const float delta);
|
2010-11-18 19:24:51 +03:00
|
|
|
|
2010-11-24 20:06:54 +03:00
|
|
|
void setOracleIndices(std::vector<size_t> oracleIndices) {
|
|
|
|
m_oracleIndices= oracleIndices;
|
2010-11-18 19:24:51 +03:00
|
|
|
}
|
2010-09-17 21:07:03 +04:00
|
|
|
|
2010-09-16 21:22:52 +04:00
|
|
|
private:
|
2010-10-25 16:22:35 +04:00
|
|
|
// number of hypotheses used for each nbest list (number of hope, fear, best model translations)
|
|
|
|
size_t m_n;
|
|
|
|
|
2010-11-16 14:44:44 +03:00
|
|
|
// whether or not to use the Hildreth algorithm in the optimisation step
|
2010-10-29 19:41:37 +04:00
|
|
|
bool m_hildreth;
|
2010-11-09 20:31:30 +03:00
|
|
|
|
2010-11-16 14:44:44 +03:00
|
|
|
// scaling the margin to regularise updates
|
|
|
|
float m_marginScaleFactor;
|
|
|
|
|
2010-11-16 21:52:08 +03:00
|
|
|
// add only violated constraints to the optimisation problem
|
|
|
|
bool m_onlyViolatedConstraints;
|
|
|
|
|
2010-11-18 19:24:51 +03:00
|
|
|
// clipping threshold for SMO to regularise updates
|
2010-10-29 19:41:37 +04:00
|
|
|
float m_c;
|
2010-11-18 19:24:51 +03:00
|
|
|
|
|
|
|
// use a fixed clipping threshold with SMO (instead of adaptive)
|
|
|
|
bool m_fixedClipping;
|
|
|
|
|
2010-11-29 21:42:18 +03:00
|
|
|
// regularise Hildreth updates
|
|
|
|
bool m_regulariseHildrethUpdates;
|
|
|
|
|
2010-12-06 22:32:59 +03:00
|
|
|
bool m_weightedLossFunction;
|
|
|
|
|
2010-11-18 19:24:51 +03:00
|
|
|
// index of oracle translation in hypothesis matrix
|
2010-11-24 20:06:54 +03:00
|
|
|
std::vector<size_t> m_oracleIndices;
|
2010-12-09 14:49:52 +03:00
|
|
|
|
|
|
|
// keep a list of oracle translations over epochs
|
|
|
|
std::vector < std::vector< Moses::ScoreComponentCollection> > m_oracles;
|
|
|
|
|
|
|
|
bool m_accumulateOracles;
|
|
|
|
|
|
|
|
// accumulate most violated constraints for every example
|
|
|
|
std::vector< Moses::ScoreComponentCollection> m_featureValueDiffs;
|
|
|
|
std::vector< float> m_lossMarginDistances;
|
|
|
|
|
|
|
|
bool m_accumulateMostViolatedConstraints;
|
2010-12-09 21:55:52 +03:00
|
|
|
|
|
|
|
bool m_pastAndCurrentConstraints;
|
2010-09-16 21:22:52 +04:00
|
|
|
};
|
2010-09-15 19:38:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|