public final double[] eval(String[] context, double[] outsums) { return eval(context, null, outsums); }
/** * Train a model using the GIS algorithm, assuming 100 iterations and no * cutoff. * * @param eventStream * The EventStream holding the data on which this model will be * trained. * @return The newly trained model, which can be used immediately or saved to * disk using an opennlp.tools.ml.maxent.io.GISModelWriter object. */ public GISModel trainModel(ObjectStream<Event> eventStream) throws IOException { return trainModel(eventStream, 100, 0); }
@Override public MaxentModel doTrain(DataIndexer indexer) throws IOException { int iterations = getIterations(); int threads = trainingParameters.getIntParameter(TrainingParameters.THREADS_PARAM, 1); AbstractModel model = trainModel(iterations, indexer, threads); return model; }
readInt(); readDouble(); String[] outcomeLabels = getOutcomes(); int[][] outcomePatterns = getOutcomePatterns(); String[] predLabels = getPredicates(); Context[] params = getParameters(outcomePatterns); return new GISModel(params, predLabels, outcomeLabels);
@Test public void testMaxentOnPrepAttachData() throws IOException { testDataIndexer.index(PrepAttachDataUtil.createTrainingStream()); // this shows why the GISTrainer should be a AbstractEventTrainer. // TODO: make sure that the trainingParameter cutoff and the // cutoff value passed here are equal. AbstractModel model = new GISTrainer(true).trainModel(100, testDataIndexer, new UniformPrior(), 1); PrepAttachDataUtil.testModel(model, 0.7997028967566229); }
@Test public void testGISTrainSignature4() throws IOException { try (ObjectStream<Event> eventStream = createEventStream()) { TrainingParameters params = ModelUtil.createDefaultTrainingParameters(); params.put(AbstractTrainer.ITERATIONS_PARAM, 10); params.put(AbstractTrainer.CUTOFF_PARAM, 1); GISTrainer trainer = (GISTrainer) TrainerFactory.getEventTrainer(params, null); trainer.setGaussianSigma(0.01); Assert.assertNotNull(trainer.trainModel(eventStream)); } }
public Event read() throws IOException { String eventString = ds.read(); if (eventString != null) { return createEvent(eventString); } return null; }
@Test public void testGISTrainSignature1() throws IOException { try (ObjectStream<Event> eventStream = createEventStream()) { TrainingParameters params = ModelUtil.createDefaultTrainingParameters(); params.put(AbstractTrainer.CUTOFF_PARAM, 1); EventTrainer trainer = TrainerFactory.getEventTrainer(params, null); Assert.assertNotNull(trainer.train(eventStream)); } }
public FootballEventStream() throws IOException { textStream = new PlainTextByLineStream( new URLInputStreamFactory(this.getClass().getResource("/opennlp/tools/ml/maxent/football.dat")), StandardCharsets.US_ASCII ); }
@Test public void testMaxentOnPrepAttachData2Threads() throws IOException { testDataIndexer.index(PrepAttachDataUtil.createTrainingStream()); AbstractModel model = new GISTrainer(true).trainModel(100, testDataIndexer, new UniformPrior(), 2); PrepAttachDataUtil.testModel(model, 0.7997028967566229); }
/** * Train a model using the GIS algorithm. * * @param iterations The number of GIS iterations to perform. * @param di The data indexer used to compress events in memory. * @param threads * @return The newly trained model, which can be used immediately or saved * to disk using an opennlp.tools.ml.maxent.io.GISModelWriter object. */ public GISModel trainModel(int iterations, DataIndexer di, int threads) { return trainModel(iterations, di, new UniformPrior(), threads); }
/** * Use this model to evaluate a context and return an array of the likelihood * of each outcome given the specified context and the specified parameters. * * @param context * The integer values of the predicates which have been observed at * the present decision point. * @param prior * The prior distribution for the specified context. * @param model * The set of parametes used in this computation. * @return The normalized probabilities for the outcomes given the context. * The indexes of the double[] are the outcome ids, and the actual * string representation of the outcomes can be obtained from the * method getOutcome(int i). */ public static double[] eval(int[] context, double[] prior, EvalParameters model) { return eval(context, null, prior, model); }
@Test public void testGISTrainSignature2() throws IOException { try (ObjectStream<Event> eventStream = createEventStream()) { TrainingParameters params = ModelUtil.createDefaultTrainingParameters(); params.put(AbstractTrainer.CUTOFF_PARAM, 1); params.put("smoothing", true); EventTrainer trainer = TrainerFactory.getEventTrainer(params, null); Assert.assertNotNull(trainer.train(eventStream)); } }
/** * Train a model using the GIS algorithm. * * @param iterations The number of GIS iterations to perform. * @param di The data indexer used to compress events in memory. * @return The newly trained model, which can be used immediately or saved * to disk using an opennlp.tools.ml.maxent.io.GISModelWriter object. */ public GISModel trainModel(int iterations, DataIndexer di) { return trainModel(iterations, di, new UniformPrior(), 1); }
public final double[] eval(String[] context, float[] values) { return (eval(context, values, new double[evalParams.getNumOutcomes()])); }
@Test public void testGISTrainSignature3() throws IOException { try (ObjectStream<Event> eventStream = createEventStream()) { TrainingParameters params = ModelUtil.createDefaultTrainingParameters(); params.put(AbstractTrainer.ITERATIONS_PARAM, 10); params.put(AbstractTrainer.CUTOFF_PARAM, 1); EventTrainer trainer = TrainerFactory.getEventTrainer(params, null); Assert.assertNotNull(trainer.train(eventStream)); } }
/** * Use this model to evaluate a context and return an array of the likelihood * of each outcome given that context. * * @param context * The names of the predicates which have been observed at the * present decision point. * @return The normalized probabilities for the outcomes given the context. * The indexes of the double[] are the outcome ids, and the actual * string representation of the outcomes can be obtained from the * method getOutcome(int i). */ public final double[] eval(String[] context) { return (eval(context, new double[evalParams.getNumOutcomes()])); }
@Test public void testGISTrainSignature5() throws IOException { try (ObjectStream<Event> eventStream = createEventStream()) { TrainingParameters params = ModelUtil.createDefaultTrainingParameters(); params.put(AbstractTrainer.ITERATIONS_PARAM, 10); params.put(AbstractTrainer.CUTOFF_PARAM, 1); params.put("smoothing", false); params.put(AbstractTrainer.VERBOSE_PARAM, false); EventTrainer trainer = TrainerFactory.getEventTrainer(params, null); Assert.assertNotNull(trainer.train(eventStream)); } }
/** * Use this model to evaluate a context and return an array of the likelihood * of each outcome given the specified context and the specified parameters. * * @param context * The integer values of the predicates which have been observed at * the present decision point. * @param values * The values for each of the parameters. * @param prior * The prior distribution for the specified context. * @param model * The set of parametes used in this computation. * @return The normalized probabilities for the outcomes given the context. * The indexes of the double[] are the outcome ids, and the actual * string representation of the outcomes can be obtained from the * method getOutcome(int i). */ static double[] eval(int[] context, float[] values, double[] prior, EvalParameters model) { Context[] scontexts = new Context[context.length]; for (int i = 0; i < context.length; i++) { scontexts[i] = model.getParams()[context[i]]; } return GISModel.eval(scontexts, values, prior, model); }
/** * Use this model to evaluate a context and return an array of the likelihood * of each outcome given that context. * * @param context * The names of the predicates which have been observed at the * present decision point. * @param outsums * This is where the distribution is stored. * @return The normalized probabilities for the outcomes given the context. * The indexes of the double[] are the outcome ids, and the actual * string representation of the outcomes can be obtained from the * method getOutcome(int i). */ public final double[] eval(String[] context, float[] values, double[] outsums) { Context[] scontexts = new Context[context.length]; for (int i = 0; i < context.length; i++) { scontexts[i] = pmap.get(context[i]); } prior.logPrior(outsums, scontexts, values); return GISModel.eval(scontexts, values, outsums, evalParams); }