Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/moses
diff options
context:
space:
mode:
authorEva Hasler <ehasler@saxnot.inf.ed.ac.uk>2012-05-15 20:49:54 +0400
committerEva Hasler <ehasler@saxnot.inf.ed.ac.uk>2012-05-15 20:49:54 +0400
commitc8f04a7e481113272f01c99b77cdf26097a5fe37 (patch)
treebfe440e6edad89f2bf9628285b86e446dbfa70e9 /moses
parent30deedde9f1ce5ef22a5f3c4a6b660bac48989cd (diff)
add feature confidence
Diffstat (limited to 'moses')
-rwxr-xr-xmoses/src/FeatureVector.cpp95
-rwxr-xr-xmoses/src/FeatureVector.h7
-rwxr-xr-xmoses/src/ScoreComponentCollection.cpp5
-rwxr-xr-xmoses/src/ScoreComponentCollection.h3
4 files changed, 102 insertions, 8 deletions
diff --git a/moses/src/FeatureVector.cpp b/moses/src/FeatureVector.cpp
index f53b7e80a..7f06a7590 100755
--- a/moses/src/FeatureVector.cpp
+++ b/moses/src/FeatureVector.cpp
@@ -282,6 +282,16 @@ namespace Moses {
}
}
+ const FValue& FVector::getSafe(const FName& name) const {
+ static const FValue DEFAULT = 100000;
+ const_iterator fi = m_features.find(name);
+ if (fi == m_features.end()) {
+ return DEFAULT;
+ } else {
+ return fi->second;
+ }
+ }
+
void FVector::thresholdScale(FValue maxValue ) {
FValue factor = 1.0;
for (const_iterator i = cbegin(); i != cend(); ++i) {
@@ -367,21 +377,21 @@ namespace Moses {
}
size_t FVector::pruneSparseFeatures(size_t threshold) {
- size_t count = 0;
- vector<FName> toErase;
+ size_t count = 0;
+ vector<FName> toErase;
for (const_iterator i = cbegin(); i != cend(); ++i) {
const std::string& fname = (i->first).name();
if (FName::getHopeIdCount(fname) < threshold && FName::getFearIdCount(fname) < threshold) {
toErase.push_back(i->first);
- //std::cerr << "pruning: " << fname << " (" << FName::getIdCount(fname) << ")" << std::endl;
+ std::cerr << "pruning: " << fname << " (" << FName::getHopeIdCount(fname) << ", " << FName::getFearIdCount(fname) << ")" << std::endl;
FName::eraseId(FName::getId(fname));
++count;
}
}
- for (size_t i = 0; i < toErase.size(); ++i)
- m_features.erase(toErase[i]);
-
+ for (size_t i = 0; i < toErase.size(); ++i)
+ m_features.erase(toErase[i]);
+
return count;
}
@@ -404,6 +414,43 @@ namespace Moses {
return count;
}
+ void FVector::updateConfidenceCounts(const FVector& weightUpdate, bool signedCounts) {
+ for (size_t i = 0; i < weightUpdate.m_coreFeatures.size(); ++i) {
+ if (signedCounts) {
+ int sign = weightUpdate.m_coreFeatures[i] >= 0 ? 1 : -1;
+ m_coreFeatures[i] += (weightUpdate.m_coreFeatures[i] * weightUpdate.m_coreFeatures[i]) * sign;
+ }
+ else
+ m_coreFeatures[i] += (weightUpdate.m_coreFeatures[i] * weightUpdate.m_coreFeatures[i]);
+ }
+
+ for (const_iterator i = weightUpdate.cbegin(); i != weightUpdate.cend(); ++i) {
+ if (weightUpdate[i->first] == 0)
+ continue;
+ float value = get(i->first);
+ if (signedCounts) {
+ int sign = weightUpdate[i->first] >= 0 ? 1 : -1;
+ value += (weightUpdate[i->first] * weightUpdate[i->first]) * sign;
+ }
+ else
+ value += (weightUpdate[i->first] * weightUpdate[i->first]);
+ set(i->first, value);
+ }
+ }
+
+ void FVector::updateLearningRates(float decay, const FVector &confidenceCounts) {
+ float r0 = 1.0;
+ for (size_t i = 0; i < confidenceCounts.m_coreFeatures.size(); ++i) {
+ m_coreFeatures[i] = 1.0/(1.0/r0 + decay * abs(confidenceCounts.m_coreFeatures[i]));
+ }
+
+ for (const_iterator i = confidenceCounts.cbegin(); i != confidenceCounts.cend(); ++i) {
+ float value = 1.0/(1.0/r0 + decay * abs(i->second));
+ set(i->first, value);
+ cerr << "set learning rate: " << i->first << ": " << value << endl;
+ }
+ }
+
// count non-zero occurrences for all sparse features
void FVector::setToBinaryOf(const FVector& rhs) {
for (const_iterator i = rhs.cbegin(); i != rhs.cend(); ++i)
@@ -501,6 +548,26 @@ namespace Moses {
m_coreFeatures /= rhs;
return *this;
}
+
+ FVector& FVector::multiplyEqualsSafe(const FVector& rhs) {
+ if (rhs.m_coreFeatures.size() > m_coreFeatures.size()) {
+ resize(rhs.m_coreFeatures.size());
+ }
+ for (iterator i = begin(); i != end(); ++i) {
+ FValue lhsValue = i->second;
+ FValue rhsValue = rhs.getSafe(i->first);
+ if (rhsValue != 100000) // indicates that feature is uninitialized
+ set(i->first,lhsValue*rhsValue);
+ }
+ for (size_t i = 0; i < m_coreFeatures.size(); ++i) {
+ if (i < rhs.m_coreFeatures.size()) {
+ m_coreFeatures[i] *= rhs.m_coreFeatures[i];
+ } else {
+ m_coreFeatures[i] = 0;
+ }
+ }
+ return *this;
+ }
FValue FVector::l1norm() const {
FValue norm = 0;
@@ -533,7 +600,7 @@ namespace Moses {
norm = absValue;
}
for (size_t i = 0; i < m_coreFeatures.size(); ++i) {
- float absValue = m_coreFeatures[i];
+ float absValue = abs(m_coreFeatures[i]);
if (absValue > norm)
norm = absValue;
}
@@ -541,6 +608,16 @@ namespace Moses {
}
void FVector::l1regularize(float lambda) {
+ for (size_t i = 0; i < m_coreFeatures.size(); ++i) {
+ float value = m_coreFeatures[i];
+ if (value > 0) {
+ m_coreFeatures[i] = max(0.0f, value - lambda);
+ }
+ else {
+ m_coreFeatures[i] = min(0.0f, value + lambda);
+ }
+ }
+
for (iterator i = begin(); i != end(); ++i) {
float value = i->second;
if (value > 0) {
@@ -553,6 +630,10 @@ namespace Moses {
}
void FVector::l2regularize(float lambda) {
+ for (size_t i = 0; i < m_coreFeatures.size(); ++i) {
+ m_coreFeatures[i] *= (1 - lambda);
+ }
+
for (iterator i = begin(); i != end(); ++i) {
i->second *= (1 - lambda);
}
diff --git a/moses/src/FeatureVector.h b/moses/src/FeatureVector.h
index a291cafbb..0f6b1251e 100755
--- a/moses/src/FeatureVector.h
+++ b/moses/src/FeatureVector.h
@@ -182,6 +182,8 @@ namespace Moses {
FVector& operator*= (const FValue& rhs);
FVector& operator/= (const FValue& rhs);
+ FVector& multiplyEqualsSafe(const FVector& rhs);
+
FVector& max_equals(const FVector& rhs);
/** norms and sums */
@@ -214,6 +216,8 @@ namespace Moses {
void printSparseFearFeatureCounts();
size_t pruneSparseFeatures(size_t threshold);
size_t pruneZeroWeightFeatures();
+ void updateConfidenceCounts(const FVector& weightUpdate, bool signedCounts);
+ void updateLearningRates(float decay, const FVector& confidence_counts);
// vector which, for each element of the original vector, reflects whether an element is zero or non-zero
void setToBinaryOf(const FVector& rhs);
@@ -232,8 +236,9 @@ namespace Moses {
/** Internal get and set. */
const FValue& get(const FName& name) const;
+ const FValue& getSafe(const FName& name) const;
void set(const FName& name, const FValue& value);
-
+
FNVmap m_features;
std::valarray<FValue> m_coreFeatures;
diff --git a/moses/src/ScoreComponentCollection.cpp b/moses/src/ScoreComponentCollection.cpp
index 3d0f71bc9..cb72505ec 100755
--- a/moses/src/ScoreComponentCollection.cpp
+++ b/moses/src/ScoreComponentCollection.cpp
@@ -106,6 +106,11 @@ void ScoreComponentCollection::MultiplyEquals(const ScoreComponentCollection& rh
m_scores *= rhs.m_scores;
}
+void ScoreComponentCollection::MultiplyEqualsSafe(const ScoreComponentCollection& rhs)
+{
+ m_scores.multiplyEqualsSafe(rhs.m_scores);
+}
+
std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs)
{
os << rhs.m_scores;
diff --git a/moses/src/ScoreComponentCollection.h b/moses/src/ScoreComponentCollection.h
index 994aad1b6..c08d391f3 100755
--- a/moses/src/ScoreComponentCollection.h
+++ b/moses/src/ScoreComponentCollection.h
@@ -142,6 +142,7 @@ public:
void CoreDivideEquals(float scalar);
void DivideEquals(const ScoreComponentCollection& rhs);
void MultiplyEquals(const ScoreComponentCollection& rhs);
+ void MultiplyEqualsSafe(const ScoreComponentCollection& rhs);
void MultiplyEquals(const ScoreProducer* sp, float scalar);
size_t GetNumberWeights(const ScoreProducer* sp);
@@ -378,6 +379,8 @@ public:
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, ScoreComponentCollection &confidenceCounts) { m_scores.updateLearningRates(decay, confidenceCounts.m_scores); }
#ifdef MPI_ENABLE
public: