1. remove all code for MetaFeature from mira.

2. in ShowWeights(), all print out dense feature weights. Don't print 'sparse' for sparse feature functions. All features functions can contains dense and sparse
This commit is contained in:
Hieu Hoang 2013-05-16 19:05:08 +01:00
parent b322e78a38
commit f04ec4c56d
15 changed files with 14 additions and 188 deletions

View File

@ -591,11 +591,6 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses/LexicalReorderingTable.h</locationURI>
</link>
<link>
<name>Makefile.am</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses/Makefile.am</locationURI>
</link>
<link>
<name>Manager.cpp</name>
<type>1</type>

View File

@ -524,19 +524,7 @@ int main(int argc, char** argv) {
// get reference to feature functions
const vector<FeatureFunction*> &featureFunctions = FeatureFunction::GetFeatureFunctions();
ScoreComponentCollection initialWeights = decoder->getWeights();
bool tuneMetaFeature = false;
const vector<const FeatureFunction*>& sparseProducers = staticData.GetSparseProducers();
for (unsigned i = 0; i < sparseProducers.size(); ++i) {
float spWeight = sparseProducers[i]->GetSparseProducerWeight();
if (spWeight != 1.0) {
tuneMetaFeature = true;
cerr << "Rank " << rank << ", sparse Producer " <<
sparseProducers[i]->GetScoreProducerDescription()
<< " weight: " << spWeight << endl;
}
}
if (add2lm != 0) {
const LMList& lmList_new = staticData.GetLMList();
for (LMList::const_iterator i = lmList_new.begin(); i != lmList_new.end(); ++i) {
@ -693,22 +681,6 @@ int main(int argc, char** argv) {
decoder->printBleuFeatureHistory(cerr);
}
if (tuneMetaFeature) {
// initialise meta feature
MetaFeatureProducer *m = staticData.GetMetaFeatureProducer();
FeatureFunction* ff = const_cast<FeatureFunction*>(sparseProducers[0]);
if (sparseProducers[0]->GetScoreProducerDescription().compare("wt") == 0) {
WordTranslationFeature* wt =
static_cast<WordTranslationFeature*>(ff);
mosesWeights.Assign(m, wt->GetSparseProducerWeight());
}
else if (sparseProducers[0]->GetScoreProducerDescription().compare("pp") == 0) {
PhrasePairFeature* pp =
static_cast<PhrasePairFeature*>(ff);
mosesWeights.Assign(m, pp->GetSparseProducerWeight());
}
}
// BATCHING: produce nbest lists for all input sentences in batch
vector<float> oracleBleuScores;
vector<float> oracleModelScores;
@ -1260,62 +1232,6 @@ int main(int argc, char** argv) {
}
}
if (kbest) {
// If we are tuning a global weight for a sparse producer,
// we must collapse the sparse features first (report weighted aggregate)
if (tuneMetaFeature) {
for (unsigned i = 0; i < sparseProducers.size(); ++i) {
float spWeight = sparseProducers[i]->GetSparseProducerWeight();
if (spWeight != 1.0) {
MetaFeatureProducer *m = staticData.GetMetaFeatureProducer();
for (size_t i=0; i < featureValuesHope.size(); ++i) {
for (size_t j=0; j < featureValuesHope[i].size(); ++j) {
// multiply sparse feature values with weights
const FVector scores =
featureValuesHope[i][j].GetVectorForProducer(sparseProducers[i]);
const FVector &weights = staticData.GetAllWeights().GetScoresVector();
float aggregate = scores.inner_product(weights);
//cerr << "Rank " << rank << ", epoch " << epoch << ", sparse Producer " <<
//sparseProducers[i]->GetScoreProducerDescription()
//<< " aggregate: " << aggregate << endl;
aggregate *= spWeight;
//cerr << "Rank " << rank << ", epoch " << epoch << ", sparse Producer " <<
//sparseProducers[i]->GetScoreProducerDescription()
//<< " weighted aggregate: " << aggregate << endl;
// copy core features to a new collection, then assign aggregated sparse feature
ScoreComponentCollection scoresAggregate;
scoresAggregate.CoreAssign(featureValuesHope[i][j]);
scoresAggregate.Assign(m, aggregate);
featureValuesHope[i][j] = scoresAggregate;
}
}
for (size_t i=0; i < featureValuesFear.size(); ++i) {
for (size_t j=0; j < featureValuesFear[i].size(); ++j) {
// multiply sparse feature values with weights
const FVector scores =
featureValuesFear[i][j].GetVectorForProducer(sparseProducers[i]);
const FVector &weights = staticData.GetAllWeights().GetScoresVector();
float aggregate = scores.inner_product(weights);
aggregate *= spWeight;
// copy core features to a new collection, then assign aggregated sparse feature
ScoreComponentCollection scoresAggregate;
scoresAggregate.CoreAssign(featureValuesFear[i][j]);
scoresAggregate.Assign(m, aggregate);
featureValuesFear[i][j] = scoresAggregate;
}
}
cerr << "Rank " << rank << ", epoch " << epoch << ", new hope feature vector: " <<
featureValuesHope[0][0] << endl;
cerr << "Rank " << rank << ", epoch " << epoch << ", new fear feature vector: " <<
featureValuesFear[0][0] << endl;
}
}
}
}
// Run optimiser on batch:
VERBOSE(1, "\nRank " << rank << ", epoch " << epoch << ", run optimiser:" << endl);
size_t update_status = 1;
@ -1381,34 +1297,6 @@ int main(int argc, char** argv) {
if (debug)
cerr << "Rank " << rank << ", epoch " << epoch << ", update: " << weightUpdate << endl;
if (tuneMetaFeature) {
MetaFeatureProducer *m = staticData.GetMetaFeatureProducer();
// update sparse producer weight
// (NOTE: this currently doesn't work for more than one sparse producer)
float metaWeightUpdate = weightUpdate.GetScoreForProducer(m);
const vector<const FeatureFunction*> sparseProducers = staticData.GetSparseProducers();
FeatureFunction* ff = const_cast<FeatureFunction*>(sparseProducers[0]);
if (sparseProducers[0]->GetScoreProducerDescription().compare("wt") == 0) {
WordTranslationFeature* wt =
static_cast<WordTranslationFeature*>(ff);
float newWeight = wt->GetSparseProducerWeight();
cerr << "Rank " << rank << ", epoch " << epoch << ", old meta weight: " << newWeight << endl;
newWeight += metaWeightUpdate;
wt->SetSparseProducerWeight(newWeight);
cerr << "Rank " << rank << ", epoch " << epoch << ", new meta weight: " << newWeight << endl;
}
else if (sparseProducers[0]->GetScoreProducerDescription().compare("pp") == 0) {
PhrasePairFeature* pp =
static_cast<PhrasePairFeature*>(ff);
float newWeight = pp->GetSparseProducerWeight();
cerr << "Rank " << rank << ", epoch " << epoch << ", old meta weight: " << newWeight << endl;
newWeight += metaWeightUpdate;
pp->SetSparseProducerWeight(newWeight);
cerr << "Rank " << rank << ", epoch " << epoch << ", new meta weight: " << newWeight << endl;
}
}
if (feature_confidence) {
// update confidence counts based on weight update
confidenceCounts.UpdateConfidenceCounts(weightUpdate, signed_counts);
@ -1420,7 +1308,7 @@ int main(int argc, char** argv) {
// apply weight update to Moses weights
mosesWeights.PlusEquals(weightUpdate);
if (normaliseWeights && !tuneMetaFeature)
if (normaliseWeights)
mosesWeights.L1Normalise();
cumulativeWeights.PlusEquals(mosesWeights);
@ -1432,7 +1320,7 @@ int main(int argc, char** argv) {
++numberOfUpdates;
++numberOfUpdatesThisEpoch;
if (averageWeights && !tuneMetaFeature) {
if (averageWeights) {
ScoreComponentCollection averageWeights(cumulativeWeights);
if (accumulateWeights) {
averageWeights.DivideEquals(numberOfUpdates);

View File

@ -187,21 +187,14 @@ bool ReadInput(IOWrapper &ioWrapper, InputTypeEnum inputType, InputType*& source
}
static void PrintFeatureWeight(const FeatureFunction* ff)
{
cout << ff->GetScoreProducerDescription() << "=";
size_t numScoreComps = ff->GetNumScoreComponents();
if (numScoreComps != 0) {
vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff);
cout << ff->GetScoreProducerDescription() << "=";
for (size_t i = 0; i < numScoreComps; ++i) {
cout << " " << values[i];
}
cout << endl;
}
else {
if (ff->GetSparseProducerWeight() == 1)
cout << ff->GetScoreProducerDescription() << "= sparse" << endl;
else
cout << ff->GetScoreProducerDescription() << "= " << ff->GetSparseProducerWeight() << endl;
vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff);
for (size_t i = 0; i < numScoreComps; ++i) {
cout << " " << values[i];
}
cout << endl;
}
static void ShowWeights()

View File

@ -446,21 +446,13 @@ private:
static void PrintFeatureWeight(const FeatureFunction* ff)
{
cout << ff->GetScoreProducerDescription() << "=";
size_t numScoreComps = ff->GetNumScoreComponents();
if (numScoreComps != 0) {
vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff);
cout << ff->GetScoreProducerDescription() << "=";
for (size_t i = 0; i < numScoreComps; ++i) {
cout << " " << values[i];
}
cout << endl;
}
else {
if (ff->GetSparseProducerWeight() == 1)
cout << ff->GetScoreProducerDescription() << "= sparse" << endl;
else
cout << ff->GetScoreProducerDescription() << "= " << ff->GetSparseProducerWeight() << endl;
vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff);
for (size_t i = 0; i < numScoreComps; ++i) {
cout << " " << values[i];
}
cout << endl;
}
static void ShowWeights()

View File

@ -109,8 +109,6 @@ public:
const std::string& GetScoreProducerDescription() const
{ return m_description; }
virtual float GetSparseProducerWeight() const { return 1; }
virtual bool IsTuneable() const { return m_tuneable; }
//!

View File

@ -60,7 +60,6 @@ private:
bool m_bitrigger;
bool m_biasFeature;
float m_sparseProducerWeight;
bool m_ignorePunctuation;
std::set<std::string> m_vocabSource;
@ -93,9 +92,6 @@ public:
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const;
void SetSparseProducerWeight(float weight) { m_sparseProducerWeight = weight; }
float GetSparseProducerWeight() const { return m_sparseProducerWeight; }
void AddFeature(ScoreComponentCollection* accumulator,
StringPiece sourceTrigger, StringPiece sourceWord, StringPiece targetTrigger,
StringPiece targetWord) const;

View File

@ -308,13 +308,6 @@ void Hypothesis::CalcScore(const SquareMatrix &futureScore)
// FUTURE COST
m_futureScore = futureScore.CalcFutureScore( m_sourceCompleted );
// Apply sparse producer weights
const vector<const FeatureFunction*>& sparseProducers = StaticData::Instance().GetSparseProducers();
for (unsigned i = 0; i < sparseProducers.size(); ++i) {
float weight = sparseProducers[i]->GetSparseProducerWeight();
m_scoreBreakdown.MultiplyEquals(sparseProducers[i], weight);
}
// TOTAL
m_totalScore = m_scoreBreakdown.GetWeightedScore() + m_futureScore;

View File

@ -49,9 +49,6 @@ public:
virtual void Evaluate(const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const;
void SetSparseProducerWeight(float weight) { m_sparseProducerWeight = weight; }
float GetSparseProducerWeight() const { return m_sparseProducerWeight; }
private:
void AddFeatures(
@ -59,7 +56,6 @@ private:
const std::string& side, ScoreComponentCollection* scores) const ;
FactorList m_sourceFactors;
FactorList m_targetFactors;
float m_sparseProducerWeight;
};
}

View File

@ -26,7 +26,6 @@ PhrasePairFeature::PhrasePairFeature(const std::string &line)
m_simple = Scan<bool>(tokens[4]);
m_sourceContext = Scan<bool>(tokens[5]);
m_domainTrigger = Scan<bool>(tokens[6]);
m_sparseProducerWeight = 1;
m_ignorePunctuation = Scan<bool>(tokens[6]);
if (m_simple == 1) std::cerr << "using simple phrase pairs.. ";

View File

@ -26,7 +26,6 @@ class PhrasePairFeature: public StatelessFeatureFunction {
bool m_simple;
bool m_sourceContext;
bool m_domainTrigger;
float m_sparseProducerWeight;
bool m_ignorePunctuation;
CharHash m_punctuationHash;
@ -47,8 +46,6 @@ class PhrasePairFeature: public StatelessFeatureFunction {
bool Load(const std::string &filePathSource/*, const std::string &filePathTarget*/);
void SetSparseProducerWeight(float weight) { m_sparseProducerWeight = weight; }
float GetSparseProducerWeight() const { return m_sparseProducerWeight; }
};
}

View File

@ -662,15 +662,6 @@ public:
return m_nBestIncludesSegmentation;
}
const std::vector<const FeatureFunction*>& GetSparseProducers() const {return m_sparseProducers;}
void AddSparseProducer(const FeatureFunction* ff) {
m_sparseProducers.push_back(ff);
}
//All sparse producers that have an activated global weight
std::vector<const FeatureFunction*> m_sparseProducers;
float GetWeightWordPenalty() const;
float GetWeightUnknownWordPenalty() const;

View File

@ -39,7 +39,6 @@ int TargetNgramState::Compare(const FFState& other) const {
TargetNgramFeature::TargetNgramFeature(const std::string &line)
:StatefulFeatureFunction("TargetNgramFeature", 0, line)
,m_sparseProducerWeight(1)
{
std::cerr << "Initializing target ngram feature.." << std::endl;

View File

@ -184,9 +184,6 @@ public:
bool Load(const std::string &filePath);
void SetSparseProducerWeight(float weight) { m_sparseProducerWeight = weight; }
float GetSparseProducerWeight() const { return m_sparseProducerWeight; }
virtual const FFState* EmptyHypothesisState(const InputType &input) const;
virtual FFState* Evaluate(const Hypothesis& cur_hypo, const FFState* prev_state,
@ -206,9 +203,6 @@ private:
size_t m_n;
bool m_lower_ngrams;
// additional weight that all sparse weights are scaled with
float m_sparseProducerWeight;
std::string m_baseName;
void appendNgram(const Word& word, bool& skip, std::stringstream& ngram) const;

View File

@ -17,7 +17,6 @@ using namespace std;
WordTranslationFeature::WordTranslationFeature(const std::string &line)
:StatelessFeatureFunction("WordTranslationFeature", 0, line)
,m_unrestricted(true)
,m_sparseProducerWeight(1)
,m_simple(true)
,m_sourceContext(false)
,m_targetContext(false)

View File

@ -31,7 +31,6 @@ private:
bool m_sourceContext;
bool m_targetContext;
bool m_domainTrigger;
float m_sparseProducerWeight;
bool m_ignorePunctuation;
CharHash m_punctuationHash;
@ -54,9 +53,6 @@ public:
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const;
void SetSparseProducerWeight(float weight) { m_sparseProducerWeight = weight; }
float GetSparseProducerWeight() const { return m_sparseProducerWeight; }
StatelessFeatureType GetStatelessFeatureType() const
{ return RequiresSegmentation; }
};