QNTrainer { public QNModel trainModel(DataIndexer indexer) { LogLikelihoodFunction objectiveFunction = generateFunction(indexer); this.dimension = objectiveFunction.getDomainDimension(); this.updateInfo = new QNInfo(this.m, this.dimension); double[] initialPoint = objectiveFunction.getInitialPoint(); double initialValue = objectiveFunction.valueAt(initialPoint); double[] initialGrad = objectiveFunction.gradientAt(initialPoint); LineSearchResult lsr = LineSearchResult.getInitialObject(initialValue, initialGrad, initialPoint, 0); int z = 0; while (true) { if (verbose) { System.out.print(z++); } double[] direction = null; direction = computeDirection(objectiveFunction, lsr); lsr = LineSearch.doLineSearch(objectiveFunction, direction, lsr, verbose); updateInfo.updateInfo(lsr); if (isConverged(lsr)) break; } return new QNModel(objectiveFunction, lsr.getNextPoint()); } QNTrainer();  QNTrainer(boolean verbose);  QNTrainer(int m);  QNTrainer(int m, boolean verbose);  QNTrainer(int m, int maxFctEval, boolean verbose); QNModel trainModel(DataIndexer indexer); static final int DEFAULT_M; static final int MAX_FCT_EVAL; static final int DEFAULT_MAX_FCT_EVAL; }
@Test public void testTrainModelReturnsAQNModel() throws Exception { RealValueFileEventStream rvfes1 = new RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt"); DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1); QNModel trainedModel = new QNTrainer(false).trainModel(testDataIndexer); assertNotNull(trainedModel); }
@Test public void testInTinyDevSet() throws Exception { RealValueFileEventStream rvfes1 = new RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt"); DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1); QNModel trainedModel = new QNTrainer(15, true).trainModel(testDataIndexer); String[] features2Classify = new String[] {"feature2","feature3", "feature3", "feature3","feature3", "feature3", "feature3","feature3", "feature3", "feature3","feature3", "feature3"}; double[] eval = trainedModel.eval(features2Classify); assertNotNull(eval); }
@Test public void testInBigDevSet() throws IOException { QNModel trainedModel = new QNTrainer(10, 1000, true).trainModel(new TwoPassDataIndexer(createTrainingStream())); testModel(trainedModel); }
@Test public void testModel() throws IOException { RealValueFileEventStream rvfes1 = new RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt"); DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1); QNModel trainedModel = new QNTrainer(15, true).trainModel(testDataIndexer); assertTrue(trainedModel.equals(trainedModel)); assertFalse(trainedModel.equals(null)); }
@Test public void testSerdeModel() throws IOException { RealValueFileEventStream rvfes1 = new RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt"); DataIndexer testDataIndexer = new OnePassRealValueDataIndexer(rvfes1,1); QNModel trainedModel = new QNTrainer(5, 700, true).trainModel(testDataIndexer); ByteArrayOutputStream modelBytes = new ByteArrayOutputStream(); GenericModelWriter modelWriter = new GenericModelWriter(trainedModel, new DataOutputStream(modelBytes)); modelWriter.persist(); modelWriter.close(); GenericModelReader modelReader = new GenericModelReader(new BinaryFileDataReader( new ByteArrayInputStream(modelBytes.toByteArray()))); AbstractModel readModel = modelReader.getModel(); QNModel deserModel = (QNModel) readModel; assertTrue(trainedModel.equals(deserModel)); 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); assertEquals(eval01.length, eval02.length); for (int i = 0; i < eval01.length; i++) { assertEquals(eval01[i], eval02[i], 0.00000001); } }