Class PerceptronTrainer
java.lang.Object
opennlp.tools.ml.AbstractTrainer
opennlp.tools.ml.AbstractEventTrainer
opennlp.tools.ml.perceptron.PerceptronTrainer
- All Implemented Interfaces:
Trainer, EventTrainer
Trains
models using the perceptron algorithm.
Each outcome is represented as a binary perceptron classifier. This supports standard (integer) weighting as well average weighting as described in:
Discriminative Training Methods for Hidden Markov Models: Theory and Experiments with the Perceptron Algorithm. Michael Collins, EMNLP 2002.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[][]Records the array of predicates seen in each event.private static final org.slf4j.Loggerprivate intNumber of events in the event set.private intNumber of outcomes.private intNumber of predicates.private int[]Records the num of times an event has been seen for each event i, in context[i].private intNumber of unique events which occurred in the event set.private String[]Stores the String names of the outcomes.private int[]List of outcomes for each event i, in context[i].static final Stringprivate String[]Stores the String names of the predicates.private Doubleprivate doublestatic final doubleprivate booleanprivate float[][]The value associates with each context.Fields inherited from class AbstractEventTrainer
DATA_INDEXER_ONE_PASS_REAL_VALUE, DATA_INDEXER_ONE_PASS_VALUE, DATA_INDEXER_PARAM, DATA_INDEXER_TWO_PASS_VALUEFields inherited from class AbstractTrainer
reportMap, trainingConfiguration, trainingParametersFields inherited from interface EventTrainer
EVENT_VALUE -
Constructor Summary
ConstructorsConstructorDescriptionInstantiates aPerceptronTrainerwith default training parameters.PerceptronTrainer(TrainingParameters parameters) Instantiates aPerceptronTrainerwith specificTrainingParameters. -
Method Summary
Modifier and TypeMethodDescriptiondoTrain(DataIndexer indexer) private MutableContext[]findParameters(int iterations, boolean useAverage) private StopCriteria<Double> getStopCriteria(TrainingConfiguration trainingConfig) Get theStopCriteriaassociated with thisTrainer.private TrainingProgressMonitorgetTrainingProgressMonitor(TrainingConfiguration trainingConfig) Get theTrainingProgressMonitorassociated with thisTrainer.private static booleanisPerfectSquare(int n) booleanvoidsetSkippedAveraging(boolean averaging) Enables skipped averaging, this flag changes the standard averaging to special averaging instead.voidsetStepSizeDecrease(double decrease) Enables and sets step size decrease.voidsetTolerance(double tolerance) Specifies the tolerance.private doubletrainingStats(EvalParameters evalParams) trainModel(int iterations, DataIndexer di, int cutoff) Trains aPerceptronModelwith given parameters.trainModel(int iterations, DataIndexer di, int cutoff, boolean useAverage) Trains aPerceptronModelwith given parameters.voidvalidate()Checks the configuredparameters.Methods inherited from class AbstractEventTrainer
getDataIndexer, train, trainMethods inherited from class AbstractTrainer
addToReport, getAlgorithm, getCutoff, getIterations, getTrainingConfiguration, init, init
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
PERCEPTRON_VALUE
- See Also:
-
TOLERANCE_DEFAULT
public static final double TOLERANCE_DEFAULT- See Also:
-
numUniqueEvents
private int numUniqueEventsNumber of unique events which occurred in the event set. -
numEvents
private int numEventsNumber of events in the event set. -
numPreds
private int numPredsNumber of predicates. -
numOutcomes
private int numOutcomesNumber of outcomes. -
contexts
private int[][] contextsRecords the array of predicates seen in each event. -
values
private float[][] valuesThe value associates with each context. If null then context values are assumes to be 1. -
outcomeList
private int[] outcomeListList of outcomes for each event i, in context[i]. -
numTimesEventsSeen
private int[] numTimesEventsSeenRecords the num of times an event has been seen for each event i, in context[i]. -
outcomeLabels
Stores the String names of the outcomes. The GIS only tracks outcomes as ints, and so this array is needed to save the model to disk and thereby allow users to know what the outcome was in human understandable terms. -
predLabels
Stores the String names of the predicates. The GIS only tracks predicates as ints, and so this array is needed to save the model to disk and thereby allow users to know what the outcome was in human understandable terms. -
tolerance
private double tolerance -
stepSizeDecrease
-
useSkippedlAveraging
private boolean useSkippedlAveraging
-
-
Constructor Details
-
PerceptronTrainer
public PerceptronTrainer()Instantiates aPerceptronTrainerwith default training parameters. -
PerceptronTrainer
Instantiates aPerceptronTrainerwith specificTrainingParameters.- Parameters:
parameters- Theparameterto use.
-
-
Method Details
-
validate
public void validate()Checks the configuredparameters. If a subclass overrides this, it should callsuper.validate();.- Overrides:
validatein classAbstractEventTrainer- Throws:
IllegalArgumentException- Thrown if the algorithm name is not equal toPERCEPTRON_VALUE.
-
isSortAndMerge
public boolean isSortAndMerge()- Specified by:
isSortAndMergein classAbstractEventTrainer
-
doTrain
- Specified by:
doTrainin classAbstractEventTrainer- Throws:
IOException
-
setTolerance
public void setTolerance(double tolerance) Specifies the tolerance. If the change in training set accuracy is less than this, stop iterating.- Parameters:
tolerance- The level of tolerance. Must not be negative.- Throws:
IllegalArgumentException- Thrown if parameters are invalid.
-
setStepSizeDecrease
public void setStepSizeDecrease(double decrease) Enables and sets step size decrease. The step size is decreased every iteration by the specified value.- Parameters:
decrease- The step size decrease in percent. Must not be negative.- Throws:
IllegalArgumentException- Thrown if parameters are invalid.
-
setSkippedAveraging
public void setSkippedAveraging(boolean averaging) Enables skipped averaging, this flag changes the standard averaging to special averaging instead.If we are doing averaging, and the current iteration is one of the first 20, or if it is a perfect square, then updated the summed parameters.
The reason we don't take all of them is that the parameters change less toward the end of training, so they drown out the contributions of the more volatile early iterations. The use of perfect squares allows us to sample from successively farther apart iterations.
- Parameters:
averaging- Whether to skip 'averaging', or not.
-
trainModel
Trains aPerceptronModelwith given parameters.- Parameters:
iterations- The number of iterations to use for training.di- TheDataIndexerused as data input.cutoff- TheTrainingParameters.CUTOFF_PARAMvalue to use for training.- Returns:
- A valid, trained
perceptron model.
-
trainModel
Trains aPerceptronModelwith given parameters.- Parameters:
iterations- The number of iterations to use for training.di- TheDataIndexerused as data input.cutoff- TheTrainingParameters.CUTOFF_PARAMvalue to use for training.useAverage- Whether to use 'averaging', or not. SeesetSkippedAveraging(boolean)for details.- Returns:
- A valid, trained
perceptron model.
-
findParameters
-
trainingStats
-
isPerfectSquare
private static boolean isPerfectSquare(int n) -
getStopCriteria
Get theStopCriteriaassociated with thisTrainer.- Parameters:
trainingConfig-TrainingConfiguration- Returns:
StopCriteria. IfTrainingConfigurationisnullorTrainingConfiguration.stopCriteria()isnull, then return the defaultStopCriteria.
-
getTrainingProgressMonitor
Get theTrainingProgressMonitorassociated with thisTrainer.- Parameters:
trainingConfig-TrainingConfiguration.- Returns:
TrainingProgressMonitor. IfTrainingConfigurationisnullorTrainingConfiguration.progMon()isnull, then return the defaultTrainingProgressMonitor.
-