-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLrate.h
324 lines (270 loc) · 9.18 KB
/
Lrate.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* This file is part of the continuous space language and translation model toolkit
* for statistical machine translation and large vocabulary speech recognition.
*
* Copyright 2015, Holger Schwenk, LIUM, University of Le Mans, France
*
* The CSLM toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation
*
* 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
*
*
*/
#ifndef _Lrate_h_
#define _Lrate_h_
#include "Mach.h"
#include "Tools.h"
#include <boost/program_options/option.hpp>
#include <sys/types.h>
#include <string>
#include <vector>
/**
* abstract base class to compute learning rates
*/
class Lrate
{
public:
/**
* type of Lrate
*/
enum LRateType {
LR_Type_Base = 0,
LR_Type_ExpDecay,
LR_Type_AdaGrad,
LR_Type_TestAndDivide,
LR_Type_DivideAndRecover
};
/**
* creates a new Lrate object corresponding to given options
* @param sParams parameters string
* @returns new Lrate object
*/
static Lrate* NewLrate(std::string sParams);
/**
* destroys learning rate object
*/
virtual ~Lrate() {}
/**
* gets Lrate type
*/
virtual inline Lrate::LRateType GetType() const { return Lrate::LR_Type_Base; }
/**
* gets current learning rate value
*/
inline REAL GetLrate() const { return lrate; }
/**
* checks if learning rate stop value is reached
* @return true if current value is less than stop value
*/
inline bool StopReached() const
{
if (lrate <= lrate_stop) {
printf(" - minimal allowed learning rate reached\n");
return true;
}
if (lrate_iter_nogain>=lrate_maxiter) {
printf(" - no improvements after %d iterations\n", lrate_maxiter);
return true;
}
return false;
}
/**
* prints information about learning rate to standard output
*/
virtual void Info() const;
/**
* updates learning rate after a forward
* @param iNbEx number of examples seen
*/
virtual inline void UpdateLrateOnForw(ulong iNbEx) {}
/**
* updates learning rate after a backward
*/
virtual inline void UpdateLrateOnBackw() {}
/**
* updates learning rate after a cross-validation
* @param rErrDev current average error
* @param rBestErrDev best average error
* @param sBestFile name of best machine file
* @param pMach pointer to machine object which could be reloaded
* @returns true if performance is better
*/
virtual inline bool UpdateLrateOnDev(REAL rErrDev, REAL rBestErrDev, const char* sBestFile, Mach*& pMach)
{
if (rErrDev < rBestErrDev) lrate_iter_nogain=0;
else lrate_iter_nogain++;
return (rErrDev < rBestErrDev);
}
protected:
REAL lrate; ///< current value
REAL lrate_beg; ///< value at beginning
REAL lrate_mult; ///< multiplier
REAL lrate_stop; ///< stop value
REAL lrate_min; ///< minimal value (lower bound)
int lrate_maxiter; ///< maximum number of iterations without improvements
int lrate_iter_nogain; ///< counts the number of iterations without improvements
/**
* creates new learning rate object
* @param rLrateBeg learning rate value at beginning
* @param rLrateMult learning rate multiplier
* @param rLrateStop learning stop value
* @param rLrateMin learning rate minimum value
* @param rLrateMaxIter maximum number of iterations without improvement
*/
Lrate(REAL rLrateBeg = 0.01, REAL rLrateMult = 0, REAL rLrateStop = 0, REAL rLrateMin = 1e-5, int rLrateMaxIter = 10) :
lrate(rLrateBeg), lrate_beg(rLrateBeg), lrate_mult(rLrateMult), lrate_stop(rLrateStop), lrate_min(rLrateMin), lrate_maxiter(rLrateMaxIter), lrate_iter_nogain(0) {}
private:
/**
* parses parameters (type and other options)
* @param vsTokens vector of tokens
* @return vector of options
* @note throws exception of class boost::program_options::error in case of error
*/
static std::vector<boost::program_options::option> parse_params(const std::vector<std::string> &vsTokens);
};
/**
* learning rate with exponential decay
*/
class LrateExpDecay : public Lrate
{
public:
/**
* creates new learning rate object
* @param rLrateBeg learning rate value at beginning
* @param rLrateMult learning rate multiplier
* @param rLrateStop learning stop value
* @param rLrateMin learning rate minimum value
* @param rLrateMaxIter maximum number of iterations without improvement
*/
LrateExpDecay(REAL rLrateBeg = 0.01, REAL rLrateMult = 0, REAL rLrateStop = 0, REAL rLrateMin = 1e-5, int rLrateMaxIter = 10) :
Lrate(rLrateBeg, rLrateMult, rLrateStop, rLrateMin, rLrateMaxIter) {}
/**
* destroys learning rate object
*/
virtual ~LrateExpDecay() {}
/**
* gets Lrate type
*/
virtual inline Lrate::LRateType GetType() const { return Lrate::LR_Type_ExpDecay; }
/**
* prints information about learning rate to standard output
*/
virtual void Info() const;
/**
* updates learning rate after a forward
* @param iNbEx number of examples seen
*/
virtual void UpdateLrateOnForw(ulong iNbEx);
};
/**
* learning rate modified during backward
*/
class LrateAdaGrad : public Lrate
{
public:
/**
* creates new learning rate object
* @param rLrateBeg learning rate value at beginning
* @param rLrateMult learning rate multiplier
* @param rLrateStop learning stop value
* @param rLrateMin learning rate minimum value
* @param rLrateMaxIter maximum number of iterations without improvement
*/
LrateAdaGrad(REAL rLrateBeg = 0.01, REAL rLrateMult = 0, REAL rLrateStop = 0, REAL rLrateMin = 1e-5, int rLrateMaxIter = 10) :
Lrate(rLrateBeg, rLrateMult, rLrateStop, rLrateMin, rLrateMaxIter) {}
/**
* destroys learning rate object
*/
virtual ~LrateAdaGrad() {}
/**
* gets Lrate type
*/
virtual inline Lrate::LRateType GetType() const { return Lrate::LR_Type_AdaGrad; }
/**
* updates learning rate after a backward
*/
virtual inline void UpdateLrateOnBackw() { Lrate::UpdateLrateOnBackw(); }
};
/**
* learning rate modified in function of the performance on the development data
*/
class LrateTestAndDivide : public Lrate
{
public:
/**
* creates new learning rate object
* @param rLrateBeg learning rate value at beginning
* @param rLrateMult learning rate multiplier
* @param rLrateStop learning stop value
* @param rLrateMin learning rate minimum value
* @param rLrateMaxIter maximum number of iterations without improvement
*/
LrateTestAndDivide(REAL rLrateBeg = 0.01, REAL rLrateMult = 0, REAL rLrateStop = 0, REAL rLrateMin = 1e-5, int rLrateMaxIter = 10) :
Lrate(rLrateBeg, rLrateMult, rLrateStop, rLrateMin, rLrateMaxIter) {}
/**
* destroys learning rate object
*/
virtual ~LrateTestAndDivide() {}
/**
* gets Lrate type
*/
virtual inline Lrate::LRateType GetType() const { return Lrate::LR_Type_TestAndDivide; }
/**
* prints information about learning rate to standard output
*/
virtual inline void Info() const;
/**
* updates learning rate after a cross-validation
* @param rErrDev current average error
* @param rBestErrDev best average error
* @param sBestFile name of best machine file
* @param pMach pointer to machine object
* @returns true if performance is better
*/
virtual bool UpdateLrateOnDev(REAL rErrDev, REAL rBestErrDev, const char* sBestFile, Mach*& pMach);
};
/**
* learning rate modified in function of the performance on the development data
* @note previous best machine is reloaded if performance decrease
*/
class LrateDivideAndRecover : public LrateTestAndDivide
{
public:
/**
* creates new learning rate object
* @param rLrateBeg learning rate value at beginning
* @param rLrateMult learning rate multiplier
* @param rLrateStop minimum value
* @param rLrateMin learning rate minimum value
* @param rLrateMaxIter maximum number of iterations without improvement
*/
LrateDivideAndRecover(REAL rLrateBeg = 0.01, REAL rLrateMult = 0, REAL rLrateStop = 0, REAL rLrateMin = 1e-5, int rLrateMaxIter = 10) :
LrateTestAndDivide(rLrateBeg, rLrateMult, rLrateStop, rLrateMin, rLrateMaxIter) {}
/**
* destroys learning rate object
*/
virtual ~LrateDivideAndRecover() {}
/**
* gets Lrate type
*/
virtual inline Lrate::LRateType GetType() const { return Lrate::LR_Type_DivideAndRecover; }
/**
* updates learning rate after a cross-validation
* @param rErrDev current average error
* @param rBestErrDev best average error
* @param sBestFile name of best machine file
* @param pMach pointer to machine object which will be reloaded if performance decrease
* @returns true if performance is better
*/
virtual bool UpdateLrateOnDev(REAL rErrDev, REAL rBestErrDev, const char* sBestFile, Mach*& pMach);
};
#endif // _Lrate_h_