diff options
author | Barry Haddow <barry.haddow@gmail.com> | 2014-08-06 14:38:51 +0400 |
---|---|---|
committer | Barry Haddow <barry.haddow@gmail.com> | 2014-08-06 14:38:51 +0400 |
commit | ff188c454a14f112d7de1df65f600574fc033713 (patch) | |
tree | 339243f2110c6006d1801eb7a3b3ea6b2fdd1d94 | |
parent | 6549d24d35911a013f6b38f8dccbedb953210791 (diff) |
Sparse features trainable with hg mira
(In phrase-based Moses)
-rw-r--r-- | mert/kbmira.cpp | 12 | ||||
-rw-r--r-- | moses-cmd/Main.cpp | 49 | ||||
-rw-r--r-- | moses/FeatureVector.cpp | 4 | ||||
-rw-r--r-- | moses/FeatureVector.h | 2 | ||||
-rw-r--r-- | moses/Manager.cpp | 37 | ||||
-rw-r--r-- | moses/Manager.h | 1 | ||||
-rw-r--r-- | moses/ScoreComponentCollection.cpp | 14 | ||||
-rw-r--r-- | moses/ScoreComponentCollection.h | 2 |
8 files changed, 22 insertions, 99 deletions
diff --git a/mert/kbmira.cpp b/mert/kbmira.cpp index da552fa36..90f201a00 100644 --- a/mert/kbmira.cpp +++ b/mert/kbmira.cpp @@ -144,7 +144,7 @@ int main(int argc, char** argv) } else { vector<string> names; string last_name = ""; - size_t feature_ctr = 0; + size_t feature_ctr = 1; do { size_t equals = buffer.find_last_of("="); UTIL_THROW_IF(equals == buffer.npos, util::Exception, "Incorrect format in dense feature file: '" @@ -154,15 +154,15 @@ int main(int argc, char** argv) initParams.push_back(boost::lexical_cast<ValType>(buffer.substr(equals+2))); //Names for features with several values need to have their id added - if (name != last_name) feature_ctr = 0; + if (name != last_name) feature_ctr = 1; last_name = name; - if (feature_ctr) { + if (feature_ctr>1) { stringstream namestr; - namestr << names.back() << feature_ctr; + namestr << names.back() << "_" << feature_ctr; names[names.size()-1] = namestr.str(); - if (feature_ctr == 1) { + if (feature_ctr == 2) { stringstream namestr; - namestr << names[names.size()-2] << (feature_ctr-1); + namestr << names[names.size()-2] << "_" << (feature_ctr-1); names[names.size()-2] = namestr.str(); } } diff --git a/moses-cmd/Main.cpp b/moses-cmd/Main.cpp index 32ceb6bc0..8524e6b41 100644 --- a/moses-cmd/Main.cpp +++ b/moses-cmd/Main.cpp @@ -520,58 +520,11 @@ static void ShowWeights() } } -size_t OutputFeatureWeightsForHypergraph(size_t index, const FeatureFunction* ff, std::ostream &outputSearchGraphStream) -{ - size_t numScoreComps = ff->GetNumScoreComponents(); - if (numScoreComps != 0) { - vector<float> values = StaticData::Instance().GetAllWeights().GetScoresForProducer(ff); - if (numScoreComps > 1) { - for (size_t i = 0; i < numScoreComps; ++i) { - outputSearchGraphStream << ff->GetScoreProducerDescription() - << i - << "=" << values[i] << endl; - } - } else { - outputSearchGraphStream << ff->GetScoreProducerDescription() - << "=" << values[0] << endl; - } - return index+numScoreComps; - } else { - UTIL_THROW2("Sparse features are not yet supported when outputting hypergraph format"); - } -} - void OutputFeatureWeightsForHypergraph(std::ostream &outputSearchGraphStream) { outputSearchGraphStream.setf(std::ios::fixed); outputSearchGraphStream.precision(6); - - const vector<const StatelessFeatureFunction*>& slf =StatelessFeatureFunction::GetStatelessFeatureFunctions(); - const vector<const StatefulFeatureFunction*>& sff = StatefulFeatureFunction::GetStatefulFeatureFunctions(); - size_t featureIndex = 1; - for (size_t i = 0; i < sff.size(); ++i) { - featureIndex = OutputFeatureWeightsForHypergraph(featureIndex, sff[i], outputSearchGraphStream); - } - for (size_t i = 0; i < slf.size(); ++i) { - /* - if (slf[i]->GetScoreProducerWeightShortName() != "u" && - slf[i]->GetScoreProducerWeightShortName() != "tm" && - slf[i]->GetScoreProducerWeightShortName() != "I" && - slf[i]->GetScoreProducerWeightShortName() != "g") - */ - { - featureIndex = OutputFeatureWeightsForHypergraph(featureIndex, slf[i], outputSearchGraphStream); - } - } - const vector<PhraseDictionary*>& pds = PhraseDictionary::GetColl(); - for( size_t i=0; i<pds.size(); i++ ) { - featureIndex = OutputFeatureWeightsForHypergraph(featureIndex, pds[i], outputSearchGraphStream); - } - const vector<GenerationDictionary*>& gds = GenerationDictionary::GetColl(); - for( size_t i=0; i<gds.size(); i++ ) { - featureIndex = OutputFeatureWeightsForHypergraph(featureIndex, gds[i], outputSearchGraphStream); - } - + StaticData::Instance().GetAllWeights().Save(outputSearchGraphStream); } diff --git a/moses/FeatureVector.cpp b/moses/FeatureVector.cpp index 536c1a720..fd44b1002 100644 --- a/moses/FeatureVector.cpp +++ b/moses/FeatureVector.cpp @@ -214,10 +214,10 @@ void FVector::save(const string& filename) const out.close(); } -void FVector::write(ostream& out) const +void FVector::write(ostream& out,const string& sep) const { for (const_iterator i = cbegin(); i != cend(); ++i) { - out << i->first << " " << i->second << endl; + out << i->first << sep << i->second << endl; } } diff --git a/moses/FeatureVector.h b/moses/FeatureVector.h index 65f8bdc2e..e14006da7 100644 --- a/moses/FeatureVector.h +++ b/moses/FeatureVector.h @@ -167,7 +167,7 @@ public: /** Load from file - each line should be 'root[_name] value' */ bool load(const std::string& filename); void save(const std::string& filename) const; - void write(std::ostream& out) const ; + void write(std::ostream& out, const std::string& sep=" ") const ; /** Element access */ ProxyFVector operator[](const FName& name); diff --git a/moses/Manager.cpp b/moses/Manager.cpp index 196f4d997..3cffa8503 100644 --- a/moses/Manager.cpp +++ b/moses/Manager.cpp @@ -757,18 +757,7 @@ void Manager::OutputFeatureValuesForHypergraph(const Hypothesis* hypo, std::ostr { outputSearchGraphStream.setf(std::ios::fixed); outputSearchGraphStream.precision(6); - - const vector<const StatelessFeatureFunction*>& slf =StatelessFeatureFunction::GetStatelessFeatureFunctions(); - const vector<const StatefulFeatureFunction*>& sff = StatefulFeatureFunction::GetStatefulFeatureFunctions(); - size_t featureIndex = 1; - for (size_t i = 0; i < sff.size(); ++i) { - featureIndex = OutputFeatureValuesForHypergraph(featureIndex, hypo, sff[i], outputSearchGraphStream); - } - for (size_t i = 0; i < slf.size(); ++i) { - { - featureIndex = OutputFeatureValuesForHypergraph(featureIndex, hypo, slf[i], outputSearchGraphStream); - } - } + hypo->GetScoreBreakdown().Save(outputSearchGraphStream, false); } @@ -833,30 +822,6 @@ size_t Manager::OutputFeatureValuesForSLF(size_t index, bool zeros, const Hypoth // } } -size_t Manager::OutputFeatureValuesForHypergraph(size_t index, const Hypothesis* hypo, const FeatureFunction* ff, std::ostream &outputSearchGraphStream) const -{ - if (!ff->IsTuneable()) { - return index; - } - ScoreComponentCollection scoreCollection = hypo->GetScoreBreakdown(); - const Hypothesis *prevHypo = hypo->GetPrevHypo(); - if (prevHypo) { - scoreCollection.MinusEquals( prevHypo->GetScoreBreakdown() ); - } - vector<float> featureValues = scoreCollection.GetScoresForProducer(ff); - size_t numScoreComps = featureValues.size(); - - if (numScoreComps > 1) { - for (size_t i = 0; i < numScoreComps; ++i) { - outputSearchGraphStream << ff->GetScoreProducerDescription() << i << "=" << featureValues[i] << " "; - } - } else { - outputSearchGraphStream << ff->GetScoreProducerDescription() << "=" << featureValues[0] << " "; - } - - return index+numScoreComps; -} - /**! Output search graph in hypergraph format of Kenneth Heafield's lazy hypergraph decoder */ void Manager::OutputSearchGraphAsHypergraph(long translationId, std::ostream &outputSearchGraphStream) const { diff --git a/moses/Manager.h b/moses/Manager.h index ccf57c527..c015b701c 100644 --- a/moses/Manager.h +++ b/moses/Manager.h @@ -106,7 +106,6 @@ private: // Helper functions to output search graph in the hypergraph format of Kenneth Heafield's lazy hypergraph decoder void OutputFeatureValuesForHypergraph(const Hypothesis* hypo, std::ostream &outputSearchGraphStream) const; - size_t OutputFeatureValuesForHypergraph(size_t index, const Hypothesis* hypo, const FeatureFunction* ff, std::ostream &outputSearchGraphStream) const; protected: diff --git a/moses/ScoreComponentCollection.cpp b/moses/ScoreComponentCollection.cpp index 52ec00dd4..d4df4742e 100644 --- a/moses/ScoreComponentCollection.cpp +++ b/moses/ScoreComponentCollection.cpp @@ -179,25 +179,31 @@ void ScoreComponentCollection::SparseL2Regularize(float lambda) m_scores.sparseL2regularize(lambda); } -void ScoreComponentCollection::Save(ostream& out) const +void ScoreComponentCollection::Save(ostream& out, bool multiline) const { + string sep = " "; + string linesep = "\n"; + if (!multiline) { + sep = "="; + linesep = " "; + } ScoreIndexMap::const_iterator iter = s_scoreIndexes.begin(); for (; iter != s_scoreIndexes.end(); ++iter ) { string name = iter->first->GetScoreProducerDescription(); IndexPair ip = iter->second; // feature indices if (ip.second-ip.first == 1) { - out << name << " " << m_scores[ip.first] << endl; + out << name << sep << m_scores[ip.first] << linesep; } else { for (size_t i=ip.first; i < ip.second; ++i) { ostringstream fullname; fullname << name << "_" << (i + 1 - ip.first); - out << fullname.str() << " " << m_scores[i] << endl; + out << fullname.str() << sep << m_scores[i] << linesep; } } } // write sparse features - m_scores.write(out); + m_scores.write(out,sep); } void ScoreComponentCollection::Save(const string& filename) const diff --git a/moses/ScoreComponentCollection.h b/moses/ScoreComponentCollection.h index a7fa65883..858fcd5a8 100644 --- a/moses/ScoreComponentCollection.h +++ b/moses/ScoreComponentCollection.h @@ -383,7 +383,7 @@ public: size_t SparseL1Regularize(float lambda); void SparseL2Regularize(float lambda); void Save(const std::string& filename) const; - void Save(std::ostream&) const; + void Save(std::ostream&, bool multiline=true) const; void IncrementSparseHopeFeatures() { m_scores.incrementSparseHopeFeatures(); |