diff options
author | redpony <redpony@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-09-28 17:06:08 +0000 |
---|---|---|
committer | redpony <redpony@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-09-28 17:06:08 +0000 |
commit | 3db6b004ae1e2319f52862d428c20be5a1538993 (patch) | |
tree | b35c697027afd92324d8d9a63c8e6b27c32d2339 /utils | |
parent | 521dc2fdbf7eee7d6a86410f490ba7a76691590b (diff) |
use boost mpi, fix L1 stochastic optimizer
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@659 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'utils')
-rw-r--r-- | utils/sparse_vector.h | 33 | ||||
-rw-r--r-- | utils/weights.cc | 7 | ||||
-rw-r--r-- | utils/weights.h | 1 |
3 files changed, 41 insertions, 0 deletions
diff --git a/utils/sparse_vector.h b/utils/sparse_vector.h index 5d0dac27..c5e18b96 100644 --- a/utils/sparse_vector.h +++ b/utils/sparse_vector.h @@ -56,6 +56,10 @@ TODO: specialize for int value types, where it probably makes sense to check if #include "small_vector.h" #include "string_to.h" +#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP +#include <boost/serialization/map.hpp> +#endif + template <class T> inline T & extend_vector(std::vector<T> &v,int i) { if (i>=v.size()) @@ -510,6 +514,35 @@ public: private: MapType values_; + +#if HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP + friend class boost::serialization::access; + template<class Archive> + void save(Archive & ar, const unsigned int version) const { + (void) version; + int eff_size = values_.size(); + const_iterator it = this->begin(); + if (values_.find(0) != values_.end()) { ++it; --eff_size; } + ar & eff_size; + while (it != this->end()) { + const std::pair<std::string, T> wire_pair(FD::Convert(it->first), it->second); + ar & wire_pair; + ++it; + } + } + template<class Archive> + void load(Archive & ar, const unsigned int version) { + (void) version; + this->clear(); + int sz; ar & sz; + for (int i = 0; i < sz; ++i) { + std::pair<std::string, T> wire_pair; + ar & wire_pair; + this->set_value(FD::Convert(wire_pair.first), wire_pair.second); + } + } + BOOST_SERIALIZATION_SPLIT_MEMBER() +#endif }; template <class T> diff --git a/utils/weights.cc b/utils/weights.cc index ea8bd816..53089f89 100644 --- a/utils/weights.cc +++ b/utils/weights.cc @@ -81,3 +81,10 @@ void Weights::InitFromVector(const std::vector<double>& w) { cerr << "WARNING: initializing weight vector has more features than the global feature dictionary!\n"; wv_.resize(FD::NumFeats(), 0); } + +void Weights::InitFromVector(const SparseVector<double>& w) { + wv_.clear(); + wv_.resize(FD::NumFeats(), 0.0); + for (int i = 1; i < FD::NumFeats(); ++i) + wv_[i] = w.value(i); +} diff --git a/utils/weights.h b/utils/weights.h index 1849f959..cc20283c 100644 --- a/utils/weights.h +++ b/utils/weights.h @@ -14,6 +14,7 @@ class Weights { void InitVector(std::vector<double>* w) const; void InitSparseVector(SparseVector<double>* w) const; void InitFromVector(const std::vector<double>& w); + void InitFromVector(const SparseVector<double>& w); private: std::vector<double> wv_; }; |