00001 #include "MiraWeightVector.h"
00002
00003 #include <cmath>
00004
00005 using namespace std;
00006
00007 namespace MosesTuning
00008 {
00009
00010
00014 MiraWeightVector::MiraWeightVector()
00015 : m_weights(),
00016 m_totals(),
00017 m_lastUpdated()
00018 {
00019 m_numUpdates = 0;
00020 }
00021
00026 MiraWeightVector::MiraWeightVector(const vector<ValType>& init)
00027 : m_weights(init),
00028 m_totals(init),
00029 m_lastUpdated(init.size(), 0)
00030 {
00031 m_numUpdates = 0;
00032 }
00033
00039 void MiraWeightVector::update(const MiraFeatureVector& fv, float tau)
00040 {
00041 m_numUpdates++;
00042 for(size_t i=0; i<fv.size(); i++) {
00043 update(fv.feat(i), fv.val(i)*tau);
00044 }
00045 }
00046
00050 void MiraWeightVector::tick()
00051 {
00052 m_numUpdates++;
00053 }
00054
00059 ValType MiraWeightVector::score(const MiraFeatureVector& fv) const
00060 {
00061 ValType toRet = 0.0;
00062 for(size_t i=0; i<fv.size(); i++) {
00063 toRet += weight(fv.feat(i)) * fv.val(i);
00064 }
00065 return toRet;
00066 }
00067
00071 AvgWeightVector MiraWeightVector::avg()
00072 {
00073 this->fixTotals();
00074 return AvgWeightVector(*this);
00075 }
00076
00080 void MiraWeightVector::update(size_t index, ValType delta)
00081 {
00082
00083
00084 while(index>=m_weights.size()) {
00085 m_weights.push_back(0.0);
00086 m_totals.push_back(0.0);
00087 m_lastUpdated.push_back(0);
00088 }
00089
00090
00091 m_totals[index] += (m_numUpdates - m_lastUpdated[index]) * m_weights[index] + delta;
00092 m_weights[index] += delta;
00093 m_lastUpdated[index] = m_numUpdates;
00094 }
00095
00096 void MiraWeightVector::ToSparse(SparseVector* sparse, size_t denseSize) const
00097 {
00098 for (size_t i = 0; i < m_weights.size(); ++i) {
00099 if(abs(m_weights[i])>1e-8) {
00100 if (i < denseSize) {
00101 sparse->set(i,m_weights[i]);
00102 } else {
00103
00104
00105 sparse->set(i-denseSize, m_weights[i]);
00106 }
00107 }
00108 }
00109 }
00110
00114 void MiraWeightVector::fixTotals()
00115 {
00116 for(size_t i=0; i<m_weights.size(); i++) update(i,0);
00117 }
00118
00122 ValType MiraWeightVector::weight(size_t index) const
00123 {
00124 if(index < m_weights.size()) {
00125 return m_weights[index];
00126 } else {
00127 return 0;
00128 }
00129 }
00130
00131 ValType MiraWeightVector::sqrNorm() const
00132 {
00133 ValType toRet = 0;
00134 for(size_t i=0; i<m_weights.size(); i++) {
00135 toRet += weight(i) * weight(i);
00136 }
00137 return toRet;
00138 }
00139
00140 AvgWeightVector::AvgWeightVector(const MiraWeightVector& wv)
00141 :m_wv(wv)
00142 {}
00143
00144 ostream& operator<<(ostream& o, const MiraWeightVector& e)
00145 {
00146 for(size_t i=0; i<e.m_weights.size(); i++) {
00147 if(abs(e.m_weights[i])>1e-8) {
00148 if(i>0) o << " ";
00149 o << i << ":" << e.m_weights[i];
00150 }
00151 }
00152 return o;
00153 }
00154
00155 ValType AvgWeightVector::weight(size_t index) const
00156 {
00157 if(m_wv.m_numUpdates==0) return m_wv.weight(index);
00158 else {
00159 if(index < m_wv.m_totals.size()) {
00160 return m_wv.m_totals[index] / m_wv.m_numUpdates;
00161 } else {
00162 return 0;
00163 }
00164 }
00165 }
00166
00167 ValType AvgWeightVector::score(const MiraFeatureVector& fv) const
00168 {
00169 ValType toRet = 0.0;
00170 for(size_t i=0; i<fv.size(); i++) {
00171 toRet += weight(fv.feat(i)) * fv.val(i);
00172 }
00173 return toRet;
00174 }
00175
00176 size_t AvgWeightVector::size() const
00177 {
00178 return m_wv.m_weights.size();
00179 }
00180
00181 void AvgWeightVector::ToSparse(SparseVector* sparse, size_t denseSize) const
00182 {
00183 for (size_t i = 0; i < size(); ++i) {
00184 ValType w = weight(i);
00185 if(abs(w)>1e-8) {
00186 if (i < denseSize) {
00187 sparse->set(i,w);
00188 } else {
00189
00190
00191 sparse->set(i-denseSize, w);
00192 }
00193 }
00194 }
00195 }
00196
00197
00198
00199
00200
00201
00202 }
00203