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 /moses | |
parent | 6549d24d35911a013f6b38f8dccbedb953210791 (diff) |
Sparse features trainable with hg mira
(In phrase-based Moses)
Diffstat (limited to 'moses')
-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 |
6 files changed, 15 insertions, 45 deletions
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(); |