File size: 6,823 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
   Moses - statistical machine translation system
   Copyright (C) 2005-2015 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "ExpectedBleuOptimizer.h"


namespace ExpectedBleuTraining
{


void ExpectedBleuOptimizer::AddTrainingInstance(const size_t nBestSizeCount, 
                                                const std::vector<float>& sBleu,
                                                const std::vector<double>& overallScoreUntransformed, 
                                                const std::vector< boost::unordered_map<size_t, float> > &sparseScore,
                                                bool maintainUpdateSet) 
{

  // compute xBLEU
  double sumUntransformedScores = 0.0;
  for (std::vector<double>::const_iterator overallScoreUntransformedIt=overallScoreUntransformed.begin();
       overallScoreUntransformedIt!=overallScoreUntransformed.end(); ++overallScoreUntransformedIt)
  {
    sumUntransformedScores += *overallScoreUntransformedIt;
  }

  double xBleu = 0.0;
  assert(nBestSizeCount == overallScoreUntransformed.size());
  std::vector<double> p;
  for (size_t i=0; i<nBestSizeCount; ++i)
  {
    if (sumUntransformedScores != 0) {
      p.push_back( overallScoreUntransformed[i] / sumUntransformedScores );
    } else {
      p.push_back( 0 );
    }
    xBleu += p.back() * sBleu[ i ];
  }

  for (size_t i=0; i<nBestSizeCount; ++i)
  {
    double D = sBleu[ i ] - xBleu;
    for (boost::unordered_map<size_t, float>::const_iterator sparseScoreIt=sparseScore[i].begin();
         sparseScoreIt!=sparseScore[i].end(); ++sparseScoreIt)
    {
      const size_t name = sparseScoreIt->first;
      float N = sparseScoreIt->second;
      if ( std::fpclassify( p[i] * N * D ) == FP_SUBNORMAL )
      {
        m_err << "Error: encountered subnormal value: p[i] * N * D= " << p[i] * N * D 
              << " with p[i]= " << p[i] << " N= " << N << " D= " << D << '\n';
        m_err.flush();
        exit(1);
      } else {
        m_gradient[name] += p[i] * N * D;
        if ( maintainUpdateSet )
        {
          m_updateSet.insert(name);
        }
      }
    }
  }

  m_xBleu += xBleu;
}


void ExpectedBleuOptimizer::InitSGD(const std::vector<float>& sparseScalingFactor)
{
  const size_t nFeatures = sparseScalingFactor.size();
  memcpy(&m_previousSparseScalingFactor.at(0), &sparseScalingFactor.at(0), nFeatures);
  m_gradient.resize(nFeatures);
}


float ExpectedBleuOptimizer::UpdateSGD(std::vector<float>& sparseScalingFactor, 
                                       size_t batchSize,
                                       bool useUpdateSet)
{

  float xBleu = m_xBleu / batchSize;

  // update sparse scaling factors

  if (useUpdateSet) {

    for (std::set<size_t>::const_iterator it = m_updateSet.begin(); it != m_updateSet.end(); ++it)
    {
      size_t name = *it;
      UpdateSingleScalingFactorSGD(name, sparseScalingFactor, batchSize);
    }
    
    m_updateSet.clear();

  } else {

    for (size_t name=0; name<sparseScalingFactor.size(); ++name)
    {
      UpdateSingleScalingFactorSGD(name, sparseScalingFactor, batchSize);
    }

  }

  m_xBleu = 0;
  m_gradient.clear();
  return xBleu;
}


void ExpectedBleuOptimizer::UpdateSingleScalingFactorSGD(size_t name,
                                                         std::vector<float>& sparseScalingFactor, 
                                                         size_t batchSize)
{
  // regularization
  if ( m_regularizationParameter != 0 )
  {
    m_gradient[name] = m_gradient[name] / m_xBleu - m_regularizationParameter * 2 * sparseScalingFactor[name];
  } else {
    // need to normalize by dividing by batchSize
    m_gradient[name] /= batchSize;
  }

  // the actual update
  sparseScalingFactor[name] += m_learningRate * m_gradient[name];

  // discard scaling factors below a threshold
  if ( fabs(sparseScalingFactor[name]) < m_floorAbsScalingFactor )
  {
    sparseScalingFactor[name] = 0;
  }
}


void ExpectedBleuOptimizer::InitRPROP(const std::vector<float>& sparseScalingFactor)
{
  const size_t nFeatures = sparseScalingFactor.size();
  m_previousSparseScalingFactor.resize(nFeatures);
  memcpy(&m_previousSparseScalingFactor.at(0), &sparseScalingFactor.at(0), nFeatures);
  m_previousGradient.resize(nFeatures);
  m_gradient.resize(nFeatures);
  m_stepSize.resize(nFeatures, m_initialStepSize);
}


float ExpectedBleuOptimizer::UpdateRPROP(std::vector<float>& sparseScalingFactor,
                                         const size_t batchSize)
{

  float xBleu = m_xBleu / batchSize;

  // update sparse scaling factors

  for (size_t name=0; name<sparseScalingFactor.size(); ++name)
  {
    // Sum of gradients. All we need is the sign. Don't need to normalize by dividing by batchSize.

    // regularization
    if ( m_regularizationParameter != 0 )
    {
      m_gradient[name] = m_gradient[name] / m_xBleu - m_regularizationParameter * 2 * sparseScalingFactor[name];
    }

    // step size
    int sign = Sign(m_gradient[name]) * Sign(m_previousGradient[name]);
    if (sign > 0) {
      m_stepSize[name] *= m_increaseRate;
    } else if (sign < 0) {
      m_stepSize[name] *= m_decreaseRate;
    }
    if (m_stepSize[name] < m_minStepSize) {
      m_stepSize[name] = m_minStepSize;
    }
    if (m_stepSize[name] > m_maxStepSize) {
      m_stepSize[name] = m_maxStepSize;
    }

    // the actual update

    m_previousGradient[name] = m_gradient[name];
    if (sign >= 0) {
      if (m_gradient[name] > 0) {
        m_previousSparseScalingFactor[name] = sparseScalingFactor[name];
        sparseScalingFactor[name] += m_stepSize[name];
      } else if (m_gradient[name] < 0) {
        m_previousSparseScalingFactor[name] = sparseScalingFactor[name];
        sparseScalingFactor[name] -= m_stepSize[name];
      }
    } else { 
      sparseScalingFactor[name] = m_previousSparseScalingFactor[name];
      // m_previousGradient[name] = 0;
    }

    // discard scaling factors below a threshold
    if ( fabs(sparseScalingFactor[name]) < m_floorAbsScalingFactor )
    {
      sparseScalingFactor[name] = 0;
    }
  }

  m_xBleu = 0;
  m_gradient.clear();
  return xBleu;
}


}