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

MiraWeightVector.cpp « mert - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 367305c4874c5fd8c39043e1cf9559a2b38df065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "MiraWeightVector.h"

#include <cmath>

using namespace std;

namespace MosesTuning
{


/**
 * Constructor, initializes to the zero vector
 */
MiraWeightVector::MiraWeightVector()
  : m_weights(),
    m_totals(),
    m_lastUpdated()
{
  m_numUpdates = 0;
}

/**
 * Constructor with provided initial vector
 * \param init Initial feature values
 */
MiraWeightVector::MiraWeightVector(const vector<ValType>& init)
  : m_weights(init),
    m_totals(init),
    m_lastUpdated(init.size(), 0)
{
  m_numUpdates = 0;
}

/**
 * Update a the model
 * \param fv  Feature vector to be added to the weights
 * \param tau FV will be scaled by this value before update
 */
void MiraWeightVector::update(const MiraFeatureVector& fv, float tau)
{
  m_numUpdates++;
  for(size_t i=0; i<fv.size(); i++) {
    update(fv.feat(i), fv.val(i)*tau);
  }
}

/**
 * Perform an empty update (affects averaging)
 */
void MiraWeightVector::tick()
{
  m_numUpdates++;
}

/**
 * Score a feature vector according to the model
 * \param fv Feature vector to be scored
 */
ValType MiraWeightVector::score(const MiraFeatureVector& fv) const
{
  ValType toRet = 0.0;
  for(size_t i=0; i<fv.size(); i++) {
    toRet += weight(fv.feat(i)) * fv.val(i);
  }
  return toRet;
}

/**
 * Return an averaged view of this weight vector
 */
AvgWeightVector MiraWeightVector::avg()
{
  this->fixTotals();
  return AvgWeightVector(*this);
}

/**
 * Updates a weight and lazily updates its total
 */
void MiraWeightVector::update(size_t index, ValType delta)
{

  // Handle previously unseen weights
  while(index>=m_weights.size()) {
    m_weights.push_back(0.0);
    m_totals.push_back(0.0);
    m_lastUpdated.push_back(0);
  }

  // Book keeping for w = w + delta
  m_totals[index] += (m_numUpdates - m_lastUpdated[index]) * m_weights[index] + delta;
  m_weights[index] += delta;
  m_lastUpdated[index] = m_numUpdates;
}

void MiraWeightVector::ToSparse(SparseVector* sparse, size_t denseSize) const
{
  for (size_t i = 0; i < m_weights.size(); ++i) {
    if(abs(m_weights[i])>1e-8) {
      if (i < denseSize) {
        sparse->set(i,m_weights[i]);
      } else {
        //The ids in MiraFeatureVector/MiraWeightVector for sparse features
        //need to be translated when converting back to SparseVector.
        sparse->set(i-denseSize, m_weights[i]);
      }
    }
  }
}

/**
 * Make sure everyone's total is up-to-date
 */
void MiraWeightVector::fixTotals()
{
  for(size_t i=0; i<m_weights.size(); i++) update(i,0);
}

/**
 * Helper to handle out of range weights
 */
ValType MiraWeightVector::weight(size_t index) const
{
  if(index < m_weights.size()) {
    return m_weights[index];
  } else {
    return 0;
  }
}

ValType MiraWeightVector::sqrNorm() const
{
  ValType toRet = 0;
  for(size_t i=0; i<m_weights.size(); i++) {
    toRet += weight(i) * weight(i);
  }
  return toRet;
}

AvgWeightVector::AvgWeightVector(const MiraWeightVector& wv)
  :m_wv(wv)
{}

ostream& operator<<(ostream& o, const MiraWeightVector& e)
{
  for(size_t i=0; i<e.m_weights.size(); i++) {
    if(abs(e.m_weights[i])>1e-8) {
      if(i>0) o << " ";
      o << i << ":" << e.m_weights[i];
    }
  }
  return o;
}

ValType AvgWeightVector::weight(size_t index) const
{
  if(m_wv.m_numUpdates==0) return m_wv.weight(index);
  else {
    if(index < m_wv.m_totals.size()) {
      return m_wv.m_totals[index] / m_wv.m_numUpdates;
    } else {
      return 0;
    }
  }
}

ValType AvgWeightVector::score(const MiraFeatureVector& fv) const
{
  ValType toRet = 0.0;
  for(size_t i=0; i<fv.size(); i++) {
    toRet += weight(fv.feat(i)) * fv.val(i);
  }
  return toRet;
}

size_t AvgWeightVector::size() const
{
  return m_wv.m_weights.size();
}

void AvgWeightVector::ToSparse(SparseVector* sparse, size_t denseSize) const
{
  for (size_t i = 0; i < size(); ++i) {
    ValType w = weight(i);
    if(abs(w)>1e-8) {
      if (i < denseSize) {
        sparse->set(i,w);
      } else {
        //The ids in MiraFeatureVector/MiraWeightVector for sparse features
        //need to be translated when converting back to SparseVector.
        sparse->set(i-denseSize, w);
      }
    }
  }
}

// --Emacs trickery--
// Local Variables:
// mode:c++
// c-basic-offset:2
// End:
}