This is an automated email from the ASF dual-hosted git repository.
koji pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/opennlp.git
The following commit(s) were added to refs/heads/master by this push:
new 376c0a5 OPENNLP-1195: use ArrayMath.argmax() rather than private
maxIndex() in PerceptronTrainer, NaiveBayesTrainer, AbstractModel and
GISTrainer (#315)
376c0a5 is described below
commit 376c0a5654493295b1d5595fe236509bdc3a41c5
Author: Koji Sekiguchi <[email protected]>
AuthorDate: Tue May 15 19:14:29 2018 +0900
OPENNLP-1195: use ArrayMath.argmax() rather than private maxIndex() in
PerceptronTrainer, NaiveBayesTrainer, AbstractModel and GISTrainer (#315)
---
.../src/main/java/opennlp/tools/ml/maxent/GISTrainer.java | 8 ++------
.../main/java/opennlp/tools/ml/model/AbstractModel.java | 7 +++----
.../opennlp/tools/ml/naivebayes/NaiveBayesTrainer.java | 11 ++---------
.../opennlp/tools/ml/perceptron/PerceptronTrainer.java | 14 +++-----------
4 files changed, 10 insertions(+), 30 deletions(-)
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISTrainer.java
b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISTrainer.java
index 1ec45d8..e8f6177 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISTrainer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISTrainer.java
@@ -28,6 +28,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import opennlp.tools.ml.AbstractEventTrainer;
+import opennlp.tools.ml.ArrayMath;
import opennlp.tools.ml.model.AbstractModel;
import opennlp.tools.ml.model.DataIndexer;
import opennlp.tools.ml.model.EvalParameters;
@@ -691,12 +692,7 @@ public class GISTrainer extends AbstractEventTrainer {
numEvents += numTimesEventsSeen[ei];
if (printMessages) {
- int max = 0;
- for (int oi = 1; oi < numOutcomes; oi++) {
- if (modelDistribution[oi] > modelDistribution[max]) {
- max = oi;
- }
- }
+ int max = ArrayMath.argmax(modelDistribution);
if (max == outcomeList[ei]) {
numCorrect += numTimesEventsSeen[ei];
}
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
index 4783a71..bbb0c91 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
@@ -23,6 +23,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
+import opennlp.tools.ml.ArrayMath;
+
public abstract class AbstractModel implements MaxentModel {
/** Mapping between predicates/contexts and an integer representing them. */
@@ -71,10 +73,7 @@ public abstract class AbstractModel implements MaxentModel {
* @return The name of the most likely outcome.
*/
public final String getBestOutcome(double[] ocs) {
- int best = 0;
- for (int i = 1; i < ocs.length; i++)
- if (ocs[i] > ocs[best]) best = i;
- return outcomeNames[best];
+ return outcomeNames[ArrayMath.argmax(ocs)];
}
public ModelType getModelType() {
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesTrainer.java
b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesTrainer.java
index 63c3244..c56d9cc 100644
---
a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesTrainer.java
+++
b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesTrainer.java
@@ -20,6 +20,7 @@ package opennlp.tools.ml.naivebayes;
import java.io.IOException;
import opennlp.tools.ml.AbstractEventTrainer;
+import opennlp.tools.ml.ArrayMath;
import opennlp.tools.ml.model.AbstractModel;
import opennlp.tools.ml.model.DataIndexer;
import opennlp.tools.ml.model.EvalParameters;
@@ -189,7 +190,7 @@ public class NaiveBayesTrainer extends AbstractEventTrainer
{
else
NaiveBayesModel.eval(contexts[ei], null, modelDistribution,
evalParams, false);
- int max = maxIndex(modelDistribution);
+ int max = ArrayMath.argmax(modelDistribution);
if (max == outcomeList[ei])
numCorrect++;
}
@@ -199,12 +200,4 @@ public class NaiveBayesTrainer extends
AbstractEventTrainer {
return trainingAccuracy;
}
-
- private int maxIndex(double[] values) {
- int max = 0;
- for (int i = 1; i < values.length; i++)
- if (values[i] > values[max])
- max = i;
- return max;
- }
}
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
index b73eaca..346d24b 100644
---
a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
+++
b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
@@ -20,6 +20,7 @@ package opennlp.tools.ml.perceptron;
import java.io.IOException;
import opennlp.tools.ml.AbstractEventTrainer;
+import opennlp.tools.ml.ArrayMath;
import opennlp.tools.ml.model.AbstractModel;
import opennlp.tools.ml.model.DataIndexer;
import opennlp.tools.ml.model.EvalParameters;
@@ -297,7 +298,7 @@ public class PerceptronTrainer extends AbstractEventTrainer
{
else
PerceptronModel.eval(contexts[ei], null, modelDistribution,
evalParams, false);
- int maxOutcome = maxIndex(modelDistribution);
+ int maxOutcome = ArrayMath.argmax(modelDistribution);
// If the predicted outcome is different from the target
// outcome, do the standard update: boost the parameters
@@ -388,7 +389,7 @@ public class PerceptronTrainer extends AbstractEventTrainer
{
else
PerceptronModel.eval(contexts[ei], null, modelDistribution,
evalParams, false);
- int max = maxIndex(modelDistribution);
+ int max = ArrayMath.argmax(modelDistribution);
if (max == outcomeList[ei])
numCorrect++;
}
@@ -398,15 +399,6 @@ public class PerceptronTrainer extends
AbstractEventTrainer {
return trainingAccuracy;
}
-
- private int maxIndex(double[] values) {
- int max = 0;
- for (int i = 1; i < values.length; i++)
- if (values[i] > values[max])
- max = i;
- return max;
- }
-
private void displayIteration(int i) {
if (i > 10 && (i % 10) != 0)
return;
--
To stop receiving notification emails like this one, please contact
[email protected].