diff options
author | Hieu Hoang <hieu@hoang.co.uk> | 2013-05-29 21:16:15 +0400 |
---|---|---|
committer | Hieu Hoang <hieu@hoang.co.uk> | 2013-05-29 21:16:15 +0400 |
commit | 6249432407af8730c10bccc7894c0725fcaf5e47 (patch) | |
tree | 3ac1f094b9fdc199b04bc5ef209ce00e3596e37d /moses/ScoreComponentCollection.h | |
parent | 59bd7deb4b6b9c4f7b3b7dbb055783528fbc31ca (diff) |
beautify
Diffstat (limited to 'moses/ScoreComponentCollection.h')
-rw-r--r-- | moses/ScoreComponentCollection.h | 353 |
1 files changed, 170 insertions, 183 deletions
diff --git a/moses/ScoreComponentCollection.h b/moses/ScoreComponentCollection.h index e76c9d06b..70c2a05f1 100644 --- a/moses/ScoreComponentCollection.h +++ b/moses/ScoreComponentCollection.h @@ -64,19 +64,18 @@ class ScoreComponentCollection { friend std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs); private: - FVector m_scores; + FVector m_scores; typedef std::pair<size_t,size_t> IndexPair; typedef std::map<const FeatureFunction*,IndexPair> ScoreIndexMap; static ScoreIndexMap s_scoreIndexes; static size_t s_denseVectorSize; - static IndexPair GetIndexes(const FeatureFunction* sp) - { + static IndexPair GetIndexes(const FeatureFunction* sp) { ScoreIndexMap::const_iterator indexIter = s_scoreIndexes.find(sp); if (indexIter == s_scoreIndexes.end()) { std::cerr << "ERROR: FeatureFunction: " << sp->GetScoreProducerDescription() << - " not registered with ScoreIndexMap" << std::endl; + " not registered with ScoreIndexMap" << std::endl; std::cerr << "You must call ScoreComponentCollection.RegisterScoreProducer() " << - " for every FeatureFunction" << std::endl; + " for every FeatureFunction" << std::endl; abort(); } return indexIter->second; @@ -91,9 +90,9 @@ public: ScoreComponentCollection(); //! Clone a score collection - ScoreComponentCollection(const ScoreComponentCollection& rhs) - : m_scores(rhs.m_scores) - {} + ScoreComponentCollection(const ScoreComponentCollection& rhs) + : m_scores(rhs.m_scores) + {} ScoreComponentCollection& operator=( const ScoreComponentCollection& rhs ) { m_scores = rhs.m_scores; @@ -101,124 +100,108 @@ public: } /** - * Register a ScoreProducer with a fixed number of scores, so that it can + * Register a ScoreProducer with a fixed number of scores, so that it can * be allocated space in the dense part of the feature vector. **/ static void RegisterScoreProducer(const FeatureFunction* scoreProducer); /** Load from file */ - bool Load(const std::string& filename) - { - return m_scores.load(filename); + bool Load(const std::string& filename) { + return m_scores.load(filename); } - const FVector& GetScoresVector() const - { - return m_scores; + const FVector& GetScoresVector() const { + return m_scores; } const std::valarray<FValue> &getCoreFeatures() const { return m_scores.getCoreFeatures(); } - size_t Size() const - { - return m_scores.size(); + size_t Size() const { + return m_scores.size(); } - void Resize() - { + void Resize() { if (m_scores.coreSize() != s_denseVectorSize) { m_scores.resize(s_denseVectorSize); } } /** Create and FVector with the right number of core features */ - static FVector CreateFVector() - { + static FVector CreateFVector() { return FVector(s_denseVectorSize); } - void SetToBinaryOf(const ScoreComponentCollection& rhs) - { - m_scores.setToBinaryOf(rhs.m_scores); + void SetToBinaryOf(const ScoreComponentCollection& rhs) { + m_scores.setToBinaryOf(rhs.m_scores); } //! Set all values to 0.0 - void ZeroAll() - { - m_scores.clear(); - } - - void MultiplyEquals(float scalar); - void DivideEquals(float scalar); - void CoreDivideEquals(float scalar); - void DivideEquals(const ScoreComponentCollection& rhs); - void MultiplyEquals(const ScoreComponentCollection& rhs); - void MultiplyEqualsBackoff(const ScoreComponentCollection& rhs, float backoff); - void MultiplyEquals(float core_r0, float sparse_r0); - void MultiplyEquals(const FeatureFunction* sp, float scalar); - - size_t GetNumberWeights(const FeatureFunction* sp); - - void CoreAssign(const ScoreComponentCollection& rhs) - { - m_scores.coreAssign(rhs.m_scores); - } - - //! add the score in rhs - void PlusEquals(const ScoreComponentCollection& rhs) - { - m_scores += rhs.m_scores; - } - - // add only sparse features - void SparsePlusEquals(const ScoreComponentCollection& rhs) - { - m_scores.sparsePlusEquals(rhs.m_scores); - } - - void PlusEquals(const FVector& scores) - { - m_scores += scores; - } - - //! subtract the score in rhs - void MinusEquals(const ScoreComponentCollection& rhs) - { - m_scores -= rhs.m_scores; - } + void ZeroAll() { + m_scores.clear(); + } + + void MultiplyEquals(float scalar); + void DivideEquals(float scalar); + void CoreDivideEquals(float scalar); + void DivideEquals(const ScoreComponentCollection& rhs); + void MultiplyEquals(const ScoreComponentCollection& rhs); + void MultiplyEqualsBackoff(const ScoreComponentCollection& rhs, float backoff); + void MultiplyEquals(float core_r0, float sparse_r0); + void MultiplyEquals(const FeatureFunction* sp, float scalar); + + size_t GetNumberWeights(const FeatureFunction* sp); + + void CoreAssign(const ScoreComponentCollection& rhs) { + m_scores.coreAssign(rhs.m_scores); + } + + //! add the score in rhs + void PlusEquals(const ScoreComponentCollection& rhs) { + m_scores += rhs.m_scores; + } + + // add only sparse features + void SparsePlusEquals(const ScoreComponentCollection& rhs) { + m_scores.sparsePlusEquals(rhs.m_scores); + } + + void PlusEquals(const FVector& scores) { + m_scores += scores; + } + + //! subtract the score in rhs + void MinusEquals(const ScoreComponentCollection& rhs) { + m_scores -= rhs.m_scores; + } //For features which have an unbounded number of components - void MinusEquals(const FeatureFunction*sp, const std::string& name, float score) - { + void MinusEquals(const FeatureFunction*sp, const std::string& name, float score) { FName fname(sp->GetScoreProducerDescription(),name); m_scores[fname] -= score; } //For features which have an unbounded number of components - void SparseMinusEquals(const std::string& full_name, float score) - { + void SparseMinusEquals(const std::string& full_name, float score) { FName fname(full_name); m_scores[fname] -= score; } - //! Add scores from a single ScoreProducer only - //! The length of scores must be equal to the number of score components - //! produced by sp - void PlusEquals(const FeatureFunction* sp, const ScoreComponentCollection& scores) - { + //! Add scores from a single ScoreProducer only + //! The length of scores must be equal to the number of score components + //! produced by sp + void PlusEquals(const FeatureFunction* sp, const ScoreComponentCollection& scores) { IndexPair indexes = GetIndexes(sp); for (size_t i = indexes.first; i < indexes.second; ++i) { m_scores[i] += scores.m_scores[i]; } - } + } - //! Add scores from a single FeatureFunction only - //! The length of scores must be equal to the number of score components - //! produced by sp - void PlusEquals(const FeatureFunction* sp, const std::vector<float>& scores) - { + //! Add scores from a single FeatureFunction only + //! The length of scores must be equal to the number of score components + //! produced by sp + void PlusEquals(const FeatureFunction* sp, const std::vector<float>& scores) { IndexPair indexes = GetIndexes(sp); CHECK(scores.size() == indexes.second - indexes.first); for (size_t i = 0; i < scores.size(); ++i) { @@ -226,56 +209,50 @@ public: } } - //! Special version PlusEquals(ScoreProducer, vector<float>) - //! to add the score from a single ScoreProducer that produces - //! a single value - void PlusEquals(const FeatureFunction* sp, float score) - { + //! Special version PlusEquals(ScoreProducer, vector<float>) + //! to add the score from a single ScoreProducer that produces + //! a single value + void PlusEquals(const FeatureFunction* sp, float score) { IndexPair indexes = GetIndexes(sp); CHECK(1 == indexes.second - indexes.first); m_scores[indexes.first] += score; - } + } //For features which have an unbounded number of components - void PlusEquals(const FeatureFunction*sp, const StringPiece& name, float score) - { + void PlusEquals(const FeatureFunction*sp, const StringPiece& name, float score) { FName fname(sp->GetScoreProducerDescription(),name); m_scores[fname] += score; } //For features which have an unbounded number of components - void SparsePlusEquals(const std::string& full_name, float score) - { - FName fname(full_name); + void SparsePlusEquals(const std::string& full_name, float score) { + FName fname(full_name); m_scores[fname] += score; } - void Assign(const FeatureFunction* sp, const std::vector<float>& scores) - { + void Assign(const FeatureFunction* sp, const std::vector<float>& scores) { IndexPair indexes = GetIndexes(sp); CHECK(scores.size() == indexes.second - indexes.first); for (size_t i = 0; i < scores.size(); ++i) { m_scores[i + indexes.first] = scores[i]; } } - + //! Special version Assign(ScoreProducer, vector<float>) //! to add the score from a single ScoreProducer that produces //! a single value - void Assign(const FeatureFunction* sp, float score) - { + void Assign(const FeatureFunction* sp, float score) { IndexPair indexes = GetIndexes(sp); CHECK(1 == indexes.second - indexes.first); m_scores[indexes.first] = score; } - + // Assign core weight by index void Assign(size_t index, float score) { m_scores[index] = score; } - void Assign(const FeatureFunction*sp, const StringPiece &name, float score) - { + void Assign(const FeatureFunction*sp, const StringPiece &name, float score) { FName fname(sp->GetScoreProducerDescription(),name); m_scores[fname] = score; } @@ -285,27 +262,23 @@ public: void Assign(const FeatureFunction* sp, const std::string line); // shortcut: setting the value directly using the feature name - void Assign(const std::string name, float score) - { - FName fname(name); - m_scores[fname] = score; - } - - float InnerProduct(const ScoreComponentCollection& rhs) const - { - return m_scores.inner_product(rhs.m_scores); - } - - float PartialInnerProduct(const FeatureFunction* sp, const std::vector<float>& rhs) const - { - std::vector<float> lhs = GetScoresForProducer(sp); - CHECK(lhs.size() == rhs.size()); - return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), 0.0f); - } - - //! return a vector of all the scores associated with a certain FeatureFunction - std::vector<float> GetScoresForProducer(const FeatureFunction* sp) const - { + void Assign(const std::string name, float score) { + FName fname(name); + m_scores[fname] = score; + } + + float InnerProduct(const ScoreComponentCollection& rhs) const { + return m_scores.inner_product(rhs.m_scores); + } + + float PartialInnerProduct(const FeatureFunction* sp, const std::vector<float>& rhs) const { + std::vector<float> lhs = GetScoresForProducer(sp); + CHECK(lhs.size() == rhs.size()); + return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), 0.0f); + } + + //! return a vector of all the scores associated with a certain FeatureFunction + std::vector<float> GetScoresForProducer(const FeatureFunction* sp) const { size_t components = sp->GetNumScoreComponents(); std::vector<float> res(components); @@ -314,58 +287,52 @@ public: res[i] = m_scores[i + indexes.first]; } return res; - } + } //! get subset of scores that belong to a certain sparse ScoreProducer FVector GetVectorForProducer(const FeatureFunction* sp) const; - float GetSparseWeight(const FName& featureName) const - { + float GetSparseWeight(const FName& featureName) const { return m_scores[featureName]; } - + void PrintCoreFeatures() { m_scores.printCoreFeatures(); } - void ThresholdScaling(float maxValue) - { - // find (smallest) factor for which all weights are <= maxValue - // 0.1 / 0.14 = 0.714285714 - // 0.1 / 0.17 = 0.588235294 + void ThresholdScaling(float maxValue) { + // find (smallest) factor for which all weights are <= maxValue + // 0.1 / 0.14 = 0.714285714 + // 0.1 / 0.17 = 0.588235294 m_scores.thresholdScale(maxValue); - } - - void CapMax(float maxValue) - { - // cap all sparse features to maxValue - m_scores.capMax(maxValue); - } - - void CapMin(float minValue) - { - // cap all sparse features to minValue - m_scores.capMin(minValue); - } - - //! if a FeatureFunction produces a single score (for example, a language model score) - //! this will return it. If not, this method will throw - float GetScoreForProducer(const FeatureFunction* sp) const - { + } + + void CapMax(float maxValue) { + // cap all sparse features to maxValue + m_scores.capMax(maxValue); + } + + void CapMin(float minValue) { + // cap all sparse features to minValue + m_scores.capMin(minValue); + } + + //! if a FeatureFunction produces a single score (for example, a language model score) + //! this will return it. If not, this method will throw + float GetScoreForProducer(const FeatureFunction* sp) const { IndexPair indexes = GetIndexes(sp); CHECK(indexes.second - indexes.first == 1); return m_scores[indexes.first]; - } + } //For features which have an unbounded number of components float GetScoreForProducer - (const FeatureFunction* sp, const std::string& name) const - { + (const FeatureFunction* sp, const std::string& name) const { FName fname(sp->GetScoreProducerDescription(),name); return m_scores[fname]; } - float GetWeightedScore() const; + float GetWeightedScore() const; void ZeroDenseFeatures(const FeatureFunction* sp); void L1Normalise(); @@ -378,45 +345,65 @@ public: void SparseL2Regularize(float lambda); void Save(const std::string& filename) const; void Save(std::ostream&) const; - - void IncrementSparseHopeFeatures() { m_scores.incrementSparseHopeFeatures(); } - void IncrementSparseFearFeatures() { m_scores.incrementSparseFearFeatures(); } - void PrintSparseHopeFeatureCounts(std::ofstream& out) { m_scores.printSparseHopeFeatureCounts(out); } - void PrintSparseFearFeatureCounts(std::ofstream& out) { m_scores.printSparseFearFeatureCounts(out); } - void PrintSparseHopeFeatureCounts() { m_scores.printSparseHopeFeatureCounts(); } - void PrintSparseFearFeatureCounts() { m_scores.printSparseFearFeatureCounts(); } - size_t PruneSparseFeatures(size_t threshold) { return m_scores.pruneSparseFeatures(threshold); } - size_t PruneZeroWeightFeatures() { return m_scores.pruneZeroWeightFeatures(); } - void UpdateConfidenceCounts(ScoreComponentCollection &weightUpdate, bool signedCounts) { m_scores.updateConfidenceCounts(weightUpdate.m_scores, signedCounts); } - void UpdateLearningRates(float decay_core, float decay_sparse, ScoreComponentCollection &confidenceCounts, float core_r0, float sparse_r0) { m_scores.updateLearningRates(decay_core, decay_sparse, confidenceCounts.m_scores, core_r0, sparse_r0); } + + void IncrementSparseHopeFeatures() { + m_scores.incrementSparseHopeFeatures(); + } + void IncrementSparseFearFeatures() { + m_scores.incrementSparseFearFeatures(); + } + void PrintSparseHopeFeatureCounts(std::ofstream& out) { + m_scores.printSparseHopeFeatureCounts(out); + } + void PrintSparseFearFeatureCounts(std::ofstream& out) { + m_scores.printSparseFearFeatureCounts(out); + } + void PrintSparseHopeFeatureCounts() { + m_scores.printSparseHopeFeatureCounts(); + } + void PrintSparseFearFeatureCounts() { + m_scores.printSparseFearFeatureCounts(); + } + size_t PruneSparseFeatures(size_t threshold) { + return m_scores.pruneSparseFeatures(threshold); + } + size_t PruneZeroWeightFeatures() { + return m_scores.pruneZeroWeightFeatures(); + } + void UpdateConfidenceCounts(ScoreComponentCollection &weightUpdate, bool signedCounts) { + m_scores.updateConfidenceCounts(weightUpdate.m_scores, signedCounts); + } + void UpdateLearningRates(float decay_core, float decay_sparse, ScoreComponentCollection &confidenceCounts, float core_r0, float sparse_r0) { + m_scores.updateLearningRates(decay_core, decay_sparse, confidenceCounts.m_scores, core_r0, sparse_r0); + } #ifdef MPI_ENABLE - public: - friend class boost::serialization::access; - - private: - //serialization - template<class Archive> - void save(Archive &ar, const unsigned int version) const { - ar << m_scores; - } - - template<class Archive> - void load(Archive &ar, const unsigned int version) { - ar >> m_scores; +public: + friend class boost::serialization::access; - } +private: + //serialization + template<class Archive> + void save(Archive &ar, const unsigned int version) const { + ar << m_scores; + } + + template<class Archive> + void load(Archive &ar, const unsigned int version) { + ar >> m_scores; + + } + + BOOST_SERIALIZATION_SPLIT_MEMBER() - BOOST_SERIALIZATION_SPLIT_MEMBER() - #endif }; struct SCCPlus { ScoreComponentCollection operator() - (const ScoreComponentCollection& lhs, - const ScoreComponentCollection& rhs) { + (const ScoreComponentCollection& lhs, + const ScoreComponentCollection& rhs) { ScoreComponentCollection sum(lhs); sum.PlusEquals(rhs); return sum; |