public double[] eval(String[] context, double[] probs) { return eval(context,null,probs); }
public AbstractModel trainModel(int iterations, DataIndexer di, int cutoff) { return trainModel(iterations,di,cutoff,true); }
public AbstractModel doTrain(DataIndexer indexer) throws IOException { int iterations = getIterations(); int cutoff = getCutoff(); AbstractModel model; boolean useAverage = trainingParameters.getBooleanParameter("UseAverage", true); boolean useSkippedAveraging = trainingParameters.getBooleanParameter("UseSkippedAveraging", false); // overwrite otherwise it might not work if (useSkippedAveraging) useAverage = true; double stepSizeDecrease = trainingParameters.getDoubleParameter("StepSizeDecrease", 0); double tolerance = trainingParameters.getDoubleParameter("Tolerance", PerceptronTrainer.TOLERANCE_DEFAULT); this.setSkippedAveraging(useSkippedAveraging); if (stepSizeDecrease > 0) this.setStepSizeDecrease(stepSizeDecrease); this.setTolerance(tolerance); model = this.trainModel(iterations, indexer, cutoff, useAverage); return model; }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>Perceptron (model type identifier) * <br>1. # of parameters (int) * <br>2. # of outcomes (int) * <br> * list of outcome names (String) * <br>3. # of different types of outcome patterns (int) * <br> * list of (int int[]) * <br> [# of predicates for which outcome pattern is true] [outcome pattern] * <br>4. # of predicates (int) * <br> * list of predicate names (String) * * <p>If you are creating a reader for a format which won't work with this * (perhaps a database or xml file), override this method and ignore the * other methods provided in this abstract class. * * @return The PerceptronModel stored in the format and location specified to * this PerceptronModelReader (usually via its the constructor). */ public AbstractModel constructModel() throws IOException { String[] outcomeLabels = getOutcomes(); int[][] outcomePatterns = getOutcomePatterns(); String[] predLabels = getPredicates(); Context[] params = getParameters(outcomePatterns); return new PerceptronModel(params, predLabels, outcomeLabels); }
private void findParameters(int iterations) throws IOException { display("Performing " + iterations + " iterations.\n"); for (int i = 1; i <= iterations; i++) { if (i < 10) display(" " + i + ": "); else if (i < 100) display(" " + i + ": "); else display(i + ": "); nextIteration(i); } if (useAverage) { trainingStats(averageParams); } else { trainingStats(params); } }
public AbstractModel doTrain(SequenceStream events) throws IOException { int iterations = getIterations(); int cutoff = getCutoff(); boolean useAverage = trainingParameters.getBooleanParameter("UseAverage", true); return trainModel(iterations, events, cutoff, useAverage); }
public AbstractModel trainModel(int iterations, DataIndexer di, int cutoff, boolean useAverage) { display("Incorporating indexed data for training... \n"); contexts = di.getContexts(); values = di.getValues(); numTimesEventsSeen = di.getNumTimesEventsSeen(); numEvents = di.getNumEvents(); numUniqueEvents = contexts.length; outcomeLabels = di.getOutcomeLabels(); outcomeList = di.getOutcomeList(); predLabels = di.getPredLabels(); numPreds = predLabels.length; numOutcomes = outcomeLabels.length; display("done.\n"); display("\tNumber of Event Tokens: " + numUniqueEvents + "\n"); display("\t Number of Outcomes: " + numOutcomes + "\n"); display("\t Number of Predicates: " + numPreds + "\n"); display("Computing model parameters...\n"); MutableContext[] finalParameters = findParameters(iterations, useAverage); display("...done.\n"); /* Create and return the model *************/ return new PerceptronModel(finalParameters, predLabels, outcomeLabels); }
private double trainingStats(EvalParameters evalParams) { int numCorrect = 0; for (int ei = 0; ei < numUniqueEvents; ei++) { for (int ni = 0; ni < this.numTimesEventsSeen[ei]; ni++) { double[] modelDistribution = new double[numOutcomes]; if (values != null) PerceptronModel.eval(contexts[ei], values[ei], modelDistribution, evalParams,false); else PerceptronModel.eval(contexts[ei], null, modelDistribution, evalParams, false); int max = ArrayMath.argmax(modelDistribution); if (max == outcomeList[ei]) numCorrect++; } } double trainingAccuracy = (double) numCorrect / numEvents; display("Stats: (" + numCorrect + "/" + numEvents + ") " + trainingAccuracy + "\n"); return trainingAccuracy; }
private void trainingStats(MutableContext[] params) throws IOException { int numCorrect = 0; int oei = 0; sequenceStream.reset(); Sequence sequence; while ((sequence = sequenceStream.read()) != null) { Event[] taggerEvents = sequenceStream.updateContext(sequence, new PerceptronModel(params,predLabels,outcomeLabels)); for (int ei = 0; ei < taggerEvents.length; ei++, oei++) { int max = omap.get(taggerEvents[ei].getOutcome()); if (max == outcomeList[oei]) { numCorrect ++; } } } display(". (" + numCorrect + "/" + numEvents + ") " + ((double) numCorrect / numEvents) + "\n"); } }
@Override public void validate() { super.validate(); String algorithmName = getAlgorithm(); if (algorithmName != null) { if (!PERCEPTRON_VALUE.equals(algorithmName)) { throw new IllegalArgumentException("algorithmName must be PERCEPTRON"); } } }
@Deprecated @Override public boolean isValid() { try { validate(); return true; } catch (IllegalArgumentException e) { return false; } }
public void checkModelType() throws java.io.IOException { String modelType = readUTF(); if (!modelType.equals("Perceptron")) System.out.println("Error: attempting to load a " + modelType + " model as a Perceptron model." + " You should expect problems."); } }
@Override public void validate() { super.validate(); String algorithmName = getAlgorithm(); if (algorithmName != null) { if (!PERCEPTRON_SEQUENCE_VALUE.equals(algorithmName)) { throw new IllegalArgumentException("algorithmName must be PERCEPTRON_SEQUENCE"); } } }
public static double[] eval(int[] context, double[] prior, EvalParameters model) { return eval(context,null,prior,model,true); }
@Deprecated @Override public boolean isValid() { if (!super.isValid()) { return false; } String algorithmName = getAlgorithm(); if (algorithmName != null) { return PERCEPTRON_VALUE.equals(algorithmName); } else { return true; } }
public double[] eval(String[] context, float[] values,double[] outsums) { Context[] scontexts = new Context[context.length]; java.util.Arrays.fill(outsums, 0); for (int i = 0; i < context.length; i++) { scontexts[i] = pmap.get(context[i]); } return eval(scontexts,values,outsums,evalParams,true); }
static double[] eval(int[] context, float[] values, double[] prior, EvalParameters model, boolean normalize) { Context[] scontexts = new Context[context.length]; for (int i = 0; i < context.length; i++) { scontexts[i] = model.getParams()[context[i]]; } return eval(scontexts, values, prior, model, normalize); }
public double[] eval(String[] context, float[] values) { return eval(context,values,new double[evalParams.getNumOutcomes()]); }
public double[] eval(String[] context) { return eval(context,new double[evalParams.getNumOutcomes()]); }