00001 /* 00002 * MiraWeightVector.h 00003 * kbmira - k-best Batch MIRA 00004 * 00005 * A self-averaging weight-vector. Good for 00006 * perceptron learning as well. 00007 * 00008 */ 00009 00010 #ifndef MERT_MIRA_WEIGHT_VECTOR_H 00011 #define MERT_MIRA_WEIGHT_VECTOR_H 00012 00013 #include <vector> 00014 #include <iostream> 00015 00016 #include "MiraFeatureVector.h" 00017 00018 namespace MosesTuning 00019 { 00020 00021 00022 class AvgWeightVector; 00023 00024 class MiraWeightVector 00025 { 00026 public: 00030 MiraWeightVector(); 00031 00036 MiraWeightVector(const std::vector<ValType>& init); 00037 00043 void update(const MiraFeatureVector& fv, float tau); 00044 00048 void tick(); 00049 00054 ValType score(const MiraFeatureVector& fv) const; 00055 00059 ValType sqrNorm() const; 00060 00064 AvgWeightVector avg(); 00065 00069 void ToSparse(SparseVector* sparse, size_t denseSize) const; 00070 00071 friend class AvgWeightVector; 00072 00073 friend std::ostream& operator<<(std::ostream& o, const MiraWeightVector& e); 00074 00075 private: 00079 void update(std::size_t index, ValType delta); 00080 00084 void fixTotals(); 00085 00089 ValType weight(std::size_t index) const; 00090 00091 std::vector<ValType> m_weights; 00092 std::vector<ValType> m_totals; 00093 std::vector<std::size_t> m_lastUpdated; 00094 std::size_t m_numUpdates; 00095 }; 00096 00100 class AvgWeightVector 00101 { 00102 public: 00103 AvgWeightVector(const MiraWeightVector& wv); 00104 ValType score(const MiraFeatureVector& fv) const; 00105 ValType weight(std::size_t index) const; 00106 std::size_t size() const; 00107 void ToSparse(SparseVector* sparse, size_t num_dense) const; 00108 private: 00109 const MiraWeightVector& m_wv; 00110 }; 00111 00112 00113 00114 // --Emacs trickery-- 00115 // Local Variables: 00116 // mode:c++ 00117 // c-basic-offset:2 00118 // End: 00119 00120 } 00121 #endif // MERT_WEIGHT_VECTOR_H