diff options
author | Michael Denkowski <mdenkows@cs.cmu.edu> | 2013-09-17 12:46:02 -0700 |
---|---|---|
committer | Michael Denkowski <mdenkows@cs.cmu.edu> | 2013-09-17 12:46:02 -0700 |
commit | a30a2e59ff117ad6bae80ece2bf535767daf7db6 (patch) | |
tree | 7fdceee683e6080cf6c99d871b2281384ba529c3 /utils | |
parent | 08be69abb923b74f7dc27712d6bef7f6e4a05377 (diff) |
Save/load weights in stream mira
Diffstat (limited to 'utils')
-rw-r--r-- | utils/weights.cc | 26 | ||||
-rw-r--r-- | utils/weights.h | 5 |
2 files changed, 31 insertions, 0 deletions
diff --git a/utils/weights.cc b/utils/weights.cc index 575877b6..1284f686 100644 --- a/utils/weights.cc +++ b/utils/weights.cc @@ -4,6 +4,7 @@ #include "fdict.h" #include "filelib.h" +#include "stringlib.h" #include "verbose.h" using namespace std; @@ -156,4 +157,29 @@ void Weights::ShowLargestFeatures(const vector<weight_t>& w) { cerr << endl; } +string Weights::GetString(const vector<weight_t>& w, + bool hide_zero_value_features) { + ostringstream os; + os.precision(17); + int nf = FD::NumFeats(); + for (unsigned i = 1; i < nf; i++) { + if (hide_zero_value_features && w[i] == 0.0) { + continue; + } + os << FD::Convert(i) << '=' << w[i]; + if (i < nf - 1) { + os << ' '; + } + } + return os.str(); +} +void Weights::UpdateFromString(string& w_string, + vector<weight_t>& w) { + vector<string> tok = SplitOnWhitespace(w_string); + for (vector<string>::iterator i = tok.begin(); i != tok.end(); i++) { + int delim = i->find('='); + int fid = FD::Convert(i->substr(0, delim)); + w[fid] = strtod(i->substr(delim + 1).c_str(), NULL); + } +} diff --git a/utils/weights.h b/utils/weights.h index 30f71db0..920fdd75 100644 --- a/utils/weights.h +++ b/utils/weights.h @@ -23,6 +23,11 @@ class Weights { static void SanityCheck(const std::vector<weight_t>& w); // write weights with largest magnitude to cerr static void ShowLargestFeatures(const std::vector<weight_t>& w); + static std::string GetString(const std::vector<weight_t>& w, + bool hide_zero_value_features = true); + // Assumes weights are already initialized for now + static void UpdateFromString(std::string& w_string, + std::vector<weight_t>& w); private: Weights(); }; |