Skip to content

Commit 7d4b450

Browse files
authored
OPENNLP-1669: Improve JavaDoc of QN related classes (#709)
1 parent a3a5e0f commit 7d4b450

File tree

9 files changed

+193
-125
lines changed

9 files changed

+193
-125
lines changed

opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/Function.java

+21
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,30 @@
2222
*/
2323
public interface Function {
2424

25+
/**
26+
* @return Retrieves the dimension value.
27+
*/
2528
int getDimension();
2629

30+
/**
31+
* Computes the function value for {@code x}.
32+
*
33+
* @param x The input vector.
34+
* @return Returns the computed value for {@code x}.
35+
*
36+
* @apiNote The parameter {@code x} should be validated and if inconsistencies are encountered
37+
* an appropriate exception should be raised, e.g. {@link IllegalArgumentException}.
38+
*/
2739
double valueAt(double[] x);
2840

41+
/**
42+
* Computes the gradient for {@code x}.
43+
*
44+
* @param x The input vector.
45+
* @return Returns the computed gradient for {@code x}.
46+
*
47+
* @apiNote The parameter {@code x} should be validated and if inconsistencies are encountered
48+
* an appropriate exception should be raised, e.g. {@link IllegalArgumentException}.
49+
*/
2950
double[] gradientAt(double[] x);
3051
}

opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/LineSearch.java

+72-68
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,25 @@
2020
import opennlp.tools.ml.ArrayMath;
2121

2222
/**
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)
2427
*/
2528
public class LineSearch {
2629
private static final double C = 0.0001;
2730
private static final double RHO = 0.5; // decrease of step size (must be from 0 to 1)
2831

2932
/**
30-
* Conducts a backtracking line search (see Nocedal &amp; 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}.
3139
*/
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) {
3442
double stepSize = initialStepSize;
3543
int currFctEvalCount = lsr.getFctEvalCount();
3644
double[] x = lsr.getNextPoint();
@@ -79,6 +87,12 @@ public static void doLineSearch(Function function,
7987
/**
8088
* Conducts a constrained line search (see section 3.2 in the paper "Scalable Training
8189
* 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}.
8296
*/
8397
public static void doConstrainedLineSearch(Function function,
8498
double[] direction, LineSearchResult lsr, double l1Cost, double initialStepSize) {
@@ -146,7 +160,8 @@ public static void doConstrainedLineSearch(Function function,
146160
// ------------------------------------------------------------------------------------- //
147161

148162
/**
149-
* Represents a LineSearch result.
163+
* Represents a {@link LineSearch} result encapsulating the relevant data
164+
* at a point in time during computation.
150165
*/
151166
public static class LineSearchResult {
152167

@@ -162,72 +177,46 @@ public static class LineSearchResult {
162177
private double[] signVector;
163178

164179
/**
165-
* Constructor
180+
* Initializes a {@link LineSearchResult} object with the specified parameters.
166181
*/
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)
176185
{
177186
setAll(stepSize, valueAtCurr, valueAtNext, gradAtCurr, gradAtNext,
178187
currPoint, nextPoint, fctEvalCount);
179188
}
180189

181190
/**
182-
* Constructor with sign vector
191+
* Initializes a {@link LineSearchResult} object with the specified parameters.
183192
*/
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)
195197
{
196198
setAll(stepSize, valueAtCurr, valueAtNext, gradAtCurr, gradAtNext,
197199
pseudoGradAtNext, currPoint, nextPoint, signVector, fctEvalCount);
198200
}
199201

200202
/**
201-
* Update line search elements
203+
* Updates line search elements.
202204
*/
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)
212208
{
213209
setAll(stepSize, valueAtCurr, valueAtNext, gradAtCurr, gradAtNext,
214210
null, currPoint, nextPoint, null, fctEvalCount);
215211
}
216212

217213
/**
218-
* Update line search elements
214+
* Updates line search elements.
219215
*/
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)
231220
{
232221
this.stepSize = stepSize;
233222
this.valueAtCurr = valueAtCurr;
@@ -326,35 +315,50 @@ public void setFctEvalCount(int fctEvalCount) {
326315
}
327316

328317
/**
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.
330325
*/
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) {
336328
return getInitialObject(valueAtX, gradAtX, null, x, null, 0);
337329
}
338330

339331
/**
340332
* 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.
341340
*/
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) {
348343
return getInitialObject(valueAtX, gradAtX, pseudoGradAtX, x, new double[x.length], 0);
349344
}
350345

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) {
358362
return new LineSearchResult(0.0, 0.0, valueAtX, new double[x.length], gradAtX,
359363
pseudoGradAtX, new double[x.length], x, signX, fctEvalCount);
360364
}

opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihood.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import opennlp.tools.ml.model.OnePassRealValueDataIndexer;
2525

2626
/**
27-
* Evaluate negative log-likelihood and its gradient from {@link DataIndexer}.
27+
* Evaluates negative log-likelihood and its gradient from {@link DataIndexer}.
28+
*
29+
* @see Function
2830
*/
2931
public class NegLogLikelihood implements Function {
3032

opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ParallelNegLogLikelihood.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
import opennlp.tools.ml.model.DataIndexer;
3434

3535
/**
36-
* Evaluate negative log-likelihood and its gradient in parallel
36+
* Evaluates {@link NegLogLikelihood negative log-likelihood} and
37+
* its gradient in parallel.
38+
*
39+
* @see Function
3740
*/
3841
public class ParallelNegLogLikelihood extends NegLogLikelihood {
3942

@@ -67,7 +70,7 @@ public ParallelNegLogLikelihood(DataIndexer indexer, int threads) {
6770
}
6871

6972
/**
70-
* Computes the negative log-likelihood.
73+
* Computes the function value for {@code x}.
7174
*
7275
* @param x The input.
7376
* @return Returns the computed negative log-likelihood.
@@ -92,7 +95,7 @@ public double valueAt(double[] x) {
9295
}
9396

9497
/**
95-
* Computes the gradient.
98+
* Computes the gradient for {@code x}.
9699
*
97100
* @param x The input.
98101
* @return Returns the computed gradient.

0 commit comments

Comments
 (0)