20
20
import opennlp .tools .ml .ArrayMath ;
21
21
22
22
/**
23
- * Class that performs line search to find minimum.
23
+ * Performs line search to find a minimum.
24
+ *
25
+ * @see <a href="https://link.springer.com/book/10.1007/978-0-387-40065-5">
26
+ * Nocedal & Wright 2006, Numerical Optimization</a>, p. 37)
24
27
*/
25
28
public class LineSearch {
26
29
private static final double C = 0.0001 ;
27
30
private static final double RHO = 0.5 ; // decrease of step size (must be from 0 to 1)
28
31
29
32
/**
30
- * Conducts a backtracking line search (see Nocedal & Wright 2006, Numerical Optimization, p. 37).
33
+ * Conducts a backtracking line search.
34
+ *
35
+ * @param function The {@link Function} to apply.
36
+ * @param direction The {@code double[]} representing the direction to search into.
37
+ * @param lsr The {@link LineSearchResult} to transport results in.
38
+ * @param initialStepSize The initial step size to apply. Must be greater than {@code 0}.
31
39
*/
32
- public static void doLineSearch (Function function ,
33
- double [] direction , LineSearchResult lsr , double initialStepSize ) {
40
+ public static void doLineSearch (Function function , double [] direction ,
41
+ LineSearchResult lsr , double initialStepSize ) {
34
42
double stepSize = initialStepSize ;
35
43
int currFctEvalCount = lsr .getFctEvalCount ();
36
44
double [] x = lsr .getNextPoint ();
@@ -79,6 +87,12 @@ public static void doLineSearch(Function function,
79
87
/**
80
88
* Conducts a constrained line search (see section 3.2 in the paper "Scalable Training
81
89
* of L1-Regularized Log-Linear Models", Andrew et al. 2007)
90
+ *
91
+ * @param function The {@link Function} to apply.
92
+ * @param direction The {@code double[]} representing the direction to search into.
93
+ * @param lsr The {@link LineSearchResult} to transport results in.
94
+ * @param l1Cost The L1-regularization costs. Must be equal or greater than {@code 0}.
95
+ * @param initialStepSize The initial step size to apply. Must be greater than {@code 0}.
82
96
*/
83
97
public static void doConstrainedLineSearch (Function function ,
84
98
double [] direction , LineSearchResult lsr , double l1Cost , double initialStepSize ) {
@@ -146,7 +160,8 @@ public static void doConstrainedLineSearch(Function function,
146
160
// ------------------------------------------------------------------------------------- //
147
161
148
162
/**
149
- * Represents a LineSearch result.
163
+ * Represents a {@link LineSearch} result encapsulating the relevant data
164
+ * at a point in time during computation.
150
165
*/
151
166
public static class LineSearchResult {
152
167
@@ -162,72 +177,46 @@ public static class LineSearchResult {
162
177
private double [] signVector ;
163
178
164
179
/**
165
- * Constructor
180
+ * Initializes a {@link LineSearchResult} object with the specified parameters.
166
181
*/
167
- public LineSearchResult (
168
- double stepSize ,
169
- double valueAtCurr ,
170
- double valueAtNext ,
171
- double [] gradAtCurr ,
172
- double [] gradAtNext ,
173
- double [] currPoint ,
174
- double [] nextPoint ,
175
- int fctEvalCount )
182
+ public LineSearchResult (double stepSize , double valueAtCurr , double valueAtNext ,
183
+ double [] gradAtCurr , double [] gradAtNext , double [] currPoint ,
184
+ double [] nextPoint , int fctEvalCount )
176
185
{
177
186
setAll (stepSize , valueAtCurr , valueAtNext , gradAtCurr , gradAtNext ,
178
187
currPoint , nextPoint , fctEvalCount );
179
188
}
180
189
181
190
/**
182
- * Constructor with sign vector
191
+ * Initializes a {@link LineSearchResult} object with the specified parameters.
183
192
*/
184
- public LineSearchResult (
185
- double stepSize ,
186
- double valueAtCurr ,
187
- double valueAtNext ,
188
- double [] gradAtCurr ,
189
- double [] gradAtNext ,
190
- double [] pseudoGradAtNext ,
191
- double [] currPoint ,
192
- double [] nextPoint ,
193
- double [] signVector ,
194
- int fctEvalCount )
193
+ public LineSearchResult (double stepSize , double valueAtCurr , double valueAtNext ,
194
+ double [] gradAtCurr , double [] gradAtNext , double [] pseudoGradAtNext ,
195
+ double [] currPoint , double [] nextPoint , double [] signVector ,
196
+ int fctEvalCount )
195
197
{
196
198
setAll (stepSize , valueAtCurr , valueAtNext , gradAtCurr , gradAtNext ,
197
199
pseudoGradAtNext , currPoint , nextPoint , signVector , fctEvalCount );
198
200
}
199
201
200
202
/**
201
- * Update line search elements
203
+ * Updates line search elements.
202
204
*/
203
- public void setAll (
204
- double stepSize ,
205
- double valueAtCurr ,
206
- double valueAtNext ,
207
- double [] gradAtCurr ,
208
- double [] gradAtNext ,
209
- double [] currPoint ,
210
- double [] nextPoint ,
211
- int fctEvalCount )
205
+ public void setAll (double stepSize , double valueAtCurr , double valueAtNext ,
206
+ double [] gradAtCurr , double [] gradAtNext , double [] currPoint ,
207
+ double [] nextPoint , int fctEvalCount )
212
208
{
213
209
setAll (stepSize , valueAtCurr , valueAtNext , gradAtCurr , gradAtNext ,
214
210
null , currPoint , nextPoint , null , fctEvalCount );
215
211
}
216
212
217
213
/**
218
- * Update line search elements
214
+ * Updates line search elements.
219
215
*/
220
- public void setAll (
221
- double stepSize ,
222
- double valueAtCurr ,
223
- double valueAtNext ,
224
- double [] gradAtCurr ,
225
- double [] gradAtNext ,
226
- double [] pseudoGradAtNext ,
227
- double [] currPoint ,
228
- double [] nextPoint ,
229
- double [] signVector ,
230
- int fctEvalCount )
216
+ public void setAll (double stepSize , double valueAtCurr , double valueAtNext ,
217
+ double [] gradAtCurr , double [] gradAtNext , double [] pseudoGradAtNext ,
218
+ double [] currPoint , double [] nextPoint , double [] signVector ,
219
+ int fctEvalCount )
231
220
{
232
221
this .stepSize = stepSize ;
233
222
this .valueAtCurr = valueAtCurr ;
@@ -326,35 +315,50 @@ public void setFctEvalCount(int fctEvalCount) {
326
315
}
327
316
328
317
/**
329
- * Initial linear search object.
318
+ * Initial linear search object for L1-regularization.
319
+ *
320
+ * @param valueAtX The value at {@code x}.
321
+ * @param gradAtX The gradient at {@code x}.
322
+ * @param x The input {@code double[]} vector.
323
+ *
324
+ * @return The {@link LineSearchResult} holding the results.
330
325
*/
331
- public static LineSearchResult getInitialObject (
332
- double valueAtX ,
333
- double [] gradAtX ,
334
- double [] x )
335
- {
326
+ public static LineSearchResult getInitialObject (double valueAtX , double [] gradAtX ,
327
+ double [] x ) {
336
328
return getInitialObject (valueAtX , gradAtX , null , x , null , 0 );
337
329
}
338
330
339
331
/**
340
332
* Initial linear search object for L1-regularization.
333
+ *
334
+ * @param valueAtX The value at {@code x}.
335
+ * @param gradAtX The gradient at {@code x}.
336
+ * @param pseudoGradAtX The pseudo-gradient at {@code x}.
337
+ * @param x The input {@code double[]} vector.
338
+ *
339
+ * @return The {@link LineSearchResult} holding the results.
341
340
*/
342
- public static LineSearchResult getInitialObjectForL1 (
343
- double valueAtX ,
344
- double [] gradAtX ,
345
- double [] pseudoGradAtX ,
346
- double [] x )
347
- {
341
+ public static LineSearchResult getInitialObjectForL1 (double valueAtX , double [] gradAtX ,
342
+ double [] pseudoGradAtX , double [] x ) {
348
343
return getInitialObject (valueAtX , gradAtX , pseudoGradAtX , x , new double [x .length ], 0 );
349
344
}
350
345
351
- public static LineSearchResult getInitialObject (
352
- double valueAtX ,
353
- double [] gradAtX ,
354
- double [] pseudoGradAtX ,
355
- double [] x ,
356
- double [] signX ,
357
- int fctEvalCount ) {
346
+ /**
347
+ * Initial linear search object for L1-regularization.
348
+ *
349
+ * @param valueAtX The value at {@code x}.
350
+ * @param gradAtX The gradient at {@code x}.
351
+ * @param pseudoGradAtX The pseudo-gradient at {@code x}.
352
+ * @param x The input {@code double[]} vector.
353
+ * @param signX The sign {@code double[]} vector for {@code x}.
354
+ * @param fctEvalCount The number of function evaluations.
355
+ * Must be equal to or greater than {@code 0}.
356
+ *
357
+ * @return The {@link LineSearchResult} holding the results.
358
+ */
359
+ public static LineSearchResult getInitialObject (double valueAtX , double [] gradAtX ,
360
+ double [] pseudoGradAtX , double [] x ,
361
+ double [] signX , int fctEvalCount ) {
358
362
return new LineSearchResult (0.0 , 0.0 , valueAtX , new double [x .length ], gradAtX ,
359
363
pseudoGradAtX , new double [x .length ], x , signX , fctEvalCount );
360
364
}
0 commit comments