Author: joern
Date: Fri Apr  4 09:36:27 2014
New Revision: 1584580

URL: http://svn.apache.org/r1584580
Log:
OPENNLP-569 Fixing remaining issues with the unit tests. Thanks to Vinh Khuc 
for providing a patch.

Added:
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihoodFunctionTest.java
Modified:
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNPrepAttachTest.java
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNTrainerTest.java
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction.java
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction02.java

Modified: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java?rev=1584580&r1=1584579&r2=1584580&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java
 Fri Apr  4 09:36:27 2014
@@ -27,7 +27,7 @@ import org.junit.Test;
 public class LineSearchTest {
   public static final double TOLERANCE = 0.01;
 
-  // @Test
+  @Test
   public void testLineSearchDeterminesSaneStepLength01() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction();
     // given
@@ -44,7 +44,7 @@ public class LineSearchTest {
     assertTrue(succCond);
   }
 
-  // @Test
+  @Test
   public void testLineSearchDeterminesSaneStepLength02() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction02();
     // given
@@ -61,7 +61,7 @@ public class LineSearchTest {
     assertTrue(succCond);
   }
 
-  //@Test
+  @Test
   public void testLineSearchFailsWithWrongDirection01() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction();
     // given
@@ -79,7 +79,7 @@ public class LineSearchTest {
     assertEquals(0.0, stepSize, TOLERANCE);
   }
 
-  // @Test
+  @Test
   public void testLineSearchFailsWithWrongDirection02() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction02();
     // given
@@ -97,7 +97,7 @@ public class LineSearchTest {
     assertEquals(0.0, stepSize, TOLERANCE);
   }
 
-  // @Test
+  @Test
   public void testLineSearchFailsWithWrongDirection03() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction();
     // given
@@ -115,7 +115,7 @@ public class LineSearchTest {
     assertEquals(0.0, stepSize, TOLERANCE);
   }
 
-  // @Test
+  @Test
   public void testLineSearchFailsWithWrongDirection04() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction02();
     // given
@@ -133,8 +133,8 @@ public class LineSearchTest {
     assertEquals(0.0, stepSize, TOLERANCE);
   }
 
-  // @Test
-  public void testLineSearchFailsAtMaxima01() {
+  @Test
+  public void testLineSearchFailsAtMinimum01() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction02();
     // given
     double[] testX = new double[] { 0 };
@@ -151,8 +151,8 @@ public class LineSearchTest {
     assertEquals(0.0, stepSize, TOLERANCE);
   }
 
-  // @Test
-  public void testLineSearchFailsAtMaxima02() {
+  @Test
+  public void testLineSearchFailsAtMinimum02() {
     DifferentiableFunction objectiveFunction = new QuadraticFunction02();
     // given
     double[] testX = new double[] { 0 };

Added: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihoodFunctionTest.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihoodFunctionTest.java?rev=1584580&view=auto
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihoodFunctionTest.java
 (added)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihoodFunctionTest.java
 Fri Apr  4 09:36:27 2014
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package opennlp.tools.ml.maxent.quasinewton;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import opennlp.tools.ml.model.DataIndexer;
+import opennlp.tools.ml.model.OnePassRealValueDataIndexer;
+import opennlp.tools.ml.model.RealValueFileEventStream;
+
+import org.junit.Test;
+
+public class NegLogLikelihoodFunctionTest {
+  public final double TOLERANCE01 = 1.0E-06;
+  public final double TOLERANCE02 = 1.0E-10;
+
+  @Test
+  public void testDomainDimensionSanity() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");  
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    int correctDomainDimension = testDataIndexer.getPredLabels().length 
+        * testDataIndexer.getOutcomeLabels().length;
+    // then
+    assertEquals(correctDomainDimension, objectFunction.getDomainDimension());
+  }
+
+  @Test
+  public void testInitialSanity() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");  
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    double[] initial = objectFunction.getInitialPoint();
+    // then
+    for (int i = 0; i < initial.length; i++) {
+      assertEquals(0.0, initial[i], TOLERANCE01);
+    }
+  }
+
+  @Test
+  public void testGradientSanity() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");  
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    double[] initial = objectFunction.getInitialPoint();
+    /** valueAt() must be always called before gradientAt() */
+    objectFunction.valueAt(initial); 
+    double[] gradientAtInitial = objectFunction.gradientAt(initial);
+    // then
+    assertNotNull(gradientAtInitial);
+  }
+
+  @Test
+  public void testValueAtInitialPoint() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    double value = objectFunction.valueAt(objectFunction.getInitialPoint());
+    double expectedValue = 13.86294361;
+    // then
+    assertEquals(expectedValue, value, TOLERANCE01);
+  }
+
+  @Test
+  public void testValueAtNonInitialPoint01() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    double[] nonInitialPoint = new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
+    double value = objectFunction.valueAt(nonInitialPoint);
+    double expectedValue = 23.862943611198894;
+    // then
+    assertEquals(expectedValue, value, TOLERANCE01);
+  }
+
+  @Test
+  public void testValueAtNonInitialPoint02() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    double[] nonInitialPoint = new double[] { 3, 2, 3, 2, 3, 2, 3, 2, 3, 2 };
+    double value = 
objectFunction.valueAt(dealignDoubleArrayForTestData(nonInitialPoint,
+                       testDataIndexer.getPredLabels(), 
+                       testDataIndexer.getOutcomeLabels()));
+    double expectedValue = 118.16321972109903;
+    // then
+    assertEquals(expectedValue, value, TOLERANCE02);
+  }
+
+  @Test 
+  public void testGradientAtInitialPoint() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    /** valueAt() must be always called before gradientAt() */
+    objectFunction.valueAt(objectFunction.getInitialPoint()); 
+    double[] gradientAtInitialPoint = 
objectFunction.gradientAt(objectFunction.getInitialPoint());
+    double[] expectedGradient = new double[] { -9.0, -14.0, -17.0, 20.0, 8.5, 
9.0, 14.0, 17.0, -20.0, -8.5 };
+    // then
+    assertTrue(compareDoubleArray(expectedGradient, gradientAtInitialPoint, 
+        testDataIndexer, TOLERANCE01));
+  }
+
+  @Test
+  public void testGradientAtNonInitialPoint() throws IOException {
+    // given
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt", 
"UTF-8");
+    DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
+    NegLogLikelihoodFunction objectFunction = new 
NegLogLikelihoodFunction(testDataIndexer);
+    // when
+    double[] nonInitialPoint = new double[] { 0.2, 0.5, 0.2, 0.5, 0.2, 0.5, 
0.2, 0.5, 0.2, 0.5 };
+    /** valueAt() must be always called before gradientAt() */
+    objectFunction.valueAt(nonInitialPoint); 
+    double[] gradientAtNonInitialPoint = 
+               
objectFunction.gradientAt(dealignDoubleArrayForTestData(nonInitialPoint,
+                               testDataIndexer.getPredLabels(), 
+                               testDataIndexer.getOutcomeLabels()));
+    double[] expectedGradient = 
+            new double[] { -8.742040938155275, -10.81798632847702, 
+                           30.311389857093182, 1.5513000872985572, 
+                            2.0513491106450754, 10.142040938155278, 
+                           12.217986328477027, -28.911389857093162, 
+                          -0.15130008729855715, -0.6513491106450751 };   
+    // then
+    assertTrue(compareDoubleArray(expectedGradient, gradientAtNonInitialPoint, 
+        testDataIndexer, TOLERANCE01));
+  }
+  
+  private double[] alignDoubleArrayForTestData(double[] expected, 
+      String[] predLabels, String[] outcomeLabels) {
+       double[] aligned = new double[predLabels.length * outcomeLabels.length];
+       
+       String[] sortedPredLabels = predLabels.clone();
+       String[] sortedOutcomeLabels =  outcomeLabels.clone();
+       Arrays.sort(sortedPredLabels);
+       Arrays.sort(sortedOutcomeLabels);
+       
+       Map<String, Integer> invertedPredIndex = new HashMap<String, Integer>();
+       Map<String, Integer> invertedOutcomeIndex = new HashMap<String, 
Integer>();
+    for (int i = 0; i < predLabels.length; i++) {
+      invertedPredIndex.put(predLabels[i], i);
+    }
+    for (int i = 0; i < outcomeLabels.length; i++) {
+      invertedOutcomeIndex.put(outcomeLabels[i], i);
+    }
+       
+    for (int i = 0; i < sortedOutcomeLabels.length; i++) {
+      for (int j = 0; j < sortedPredLabels.length; j++) {
+        aligned[i * sortedPredLabels.length + j] = 
expected[invertedOutcomeIndex
+            .get(sortedOutcomeLabels[i])
+            * sortedPredLabels.length
+            + invertedPredIndex.get(sortedPredLabels[j])];
+      }
+    }
+    return aligned;
+  }
+  
+  private double[] dealignDoubleArrayForTestData(double[] expected,
+      String[] predLabels, String[] outcomeLabels) {
+    double[] dealigned = new double[predLabels.length * outcomeLabels.length];
+
+    String[] sortedPredLabels = predLabels.clone();
+    String[] sortedOutcomeLabels = outcomeLabels.clone();
+    Arrays.sort(sortedPredLabels);
+    Arrays.sort(sortedOutcomeLabels);
+
+    Map<String, Integer> invertedPredIndex = new HashMap<String, Integer>();
+    Map<String, Integer> invertedOutcomeIndex = new HashMap<String, Integer>();
+    for (int i = 0; i < predLabels.length; i++) {
+      invertedPredIndex.put(predLabels[i], i);
+    }
+    for (int i = 0; i < outcomeLabels.length; i++) {
+      invertedOutcomeIndex.put(outcomeLabels[i], i);
+    }
+
+    for (int i = 0; i < sortedOutcomeLabels.length; i++) {
+      for (int j = 0; j < sortedPredLabels.length; j++) {
+        dealigned[invertedOutcomeIndex.get(sortedOutcomeLabels[i])
+            * sortedPredLabels.length
+            + invertedPredIndex.get(sortedPredLabels[j])] = expected[i
+            * sortedPredLabels.length + j];
+      }
+    }
+
+    return dealigned;
+  }
+  
+  private boolean compareDoubleArray(double[] expected, double[] actual, 
+      DataIndexer indexer, double tolerance) {
+    double[] alignedActual = alignDoubleArrayForTestData(
+        actual, indexer.getPredLabels(), indexer.getOutcomeLabels());
+         
+    if (expected.length != alignedActual.length) {
+      return false;
+    }
+    
+    for (int i = 0; i < alignedActual.length; i++) {
+      if (Math.abs(alignedActual[i] - expected[i]) > tolerance) {
+        return false;
+      }
+    }
+    return true;
+  }
+}

Modified: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNPrepAttachTest.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNPrepAttachTest.java?rev=1584580&r1=1584579&r2=1584580&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNPrepAttachTest.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNPrepAttachTest.java
 Fri Apr  4 09:36:27 2014
@@ -52,7 +52,8 @@ public class QNPrepAttachTest {
     trainParams.put(AbstractEventTrainer.DATA_INDEXER_PARAM,
         AbstractEventTrainer.DATA_INDEXER_TWO_PASS_VALUE);
     trainParams.put(AbstractTrainer.CUTOFF_PARAM, Integer.toString(1));
-    trainParams.put(QNTrainer.L2COST_PARAM, Double.toString(2.0)); // L2-cost 
higher than the default
+    // use L2-cost higher than the default
+    trainParams.put(QNTrainer.L2COST_PARAM, Double.toString(2.0));
     
     MaxentModel model = TrainerFactory.getEventTrainer(trainParams, null)
                                       .train(createTrainingStream());

Modified: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNTrainerTest.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNTrainerTest.java?rev=1584580&r1=1584579&r2=1584580&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNTrainerTest.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNTrainerTest.java
 Fri Apr  4 09:36:27 2014
@@ -16,44 +16,35 @@
  */
 package opennlp.tools.ml.maxent.quasinewton;
 
-import static opennlp.tools.ml.PrepAttachDataUtil.createTrainingStream;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.List;
 
 import opennlp.tools.ml.model.AbstractModel;
 import opennlp.tools.ml.model.BinaryFileDataReader;
 import opennlp.tools.ml.model.DataIndexer;
-import opennlp.tools.ml.model.Event;
 import opennlp.tools.ml.model.GenericModelReader;
 import opennlp.tools.ml.model.GenericModelWriter;
-import opennlp.tools.ml.model.MaxentModel;
 import opennlp.tools.ml.model.OnePassRealValueDataIndexer;
 import opennlp.tools.ml.model.RealValueFileEventStream;
-import opennlp.tools.ml.model.TwoPassDataIndexer;
-import opennlp.tools.ml.perceptron.PerceptronPrepAttachTest;
 
 import org.junit.Test;
 
 public class QNTrainerTest {
   
-  private static int ITERATIONS = 10;
+  private static int ITERATIONS = 50;
   
   @Test
   public void testTrainModelReturnsAQNModel() throws Exception {
     // given
-    RealValueFileEventStream rvfes1 = new 
RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
     DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
     // when
     QNModel trainedModel = new QNTrainer(false).trainModel(ITERATIONS, 
testDataIndexer);
@@ -64,30 +55,30 @@ public class QNTrainerTest {
   @Test
   public void testInTinyDevSet() throws Exception {
     // given
-    RealValueFileEventStream rvfes1 = new 
RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
+    RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+        
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
     DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1);
     // when
     QNModel trainedModel = new QNTrainer(15, true).trainModel(ITERATIONS, 
testDataIndexer);
-    String[] features2Classify = new String[] {"feature2","feature3", 
"feature3", "feature3","feature3", "feature3", "feature3","feature3", 
"feature3", "feature3","feature3", "feature3"};
+    String[] features2Classify = new String[] {
+        "feature2","feature3", "feature3", 
+        "feature3","feature3", "feature3", 
+        "feature3","feature3", "feature3", 
+        "feature3","feature3", "feature3"};
     double[] eval = trainedModel.eval(features2Classify);
     // then
     assertNotNull(eval);
   }
-
-  @Test
-  public void testInBigDevSet() throws IOException {
-    QNModel trainedModel = new QNTrainer(10, 1000, 
true).trainModel(ITERATIONS, new TwoPassDataIndexer(createTrainingStream()));
-    // then
-    testModel(trainedModel);
-  }
   
   @Test
   public void testModel() throws IOException {
            // given
-           RealValueFileEventStream rvfes1 = new 
RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
+           RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+               
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
            DataIndexer testDataIndexer = new 
OnePassRealValueDataIndexer(rvfes1,1);
            // when
-           QNModel trainedModel = new QNTrainer(15, 
true).trainModel(ITERATIONS, testDataIndexer);
+           QNModel trainedModel = new QNTrainer(15, true).trainModel(
+               ITERATIONS, testDataIndexer);
            
            assertTrue(trainedModel.equals(trainedModel));  
            assertFalse(trainedModel.equals(null));
@@ -96,14 +87,15 @@ public class QNTrainerTest {
   @Test
   public void testSerdeModel() throws IOException {
            // given
-           RealValueFileEventStream rvfes1 = new 
RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
+           RealValueFileEventStream rvfes1 = new RealValueFileEventStream(
+               
"src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
  
            DataIndexer testDataIndexer = new 
OnePassRealValueDataIndexer(rvfes1,1);
            // when
-          // QNModel trainedModel = new QNTrainer(5, 500, true).trainModel(new 
TwoPassDataIndexer(createTrainingStream()));
            QNModel trainedModel = new QNTrainer(5, 700, 
true).trainModel(ITERATIONS, testDataIndexer);
            
            ByteArrayOutputStream modelBytes = new ByteArrayOutputStream();
-           GenericModelWriter modelWriter = new 
GenericModelWriter(trainedModel, new DataOutputStream(modelBytes));
+           GenericModelWriter modelWriter = new 
GenericModelWriter(trainedModel, 
+               new DataOutputStream(modelBytes));
            modelWriter.persist();
            modelWriter.close();
            
@@ -114,7 +106,11 @@ public class QNTrainerTest {
            
            assertTrue(trainedModel.equals(deserModel)); 
            
-           String[] features2Classify = new String[] {"feature2","feature3", 
"feature3", "feature3","feature3", "feature3", "feature3","feature3", 
"feature3", "feature3","feature3", "feature3"};
+           String[] features2Classify = new String[] {
+               "feature2","feature3", "feature3", 
+               "feature3","feature3", "feature3", 
+               "feature3","feature3", "feature3", 
+               "feature3","feature3", "feature3"};
            double[] eval01 = trainedModel.eval(features2Classify);
            double[] eval02 = deserModel.eval(features2Classify);
            
@@ -123,51 +119,4 @@ public class QNTrainerTest {
                assertEquals(eval01[i], eval02[i], 0.00000001);
            }
   }
-
-  public static void testModel(MaxentModel model) throws IOException {
-    List<Event> devEvents = readPpaFile("devset");
-
-    int total = 0;
-    int correct = 0;
-    for (Event ev: devEvents) {
-      String targetLabel = ev.getOutcome();
-      double[] ocs = model.eval(ev.getContext());
-
-      int best = 0;
-      for (int i=1; i<ocs.length; i++)
-        if (ocs[i] > ocs[best])
-          best = i;
-      String predictedLabel = model.getOutcome(best);
-
-      if (targetLabel.equals(predictedLabel))
-        correct++;
-      total++;
-    }
-
-    double accuracy = correct/(double)total;
-    System.out.println("Accuracy on PPA devset: (" + correct + "/" + total + 
") " + accuracy);
-  }
-
-  private static List<Event> readPpaFile(String filename) throws IOException {
-
-    List<Event> events = new ArrayList<Event>();
-
-    InputStream in = 
PerceptronPrepAttachTest.class.getResourceAsStream("/data/ppa/" +
-      filename);
-
-    try {
-      BufferedReader reader = new BufferedReader(new InputStreamReader(in, 
"UTF-8"));
-      String line;
-      while ((line = reader.readLine()) != null) {
-        String[] items = line.split("\\s+");
-        String label = items[5];
-        String[] context = { "verb=" + items[1], "noun=" + items[2],
-          "prep=" + items[3], "prep_obj=" + items[4] };
-        events.add(new Event(label, context));
-      }
-    } finally {
-      in.close();
-    }
-    return events;
-  }
 }

Modified: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction.java?rev=1584580&r1=1584579&r2=1584580&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction.java
 Fri Apr  4 09:36:27 2014
@@ -17,17 +17,18 @@
 package opennlp.tools.ml.maxent.quasinewton;
 
 /**
- * sample function for unit tests of LineSearch
+ * Sample function for unit tests of LineSearch
  */
 public class QuadraticFunction implements DifferentiableFunction {
 
   public double valueAt(double[] x) {
-  // -(x-2)^2 + 4;
-    return (Math.pow(x[0] - 2.0, 2.0) * -1.0) + 4.0;
+    // (x-2)^2 + 4;
+    return Math.pow(x[0] - 2, 2) + 4;
   }
 
   public double[] gradientAt(double[] x) {
-    return new double[] {(-2.0 * x[0]) + 4.0};
+    // 2(x-2)
+    return new double[] {2 * (x[0]- 2)};
   }
 
   public int getDomainDimension() {

Modified: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction02.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction02.java?rev=1584580&r1=1584579&r2=1584580&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction02.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QuadraticFunction02.java
 Fri Apr  4 09:36:27 2014
@@ -17,17 +17,17 @@
 package opennlp.tools.ml.maxent.quasinewton;
 
 /**
- * sample function for unit tests of LineSearch
+ * Sample function for unit tests of LineSearch
  */
 public class QuadraticFunction02 implements DifferentiableFunction {
   public double valueAt(double[] x) {
-    // -x^2;
-    return Math.pow(x[0], 2) * -1;     
+    // x^2;
+    return Math.pow(x[0], 2);  
   }
 
   public double[] gradientAt(double[] x) {
-    // -2x
-    return new double[] {-2.0 * x[0]};
+    // 2x
+    return new double[] {2 * x[0]};
   }
 
   public int getDomainDimension() {


Reply via email to