You can subscribe to this list here.
2001 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
(5) |
Sep
|
Oct
(14) |
Nov
(37) |
Dec
(13) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2002 |
Jan
(14) |
Feb
|
Mar
|
Apr
(15) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
(3) |
Dec
(2) |
2003 |
Jan
(4) |
Feb
|
Mar
(1) |
Apr
(2) |
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
(4) |
2004 |
Jan
(1) |
Feb
(3) |
Mar
|
Apr
|
May
(4) |
Jun
(3) |
Jul
(1) |
Aug
(6) |
Sep
|
Oct
|
Nov
|
Dec
|
2005 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(17) |
Nov
(3) |
Dec
|
2006 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
(23) |
Dec
|
2007 |
Jan
|
Feb
|
Mar
(7) |
Apr
(17) |
May
(1) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2008 |
Jan
|
Feb
|
Mar
|
Apr
(1) |
May
|
Jun
|
Jul
|
Aug
(3) |
Sep
(20) |
Oct
|
Nov
(15) |
Dec
(2) |
2009 |
Jan
(38) |
Feb
(4) |
Mar
(20) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2010 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(4) |
Jul
|
Aug
(17) |
Sep
(26) |
Oct
|
Nov
(2) |
Dec
|
From: Thomas M. <tsm...@us...> - 2007-03-15 04:51:06
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv6421/src/java/opennlp/maxent Modified Files: TwoPassDataIndexer.java FileEventStream.java Log Message: added support for specifing the encoding for when event files are dumped. Index: TwoPassDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/TwoPassDataIndexer.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** TwoPassDataIndexer.java 21 Nov 2006 21:30:50 -0000 1.7 --- TwoPassDataIndexer.java 15 Mar 2007 04:51:02 -0000 1.8 *************** *** 22,27 **** --- 22,29 ---- import java.io.File; + import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; + import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; *************** *** 53,56 **** --- 55,61 ---- } + public TwoPassDataIndexer(EventStream eventStream, int cutoff) { + this(eventStream,cutoff,null); + } /** * Two argument constructor for DataIndexer. *************** *** 61,65 **** * observed in order to be included in the model. */ ! public TwoPassDataIndexer(EventStream eventStream, int cutoff) { TObjectIntHashMap predicateIndex; List eventsToCompare; --- 66,70 ---- * observed in order to be included in the model. */ ! public TwoPassDataIndexer(EventStream eventStream, int cutoff,String encoding) { TObjectIntHashMap predicateIndex; List eventsToCompare; *************** *** 72,81 **** File tmp = File.createTempFile("events", null); tmp.deleteOnExit(); ! int numEvents = computeEventCounts(eventStream, new FileWriter(tmp), predicateIndex, cutoff); System.out.println("done. " + numEvents + " events"); System.out.print("\tIndexing... "); ! eventsToCompare = index(numEvents, new FileEventStream(tmp), predicateIndex); // done with predicates predicateIndex = null; --- 77,93 ---- File tmp = File.createTempFile("events", null); tmp.deleteOnExit(); ! OutputStreamWriter osw; ! if (encoding != null) { ! osw = new OutputStreamWriter(new FileOutputStream(tmp),encoding); ! } ! else { ! osw = new FileWriter(tmp); ! } ! int numEvents = computeEventCounts(eventStream, osw, predicateIndex, cutoff); System.out.println("done. " + numEvents + " events"); System.out.print("\tIndexing... "); ! eventsToCompare = index(numEvents, new FileEventStream(tmp,encoding), predicateIndex); // done with predicates predicateIndex = null; Index: FileEventStream.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/FileEventStream.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FileEventStream.java 24 Oct 2005 13:37:54 -0000 1.3 --- FileEventStream.java 15 Mar 2007 04:51:02 -0000 1.4 *************** *** 20,25 **** --- 20,27 ---- import java.io.BufferedReader; import java.io.File; + import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; + import java.io.InputStreamReader; import java.util.StringTokenizer; *************** *** 42,47 **** * @throws IOException When the specified file can not be read. */ public FileEventStream(String fileName) throws IOException { ! reader = new BufferedReader(new FileReader(fileName)); } --- 44,62 ---- * @throws IOException When the specified file can not be read. */ + public FileEventStream(String fileName, String encoding) throws IOException { + if (encoding == null) { + reader = new BufferedReader(new FileReader(fileName)); + } + else { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),encoding)); + } + } + public FileEventStream(String fileName) throws IOException { ! this(fileName,null); ! } ! ! public FileEventStream(File file) throws IOException { ! this(file,null); } *************** *** 51,56 **** * @throws IOException When the specified file can not be read. */ ! public FileEventStream(File file) throws IOException { ! reader = new BufferedReader(new FileReader(file)); } --- 66,76 ---- * @throws IOException When the specified file can not be read. */ ! public FileEventStream(File file, String encoding) throws IOException { ! if (encoding == null) { ! reader = new BufferedReader(new FileReader(file)); ! } ! else { ! reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),encoding)); ! } } |
From: Thomas M. <tsm...@us...> - 2007-03-15 04:42:05
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv2661/src/java/opennlp/maxent Added Files: EvalParameters.java Log Message: Moed out of GISModel to prevent GISTrainer reference from being confused in Eclipse. --- NEW FILE: EvalParameters.java --- package opennlp.maxent; /** * This class encapsulates the varibales used in producing probabilities from a model * and facilitaes passing these variables to the eval method. Variables are declared * non-private so that they may be accessed and updated without a method call for efficiency * reasons. * @author Tom Morton * */ public class EvalParameters { /** Mapping between outcomes and paramater values for each context. * The integer representation of the context can be found using <code>pmap</code>.*/ Context[] params; /** The number of outcomes being predicted. */ final int numOutcomes; /** The maximum number of feattures fired in an event. Usually refered to a C. * This is used to normalize the number of features which occur in an event. */ double correctionConstant; /** Stores inverse of the correction constant, 1/C. */ final double constantInverse; /** The correction parameter of the model. */ double correctionParam; /** Log of 1/C; initial value of probabilities. */ final double iprob; /** Stores the number of features that get fired for each outcome in an event. * This is over-written for each event evaluation, but declared once for efficiency.*/ int[] numfeats; /** * Creates a set of paramters which can be evaulated with the eval method. * @param params The parameters of the model. * @param correctionParam The correction paramter. * @param correctionConstant The correction constant. * @param numOutcomes The number of outcomes. */ public EvalParameters(Context[] params, double correctionParam, double correctionConstant, int numOutcomes) { this.params = params; this.correctionParam = correctionParam; this.numOutcomes = numOutcomes; this.numfeats = new int[numOutcomes]; this.correctionConstant = correctionConstant; this.constantInverse = 1.0 / correctionConstant; this.iprob = Math.log(1.0/numOutcomes); } } |
From: Thomas M. <tsm...@us...> - 2006-11-21 23:01:03
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv25321/src/java/opennlp/maxent Modified Files: GISTrainer.java Log Message: fixed trainined with prior bug. Index: GISTrainer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISTrainer.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** GISTrainer.java 21 Nov 2006 21:31:19 -0000 1.23 --- GISTrainer.java 21 Nov 2006 23:00:55 -0000 1.24 *************** *** 279,283 **** else { //determine active outcomes for (int oi = 0; oi < numOutcomes; oi++) { ! if (predCount[pi][oi] > 0) { activeOutcomes[numActiveOutcomes] = oi; numActiveOutcomes++; --- 279,283 ---- else { //determine active outcomes for (int oi = 0; oi < numOutcomes; oi++) { ! if (predCount[pi][oi] > 0 && predicateCounts[pi] > cutoff) { activeOutcomes[numActiveOutcomes] = oi; numActiveOutcomes++; *************** *** 434,438 **** if (useSlackParameter) CFMOD += (evalParams.correctionConstant - contexts[ei].length) * numTimesEventsSeen[ei]; ! loglikelihood += Math.log(modelDistribution[outcomes[ei]]) * numTimesEventsSeen[ei]; numEvents += numTimesEventsSeen[ei]; --- 434,438 ---- if (useSlackParameter) CFMOD += (evalParams.correctionConstant - contexts[ei].length) * numTimesEventsSeen[ei]; ! loglikelihood += Math.log(modelDistribution[outcomes[ei]]) * numTimesEventsSeen[ei]; numEvents += numTimesEventsSeen[ei]; *************** *** 462,465 **** --- 462,468 ---- } else { + if (model[aoi] == 0) { + System.err.println("Model expects == 0 for "+predLabels[pi]+" "+outcomeLabels[aoi]); + } params[pi].updateParameter(aoi,(Math.log(observed[aoi]) - Math.log(model[aoi]))); } |
From: Thomas M. <tsm...@us...> - 2006-11-21 21:31:24
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv20540/src/java/opennlp/maxent Modified Files: GISTrainer.java Log Message: added equals to cutoff. Index: GISTrainer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISTrainer.java,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** GISTrainer.java 15 Nov 2006 21:41:06 -0000 1.22 --- GISTrainer.java 21 Nov 2006 21:31:19 -0000 1.23 *************** *** 417,421 **** for (int j = 0; j < contexts[ei].length; j++) { int pi = contexts[ei][j]; ! if (predicateCounts[pi] > cutoff) { int[] activeOutcomes = modelExpects[pi].getOutcomes(); for (int aoi=0;aoi<activeOutcomes.length;aoi++) { --- 417,421 ---- for (int j = 0; j < contexts[ei].length; j++) { int pi = contexts[ei][j]; ! if (predicateCounts[pi] >= cutoff) { int[] activeOutcomes = modelExpects[pi].getOutcomes(); for (int aoi=0;aoi<activeOutcomes.length;aoi++) { |
From: Thomas M. <tsm...@us...> - 2006-11-21 21:30:53
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv20115/src/java/opennlp/maxent Modified Files: TwoPassDataIndexer.java Log Message: added code to populate predicate map. Index: TwoPassDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/TwoPassDataIndexer.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** TwoPassDataIndexer.java 15 Nov 2006 21:38:47 -0000 1.6 --- TwoPassDataIndexer.java 21 Nov 2006 21:30:50 -0000 1.7 *************** *** 28,31 **** --- 28,32 ---- import java.util.Arrays; import java.util.HashSet; + import java.util.Iterator; import java.util.List; import java.util.Set; *************** *** 113,117 **** update(ec,predicateSet,counter,cutoff); } ! predicatesInOut.trimToSize(); eventStore.close(); return eventCount; --- 114,124 ---- update(ec,predicateSet,counter,cutoff); } ! predCounts = new int[predicateSet.size()]; ! int index = 0; ! for (Iterator pi=predicateSet.iterator();pi.hasNext();index++) { ! String predicate = (String) pi.next(); ! predCounts[index] = counter.get(predicate); ! predicatesInOut.put(predicate,index); ! } eventStore.close(); return eventCount; |
From: Thomas M. <tsm...@us...> - 2006-11-21 21:30:04
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv19598/src/java/opennlp/maxent Modified Files: OnePassDataIndexer.java Log Message: fixed bug for adding predicate to map. Index: OnePassDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/OnePassDataIndexer.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** OnePassDataIndexer.java 15 Nov 2006 21:38:19 -0000 1.3 --- OnePassDataIndexer.java 21 Nov 2006 21:29:59 -0000 1.4 *************** *** 106,109 **** --- 106,110 ---- String predicate = (String) pi.next(); predCounts[index] = counter.get(predicate); + predicatesInOut.put(predicate,index); } return events; |
From: Thomas M. <tsm...@us...> - 2006-11-21 21:25:50
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv17696/src/java/opennlp/maxent Modified Files: AbstractDataIndexer.java Log Message: fixed bug with predicate identification. Index: AbstractDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/AbstractDataIndexer.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** AbstractDataIndexer.java 15 Nov 2006 21:37:50 -0000 1.3 --- AbstractDataIndexer.java 21 Nov 2006 21:25:46 -0000 1.4 *************** *** 131,135 **** counter.put(ec[j], 1); } ! if (predicateSet.contains(ec[j]) && counter.get(ec[j]) >= cutoff) { predicateSet.add(ec[j]); } --- 131,135 ---- counter.put(ec[j], 1); } ! if (!predicateSet.contains(ec[j]) && counter.get(ec[j]) >= cutoff) { predicateSet.add(ec[j]); } |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:42:47
|
Update of /cvsroot/maxent/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv1090 Modified Files: build.xml Log Message: updated version number. Index: build.xml =================================================================== RCS file: /cvsroot/maxent/maxent/build.xml,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** build.xml 6 Oct 2005 11:04:40 -0000 1.24 --- build.xml 15 Nov 2006 21:42:44 -0000 1.25 *************** *** 10,14 **** <property name="Name" value="Maxent" /> <property name="name" value="maxent" /> ! <property name="version" value="2.4.0" /> <property name="year" value="2005"/> --- 10,14 ---- <property name="Name" value="Maxent" /> <property name="name" value="maxent" /> ! <property name="version" value="2.5.0" /> <property name="year" value="2005"/> |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:42:07
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv674/src/java/opennlp/maxent Modified Files: GIS.java Log Message: extended interface. Index: GIS.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GIS.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** GIS.java 13 Oct 2005 18:21:15 -0000 1.7 --- GIS.java 15 Nov 2006 21:42:04 -0000 1.8 *************** *** 116,120 **** */ public static GISModel trainModel(int iterations, DataIndexer indexer, boolean smoothing) { ! return trainModel(iterations,indexer,false,smoothing); } --- 116,120 ---- */ public static GISModel trainModel(int iterations, DataIndexer indexer, boolean smoothing) { ! return trainModel(iterations,indexer,false,smoothing,null,0); } *************** *** 123,149 **** * @param iterations The number of GIS iterations to perform. * @param indexer The object which will be used for event compilation. - * @param printMessagesWhileTraining Determines whether training status messages are written to STDOUT. - * @param smoothing Defines whether the created trainer will use smoothing while training the model. * @return The newly trained model, which can be used immediately or saved * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public static GISModel trainModel(int iterations, DataIndexer indexer, boolean printMessagesWhileTraining, boolean smoothing) { ! GISTrainer trainer = new GISTrainer(printMessagesWhileTraining); ! trainer.setSmoothing(smoothing); ! trainer.setSmoothingObservation(SMOOTHING_OBSERVATION); ! return trainer.trainModel(iterations, indexer); } ! /** ! * Train a model using the GIS algorithm. * @param iterations The number of GIS iterations to perform. * @param indexer The object which will be used for event compilation. * @return The newly trained model, which can be used immediately or saved * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public static GISModel trainModel(int iterations, DataIndexer indexer) { ! return trainModel(iterations,indexer,true,false); } ! } --- 123,168 ---- * @param iterations The number of GIS iterations to perform. * @param indexer The object which will be used for event compilation. * @return The newly trained model, which can be used immediately or saved * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public static GISModel trainModel(int iterations, DataIndexer indexer) { ! return trainModel(iterations,indexer,true,false,null,0); } ! /** ! * Train a model using the GIS algorithm with the specified number of iterations, data indexer, and prior. * @param iterations The number of GIS iterations to perform. * @param indexer The object which will be used for event compilation. + * @param modelPrior The prior distribution for the model. * @return The newly trained model, which can be used immediately or saved * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public static GISModel trainModel(int iterations, DataIndexer indexer, Prior modelPrior, int cutoff) { ! return trainModel(iterations,indexer,true,false,modelPrior,cutoff); } ! ! ! /** ! * Train a model using the GIS algorithm. ! * @param iterations The number of GIS iterations to perform. ! * @param indexer The object which will be used for event compilation. ! * @param printMessagesWhileTraining Determines whether training status messages are written to STDOUT. ! * @param smoothing Defines whether the created trainer will use smoothing while training the model. ! * @param modelPrior The prior distribution for the model. ! * @param cutoff The number of times a predicate must occur to be used in a model. ! * @return The newly trained model, which can be used immediately or saved ! * to disk using an opennlp.maxent.io.GISModelWriter object. ! */ ! public static GISModel trainModel(int iterations, DataIndexer indexer, boolean printMessagesWhileTraining, boolean smoothing, Prior modelPrior, int cutoff) { ! GISTrainer trainer = new GISTrainer(printMessagesWhileTraining); ! trainer.setSmoothing(smoothing); ! trainer.setSmoothingObservation(SMOOTHING_OBSERVATION); ! if (modelPrior != null) { ! return trainer.trainModel(iterations, indexer, modelPrior,cutoff); ! } ! else { ! return trainer.trainModel(iterations, indexer,0); ! } ! } } |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:41:45
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv546/src/java/opennlp/maxent Modified Files: GISModel.java Log Message: updated for UniformPrior change. Index: GISModel.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISModel.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** GISModel.java 9 Nov 2006 20:53:49 -0000 1.17 --- GISModel.java 15 Nov 2006 21:41:42 -0000 1.18 *************** *** 49,53 **** */ public GISModel (Context[] params, String[] predLabels, String[] outcomeNames, int correctionConstant, double correctionParam) { ! this(params,predLabels,outcomeNames,correctionConstant,correctionParam, new UniformPrior(outcomeNames.length)); } --- 49,53 ---- */ public GISModel (Context[] params, String[] predLabels, String[] outcomeNames, int correctionConstant, double correctionParam) { ! this(params,predLabels,outcomeNames,correctionConstant,correctionParam, new UniformPrior()); } |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:41:09
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv333/src/java/opennlp/maxent Modified Files: GISTrainer.java Log Message: update to reflect cutoff changes for priors. Index: GISTrainer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISTrainer.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** GISTrainer.java 9 Nov 2006 20:55:23 -0000 1.21 --- GISTrainer.java 15 Nov 2006 21:41:06 -0000 1.22 *************** *** 63,68 **** private boolean printMessages = false; ! /** Number of event tokens. */ ! private int numTokens; /** Number of predicates. */ private int numPreds; --- 63,68 ---- private boolean printMessages = false; ! /** Number of unique events which occured in the event set. */ ! private int numUniqueEvents; /** Number of predicates. */ private int numPreds; *************** *** 80,83 **** --- 80,88 ---- /** Records the num of times an event has been seen for each event i, in context[i]. */ private int[] numTimesEventsSeen; + + /** The number of times a predicate occured in the training data. */ + private int[] predicateCounts; + + private int cutoff; /** Stores the String names of the outcomes. The GIS only tracks outcomes *************** *** 176,180 **** */ public GISModel trainModel(EventStream eventStream, int iterations, int cutoff) { ! return trainModel(iterations, new OnePassDataIndexer(eventStream,cutoff)); } --- 181,185 ---- */ public GISModel trainModel(EventStream eventStream, int iterations, int cutoff) { ! return trainModel(iterations, new OnePassDataIndexer(eventStream,cutoff),cutoff); } *************** *** 187,192 **** * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public GISModel trainModel(int iterations, DataIndexer di) { ! return trainModel(iterations,di,new UniformPrior(di.getOutcomeList().length)); } --- 192,197 ---- * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public GISModel trainModel(int iterations, DataIndexer di, int cutoff) { ! return trainModel(iterations,di,new UniformPrior(),cutoff); } *************** *** 200,210 **** * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public GISModel trainModel(int iterations, DataIndexer di, Prior modelPrior) { /************** Incorporate all of the needed info ******************/ display("Incorporating indexed data for training... \n"); contexts = di.getContexts(); outcomes = di.getOutcomeList(); numTimesEventsSeen = di.getNumTimesEventsSeen(); ! numTokens = contexts.length; this.prior = modelPrior; //printTable(contexts); --- 205,217 ---- * to disk using an opennlp.maxent.io.GISModelWriter object. */ ! public GISModel trainModel(int iterations, DataIndexer di, Prior modelPrior, int cutoff) { /************** Incorporate all of the needed info ******************/ display("Incorporating indexed data for training... \n"); contexts = di.getContexts(); outcomes = di.getOutcomeList(); + this.cutoff = cutoff; + predicateCounts = di.getPredCounts(); numTimesEventsSeen = di.getNumTimesEventsSeen(); ! numUniqueEvents = contexts.length; this.prior = modelPrior; //printTable(contexts); *************** *** 225,231 **** predLabels = di.getPredLabels(); numPreds = predLabels.length; ! display("\tNumber of Event Tokens: " + numTokens + "\n"); display("\t Number of Outcomes: " + numOutcomes + "\n"); display("\t Number of Predicates: " + numPreds + "\n"); --- 232,239 ---- predLabels = di.getPredLabels(); + prior.setLabels(outcomeLabels,predLabels); numPreds = predLabels.length; ! display("\tNumber of Event Tokens: " + numUniqueEvents + "\n"); display("\t Number of Outcomes: " + numOutcomes + "\n"); display("\t Number of Predicates: " + numPreds + "\n"); *************** *** 233,239 **** // set up feature arrays int[][] predCount = new int[numPreds][numOutcomes]; ! for (int ti = 0; ti < numTokens; ti++) ! for (int j = 0; j < contexts[ti].length; j++) predCount[contexts[ti][j]][outcomeList[ti]] += numTimesEventsSeen[ti]; //printTable(predCount); --- 241,249 ---- // set up feature arrays int[][] predCount = new int[numPreds][numOutcomes]; ! for (int ti = 0; ti < numUniqueEvents; ti++) { ! for (int j = 0; j < contexts[ti].length; j++) { predCount[contexts[ti][j]][outcomeList[ti]] += numTimesEventsSeen[ti]; + } + } //printTable(predCount); *************** *** 303,307 **** if (useSlackParameter) { int cfvalSum = 0; ! for (int ti = 0; ti < numTokens; ti++) { for (int j = 0; j < contexts[ti].length; j++) { int pi = contexts[ti][j]; --- 313,317 ---- if (useSlackParameter) { int cfvalSum = 0; ! for (int ti = 0; ti < numUniqueEvents; ti++) { for (int j = 0; j < contexts[ti].length; j++) { int pi = contexts[ti][j]; *************** *** 402,419 **** int numEvents = 0; int numCorrect = 0; ! for (int TID = 0; TID < numTokens; TID++) { ! prior.logPrior(modelDistribution,contexts[TID]); ! GISModel.eval(contexts[TID], modelDistribution, evalParams); ! for (int j = 0; j < contexts[TID].length; j++) { ! int pi = contexts[TID][j]; ! int[] activeOutcomes = modelExpects[pi].getOutcomes(); ! for (int aoi=0;aoi<activeOutcomes.length;aoi++) { ! int oi = activeOutcomes[aoi]; ! modelExpects[pi].updateParameter(aoi,modelDistribution[oi] * numTimesEventsSeen[TID]); ! } ! if (useSlackParameter) { ! for (int oi = 0; oi < numOutcomes; oi++) { ! if (!modelExpects[pi].contains(oi)) { ! CFMOD += modelDistribution[oi] * numTimesEventsSeen[TID]; } } --- 412,431 ---- int numEvents = 0; int numCorrect = 0; ! for (int ei = 0; ei < numUniqueEvents; ei++) { ! prior.logPrior(modelDistribution,contexts[ei]); ! GISModel.eval(contexts[ei], modelDistribution, evalParams); ! for (int j = 0; j < contexts[ei].length; j++) { ! int pi = contexts[ei][j]; ! if (predicateCounts[pi] > cutoff) { ! int[] activeOutcomes = modelExpects[pi].getOutcomes(); ! for (int aoi=0;aoi<activeOutcomes.length;aoi++) { ! int oi = activeOutcomes[aoi]; ! modelExpects[pi].updateParameter(aoi,modelDistribution[oi] * numTimesEventsSeen[ei]); ! } ! if (useSlackParameter) { ! for (int oi = 0; oi < numOutcomes; oi++) { ! if (!modelExpects[pi].contains(oi)) { ! CFMOD += modelDistribution[oi] * numTimesEventsSeen[ei]; ! } } } *************** *** 421,428 **** } if (useSlackParameter) ! CFMOD += (evalParams.correctionConstant - contexts[TID].length) * numTimesEventsSeen[TID]; ! loglikelihood += Math.log(modelDistribution[outcomes[TID]]) * numTimesEventsSeen[TID]; ! numEvents += numTimesEventsSeen[TID]; if (printMessages) { int max = 0; --- 433,440 ---- } if (useSlackParameter) ! CFMOD += (evalParams.correctionConstant - contexts[ei].length) * numTimesEventsSeen[ei]; ! loglikelihood += Math.log(modelDistribution[outcomes[ei]]) * numTimesEventsSeen[ei]; ! numEvents += numTimesEventsSeen[ei]; if (printMessages) { int max = 0; *************** *** 432,437 **** } } ! if (max == outcomes[TID]) { ! numCorrect += numTimesEventsSeen[TID]; } } --- 444,449 ---- } } ! if (max == outcomes[ei]) { ! numCorrect += numTimesEventsSeen[ei]; } } *************** *** 450,454 **** } else { ! params[pi].updateParameter(aoi,((Math.log(observed[aoi])) - Math.log(model[aoi]))); } modelExpects[pi].setParameter(aoi,0.0); // re-initialize to 0.0's --- 462,466 ---- } else { ! params[pi].updateParameter(aoi,(Math.log(observed[aoi]) - Math.log(model[aoi]))); } modelExpects[pi].setParameter(aoi,0.0); // re-initialize to 0.0's |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:40:11
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv32012/src/java/opennlp/maxent Modified Files: UniformPrior.java Log Message: updated to reflect interface change. Index: UniformPrior.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/UniformPrior.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** UniformPrior.java 9 Nov 2006 20:56:37 -0000 1.1 --- UniformPrior.java 15 Nov 2006 21:40:01 -0000 1.2 *************** *** 10,24 **** private int numOutcomes; private double r; ! ! /** ! * Creates a uniform prior of 1/N for each outcome where N is the specified number ! * of outcomes. ! * @param numOutcomes ! */ ! public UniformPrior(int numOutcomes) { ! this.numOutcomes = numOutcomes; ! r = Math.log(1.0/numOutcomes); ! } ! public void logPrior(double[] dist, int[] context) { for (int oi=0;oi<numOutcomes;oi++) { --- 10,14 ---- private int numOutcomes; private double r; ! public void logPrior(double[] dist, int[] context) { for (int oi=0;oi<numOutcomes;oi++) { *************** *** 27,29 **** --- 17,23 ---- } + public void setLabels(String[] outcomeLabels, String[] contextLabels) { + this.numOutcomes = outcomeLabels.length; + r = Math.log(1.0/numOutcomes); + } } |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:39:42
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv31923/src/java/opennlp/maxent Modified Files: Prior.java Log Message: added setLabels method so contexts sent to Prior can be decoded. Index: Prior.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/Prior.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Prior.java 9 Nov 2006 20:56:06 -0000 1.1 --- Prior.java 15 Nov 2006 21:39:38 -0000 1.2 *************** *** 15,17 **** --- 15,26 ---- */ public void logPrior(double[] dist, int[] context); + + /** + * Method to specify the label for the outcomes and contexts. This is used to map + * integer outcomes and contexts to their string values. This method is called prior + * to any call to #logPrior. + * @param outcomeLabels An array of each outcome label. + * @param contextLabels An array of each context label. + */ + public void setLabels(String[] outcomeLabels, String[] contextLabels); } |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:38:50
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv31440/src/java/opennlp/maxent Modified Files: TwoPassDataIndexer.java Log Message: updated to support DataIndexer interface changes. Index: TwoPassDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/TwoPassDataIndexer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** TwoPassDataIndexer.java 21 Nov 2005 18:55:24 -0000 1.5 --- TwoPassDataIndexer.java 15 Nov 2006 21:38:47 -0000 1.6 *************** *** 27,31 **** --- 27,33 ---- import java.util.ArrayList; import java.util.Arrays; + import java.util.HashSet; import java.util.List; + import java.util.Set; /** *************** *** 102,107 **** private int computeEventCounts(EventStream eventStream, Writer eventStore, TObjectIntHashMap predicatesInOut, int cutoff) throws IOException { TObjectIntHashMap counter = new TObjectIntHashMap(); - int predicateIndex = 0; int eventCount = 0; while (eventStream.hasNext()) { Event ev = eventStream.nextEvent(); --- 104,109 ---- private int computeEventCounts(EventStream eventStream, Writer eventStore, TObjectIntHashMap predicatesInOut, int cutoff) throws IOException { TObjectIntHashMap counter = new TObjectIntHashMap(); int eventCount = 0; + Set predicateSet = new HashSet(); while (eventStream.hasNext()) { Event ev = eventStream.nextEvent(); *************** *** 109,124 **** eventStore.write(FileEventStream.toLine(ev)); String[] ec = ev.getContext(); ! for (int j = 0; j < ec.length; j++) { ! if (!predicatesInOut.containsKey(ec[j])) { ! if (counter.increment(ec[j])) {} ! else { ! counter.put(ec[j], 1); ! } ! if (counter.get(ec[j]) >= cutoff) { ! predicatesInOut.put(ec[j], predicateIndex++); ! counter.remove(ec[j]); ! } ! } ! } } predicatesInOut.trimToSize(); --- 111,115 ---- eventStore.write(FileEventStream.toLine(ev)); String[] ec = ev.getContext(); ! update(ec,predicateSet,counter,cutoff); } predicatesInOut.trimToSize(); |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:38:23
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv31343/src/java/opennlp/maxent Modified Files: OnePassDataIndexer.java Log Message: updated to support DataIndexer interface changes. Index: OnePassDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/OnePassDataIndexer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** OnePassDataIndexer.java 29 Aug 2004 18:59:12 -0000 1.2 --- OnePassDataIndexer.java 15 Nov 2006 21:38:19 -0000 1.3 *************** *** 91,122 **** */ private TLinkedList computeEventCounts(EventStream eventStream, ! TObjectIntHashMap predicatesInOut, ! int cutoff) { ! TObjectIntHashMap counter = new TObjectIntHashMap(); ! TLinkedList events = new TLinkedList(); ! int predicateIndex = 0; ! while (eventStream.hasNext()) { ! Event ev = eventStream.nextEvent(); ! events.addLast(ev); ! String[] ec = ev.getContext(); ! for (int j=0; j<ec.length; j++) { ! if (! predicatesInOut.containsKey(ec[j])) { ! if (counter.increment(ec[j])) { ! } else { ! counter.put(ec[j], 1); ! } ! if (counter.get(ec[j]) >= cutoff) { ! predicatesInOut.put(ec[j], predicateIndex++); ! counter.remove(ec[j]); ! //if (predicateIndex %1000 == 0) { ! // System.err.println(predicateIndex+ " predicates"+ "event.length="+ec.length+" "+Arrays.asList(ec)); ! // counter.trimToSize(); ! //} ! } ! } ! } ! } ! predicatesInOut.trimToSize(); ! return events; } --- 91,111 ---- */ private TLinkedList computeEventCounts(EventStream eventStream, ! TObjectIntHashMap predicatesInOut, ! int cutoff) { ! Set predicateSet = new HashSet(); ! TObjectIntHashMap counter = new TObjectIntHashMap(); ! TLinkedList events = new TLinkedList(); ! while (eventStream.hasNext()) { ! Event ev = eventStream.nextEvent(); ! events.addLast(ev); ! update(ev.getContext(),predicateSet,counter,cutoff); ! } ! predCounts = new int[predicateSet.size()]; ! int index = 0; ! for (Iterator pi=predicateSet.iterator();pi.hasNext();index++) { ! String predicate = (String) pi.next(); ! predCounts[index] = counter.get(predicate); ! } ! return events; } *************** *** 127,131 **** int numEvents = events.size(); int outcomeCount = 0; - int predCount = 0; List eventsToCompare = new ArrayList(numEvents); TIntArrayList indexedContext = new TIntArrayList(); --- 116,119 ---- *************** *** 136,140 **** ComparableEvent ce; ! int predID, ocID; String oc = ev.getOutcome(); --- 124,128 ---- ComparableEvent ce; ! int ocID; String oc = ev.getOutcome(); *************** *** 158,164 **** eventsToCompare.add(ce); } ! else { ! System.err.println("Dropped event "+ev.getOutcome()+":"+Arrays.asList(ev.getContext())); ! } // recycle the TIntArrayList indexedContext.resetQuick(); --- 146,152 ---- eventsToCompare.add(ce); } ! else { ! System.err.println("Dropped event "+ev.getOutcome()+":"+Arrays.asList(ev.getContext())); ! } // recycle the TIntArrayList indexedContext.resetQuick(); |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:37:55
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv30903/src/java/opennlp/maxent Modified Files: AbstractDataIndexer.java Log Message: updated to support DataIndexer interface changes. Index: AbstractDataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/AbstractDataIndexer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AbstractDataIndexer.java 29 Aug 2004 18:59:12 -0000 1.2 --- AbstractDataIndexer.java 15 Nov 2006 21:37:50 -0000 1.3 *************** *** 23,26 **** --- 23,27 ---- import java.util.Collections; import java.util.List; + import java.util.Set; /** *************** *** 40,43 **** --- 41,46 ---- /** The names of the outcomes. */ protected String[] outcomeLabels; + /** The number of times each predicate occured. */ + protected int[] predCounts; public int[][] getContexts() { *************** *** 63,66 **** --- 66,73 ---- + public int[] getPredCounts() { + return predCounts; + } + /** * Sorts and uniques the array of comparable events. This method *************** *** 111,134 **** } } /** ! * Utility method for creating a String[] array from a map whose ! * keys are labels (Strings) to be stored in the array and whose ! * values are the indices (Integers) at which the corresponding ! * labels should be inserted. ! * ! * @param labelToIndexMap a <code>TObjectIntHashMap</code> value ! * @return a <code>String[]</code> value ! * @since maxent 1.2.6 ! */ protected static String[] toIndexedStringArray(TObjectIntHashMap labelToIndexMap) { ! final String[] array = new String[labelToIndexMap.size()]; ! labelToIndexMap.forEachEntry(new TObjectIntProcedure() { ! public boolean execute(Object str, int index) { ! array[index] = (String)str; ! return true; ! } ! }); ! return array; } } --- 118,159 ---- } } + + /** + * Updates the set of predicated and counter with the specified event contexts and cutoff. + * @param ec The contexts/features which occur in a event. + * @param predicateSet The set of predicates which will be used for model building. + * @param counter The predicate counters. + * @param cutoff The cutoff which determines whether a predicate is included. + */ + protected static void update(String[] ec, Set predicateSet, TObjectIntHashMap counter, int cutoff) { + for (int j=0; j<ec.length; j++) { + if (!counter.increment(ec[j])) { + counter.put(ec[j], 1); + } + if (predicateSet.contains(ec[j]) && counter.get(ec[j]) >= cutoff) { + predicateSet.add(ec[j]); + } + } + } /** ! * Utility method for creating a String[] array from a map whose ! * keys are labels (Strings) to be stored in the array and whose ! * values are the indices (Integers) at which the corresponding ! * labels should be inserted. ! * ! * @param labelToIndexMap a <code>TObjectIntHashMap</code> value ! * @return a <code>String[]</code> value ! * @since maxent 1.2.6 ! */ protected static String[] toIndexedStringArray(TObjectIntHashMap labelToIndexMap) { ! final String[] array = new String[labelToIndexMap.size()]; ! labelToIndexMap.forEachEntry(new TObjectIntProcedure() { ! public boolean execute(Object str, int index) { ! array[index] = (String)str; ! return true; ! } ! }); ! return array; } } |
From: Thomas M. <tsm...@us...> - 2006-11-15 21:37:03
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv30407/src/java/opennlp/maxent Modified Files: DataIndexer.java Log Message: added method to interface allow filtering of predicates to take place during training rather than by data indexer. Index: DataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/DataIndexer.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** DataIndexer.java 9 Nov 2006 20:52:14 -0000 1.13 --- DataIndexer.java 15 Nov 2006 21:36:59 -0000 1.14 *************** *** 48,51 **** --- 48,57 ---- /** + * Returns an array of the count of each predicate in the events. + * @return an array of the count of each predicate in the events. + */ + public int[] getPredCounts(); + + /** * Returns an array of outcome names. * @return an array of outcome names indexed by outcome index. |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:52:49
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent/io In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24137/src/java/opennlp/maxent/io Modified Files: GISModelReader.java Log Message: remove unused import. Index: GISModelReader.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/io/GISModelReader.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** GISModelReader.java 6 Oct 2005 11:04:16 -0000 1.6 --- GISModelReader.java 9 Nov 2006 20:57:04 -0000 1.7 *************** *** 22,26 **** import opennlp.maxent.Context; import opennlp.maxent.GISModel; - import opennlp.maxent.TIntParamHashMap; /** --- 22,25 ---- |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:52:49
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24100/src/java/opennlp/maxent Added Files: UniformPrior.java Log Message: Implementation for uniform prior, default for maxent. --- NEW FILE: UniformPrior.java --- package opennlp.maxent; /** * Provide a maximum entropy model with a uniform prior. * @author Tom Morton * */ public class UniformPrior implements Prior { private int numOutcomes; private double r; /** * Creates a uniform prior of 1/N for each outcome where N is the specified number * of outcomes. * @param numOutcomes */ public UniformPrior(int numOutcomes) { this.numOutcomes = numOutcomes; r = Math.log(1.0/numOutcomes); } public void logPrior(double[] dist, int[] context) { for (int oi=0;oi<numOutcomes;oi++) { dist[oi] = r; } } } |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:52:28
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv23606/src/java/opennlp/maxent Modified Files: GISTrainer.java Log Message: update to use priors. started some gaussian smoothing code. removed use of globals for indicies (yeah!). Index: GISTrainer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISTrainer.java,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** GISTrainer.java 8 Nov 2006 22:03:43 -0000 1.20 --- GISTrainer.java 9 Nov 2006 20:55:23 -0000 1.21 *************** *** 1,4 **** ///////////////////////////////////////////////////////////////////////////// ! // Copyright (C) 2001 Jason Baldridge and Gann Bierner and Tom Morton // // This library is free software; you can redistribute it and/or --- 1,4 ---- ///////////////////////////////////////////////////////////////////////////// ! // Copyright (C) 2001 Jason Baldridge, Gann Bierner, and Tom Morton // // This library is free software; you can redistribute it and/or *************** *** 26,47 **** * * The slack parameter used in the above implementation has been removed by default ! * from the computation per Investigating GIS and Smoothing for Maximum Entropy Taggers, Clark and Curran (2002). * <a href="http://acl.ldc.upenn.edu/E/E03/E03-1071.pdf"><code>http://acl.ldc.upenn.edu/E/E03/E03-1071.pdf</code></a> ! * The slack parameter can be used by setting _useSlackParameter to true. * ! * @author Jason Baldridge * @author Tom Morton * @version $Revision$, $Date$ */ class GISTrainer { ! // This can improve model accuracy, though training will potentially take ! // longer and use more memory. Model size will also be larger. Initial ! // testing indicates improvements for models built on small data sets and ! // few outcomes, but performance degradation for those with large data ! // sets and lots of outcomes. ! private boolean _simpleSmoothing = false; ! ! private boolean _useSlackParameter = false; private double sigma = 2.0; --- 26,57 ---- * * The slack parameter used in the above implementation has been removed by default ! * from the computation and a method for updating with Gaussian smoothing has been ! * added per Investigating GIS and Smoothing for Maximum Entropy Taggers, Clark and Curran (2002). * <a href="http://acl.ldc.upenn.edu/E/E03/E03-1071.pdf"><code>http://acl.ldc.upenn.edu/E/E03/E03-1071.pdf</code></a> ! * The slack parameter can be used by setting <code>useSlackParameter</code> to true. ! * Gaussian smoothing can be used by setting <code>useGaussianSmoothing</code> to true. * ! * A prior can be used to train models which converge to the distribution which minimizes the ! * relative entropy between the distribution specified by the emperical constraints of the training ! * data and the specified prior. By default, the uniform distribution is used as the prior. ! * * @author Tom Morton + * @author Jason Baldridge * @version $Revision$, $Date$ */ class GISTrainer { ! /** ! * Specifies whether unseen context/outcome pairs should be estimated as occur very infrequently. ! */ ! private boolean useSimpleSmoothing = false; ! /** ! * Specifies whether a slack parameter should be used in the model. ! */ ! private boolean useSlackParameter = false; ! /** Specified whether parameter updates should prefer a distribution of parameters which ! * is gaussian. ! */ ! private boolean useGaussianSmoothing = false; private double sigma = 2.0; *************** *** 53,65 **** private boolean printMessages = false; ! private int numTokens; // # of event tokens ! private int numPreds; // # of predicates ! private int numOutcomes; // # of outcomes ! /** A global index variable for Events. */ ! private int TID; ! /** A global index variable for Predicates. */ ! private int PID; ! /** A global index variable for Outcomes. */ ! private int OID; /** Records the array of predicates seen in each event. */ --- 63,72 ---- private boolean printMessages = false; ! /** Number of event tokens. */ ! private int numTokens; ! /** Number of predicates. */ ! private int numPreds; ! /** Number of outcomes. */ ! private int numOutcomes; /** Records the array of predicates seen in each event. */ *************** *** 68,76 **** /** Records the array of outcomes seen in each event. */ private int[] outcomes; ! private int[] outcomeList; ! // records the num of times an event has been seen, paired to ! // int[][] contexts private int[] numTimesEventsSeen; --- 75,82 ---- /** Records the array of outcomes seen in each event. */ private int[] outcomes; ! /** List of outcomes for each event i, in context[i]. */ private int[] outcomeList; ! /** Records the num of times an event has been seen for each event i, in context[i]. */ private int[] numTimesEventsSeen; *************** *** 95,106 **** /** Stores the expected values of the features based on the current models */ private MutableContext[] modelExpects; - /** The maximum number of feattures fired in an event. Usually refered to a C.*/ - //private int constant; - /** Stores inverse of constant, 1/C. */ - //private double constantInverse; - /** The correction parameter of the model. */ - //private double correctionParam; /** Observed expectation of correction feature. */ private double cfObservedExpect; --- 101,109 ---- /** Stores the expected values of the features based on the current models */ private MutableContext[] modelExpects; + + /** This is the prior distribution that the model uses for training. */ + private Prior prior; /** Observed expectation of correction feature. */ private double cfObservedExpect; *************** *** 148,152 **** */ public void setSmoothing(boolean smooth) { ! _simpleSmoothing = smooth; } --- 151,155 ---- */ public void setSmoothing(boolean smooth) { ! useSimpleSmoothing = smooth; } *************** *** 162,170 **** _smoothingObservation = timesSeen; } public GISModel trainModel(EventStream eventStream, int iterations, int cutoff) { ! return trainModel(iterations, new OnePassDataIndexer(eventStream, cutoff)); } ! /** * Train a model using the GIS algorithm. --- 165,182 ---- _smoothingObservation = timesSeen; } + + /** + * Trains a GIS model on the event in the specified event stream, using the specified number + * of iterations and the specified count cutoff. + * @param eventStream A stream of all events. + * @param iterations The number of iterations to use for GIS. + * @param cutoff The number of times a feature must occur to be included. + * @return A GIS model trained with specified + */ public GISModel trainModel(EventStream eventStream, int iterations, int cutoff) { ! return trainModel(iterations, new OnePassDataIndexer(eventStream,cutoff)); } ! /** * Train a model using the GIS algorithm. *************** *** 176,179 **** --- 188,204 ---- */ public GISModel trainModel(int iterations, DataIndexer di) { + return trainModel(iterations,di,new UniformPrior(di.getOutcomeList().length)); + } + + /** + * 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 modelPrior The prior distribution used to train this model. + * @return The newly trained model, which can be used immediately or saved + * to disk using an opennlp.maxent.io.GISModelWriter object. + */ + public GISModel trainModel(int iterations, DataIndexer di, Prior modelPrior) { /************** Incorporate all of the needed info ******************/ display("Incorporating indexed data for training... \n"); *************** *** 182,193 **** numTimesEventsSeen = di.getNumTimesEventsSeen(); numTokens = contexts.length; ! //printTable(contexts); // determine the correction constant and its inverse int correctionConstant = contexts[0].length; ! for (TID = 1; TID < contexts.length; TID++) { ! if (contexts[TID].length > correctionConstant) { ! correctionConstant = contexts[TID].length; } } --- 207,218 ---- numTimesEventsSeen = di.getNumTimesEventsSeen(); numTokens = contexts.length; ! this.prior = modelPrior; //printTable(contexts); // determine the correction constant and its inverse int correctionConstant = contexts[0].length; ! for (int ci = 1; ci < contexts.length; ci++) { ! if (contexts[ci].length > correctionConstant) { ! correctionConstant = contexts[ci].length; } } *************** *** 208,214 **** // set up feature arrays int[][] predCount = new int[numPreds][numOutcomes]; ! for (TID = 0; TID < numTokens; TID++) ! for (int j = 0; j < contexts[TID].length; j++) ! predCount[contexts[TID][j]][outcomeList[TID]] += numTimesEventsSeen[TID]; //printTable(predCount); --- 233,239 ---- // set up feature arrays int[][] predCount = new int[numPreds][numOutcomes]; ! for (int ti = 0; ti < numTokens; ti++) ! for (int j = 0; j < contexts[ti].length; j++) ! predCount[contexts[ti][j]][outcomeList[ti]] += numTimesEventsSeen[ti]; //printTable(predCount); *************** *** 232,249 **** int[] outcomePattern; int[] allOutcomesPattern= new int[numOutcomes]; ! for (OID = 0; OID < numOutcomes; OID++) { ! allOutcomesPattern[OID] = OID; } int numActiveOutcomes = 0; ! for (PID = 0; PID < numPreds; PID++) { numActiveOutcomes = 0; ! if (_simpleSmoothing) { numActiveOutcomes = numOutcomes; outcomePattern = allOutcomesPattern; } else { //determine active outcomes ! for (OID = 0; OID < numOutcomes; OID++) { ! if (predCount[PID][OID] > 0) { ! activeOutcomes[numActiveOutcomes] = OID; numActiveOutcomes++; } --- 257,274 ---- int[] outcomePattern; int[] allOutcomesPattern= new int[numOutcomes]; ! for (int oi = 0; oi < numOutcomes; oi++) { ! allOutcomesPattern[oi] = oi; } int numActiveOutcomes = 0; ! for (int pi = 0; pi < numPreds; pi++) { numActiveOutcomes = 0; ! if (useSimpleSmoothing) { numActiveOutcomes = numOutcomes; outcomePattern = allOutcomesPattern; } else { //determine active outcomes ! for (int oi = 0; oi < numOutcomes; oi++) { ! if (predCount[pi][oi] > 0) { ! activeOutcomes[numActiveOutcomes] = oi; numActiveOutcomes++; } *************** *** 259,274 **** } } ! params[PID] = new MutableContext(outcomePattern,new double[numActiveOutcomes]); ! modelExpects[PID] = new MutableContext(outcomePattern,new double[numActiveOutcomes]); ! observedExpects[PID] = new MutableContext(outcomePattern,new double[numActiveOutcomes]); for (int aoi=0;aoi<numActiveOutcomes;aoi++) { ! OID = outcomePattern[aoi]; ! params[PID].setParameter(aoi, 0.0); ! modelExpects[PID].setParameter(aoi, 0.0); ! if (predCount[PID][OID] > 0) { ! observedExpects[PID].setParameter(aoi, predCount[PID][OID]); } ! else if (_simpleSmoothing) { ! observedExpects[PID].setParameter(aoi,smoothingObservation); } } --- 284,299 ---- } } ! params[pi] = new MutableContext(outcomePattern,new double[numActiveOutcomes]); ! modelExpects[pi] = new MutableContext(outcomePattern,new double[numActiveOutcomes]); ! observedExpects[pi] = new MutableContext(outcomePattern,new double[numActiveOutcomes]); for (int aoi=0;aoi<numActiveOutcomes;aoi++) { ! int oi = outcomePattern[aoi]; ! params[pi].setParameter(aoi, 0.0); ! modelExpects[pi].setParameter(aoi, 0.0); ! if (predCount[pi][oi] > 0) { ! observedExpects[pi].setParameter(aoi, predCount[pi][oi]); } ! else if (useSimpleSmoothing) { ! observedExpects[pi].setParameter(aoi,smoothingObservation); } } *************** *** 276,289 **** // compute the expected value of correction ! if (_useSlackParameter) { int cfvalSum = 0; ! for (TID = 0; TID < numTokens; TID++) { ! for (int j = 0; j < contexts[TID].length; j++) { ! PID = contexts[TID][j]; ! if (!modelExpects[PID].contains(outcomes[TID])) { ! cfvalSum += numTimesEventsSeen[TID]; } } ! cfvalSum += (correctionConstant - contexts[TID].length) * numTimesEventsSeen[TID]; } if (cfvalSum == 0) { --- 301,314 ---- // compute the expected value of correction ! if (useSlackParameter) { int cfvalSum = 0; ! for (int ti = 0; ti < numTokens; ti++) { ! for (int j = 0; j < contexts[ti].length; j++) { ! int pi = contexts[ti][j]; ! if (!modelExpects[pi].contains(outcomes[ti])) { ! cfvalSum += numTimesEventsSeen[ti]; } } ! cfvalSum += (correctionConstant - contexts[ti].length) * numTimesEventsSeen[ti]; } if (cfvalSum == 0) { *************** *** 340,345 **** numTimesEventsSeen = null; contexts = null; ! } ! /* Compute one iteration of GIS and retutn log-likelihood.*/ private double nextIteration() { --- 365,397 ---- numTimesEventsSeen = null; contexts = null; ! } ! ! //modeled on implementation in Zhang Le's maxent kit ! private double gaussianUpdate(int predicate, int oid, int n, double correctionConstant) { ! double param = params[predicate].getParameters()[oid]; ! double x = 0.0; ! double x0 = 0.0; ! double f; ! double tmp; ! double fp; ! double modelValue = modelExpects[predicate].getParameters()[oid]; ! double observedValue = observedExpects[predicate].getParameters()[oid]; ! for (int i = 0; i < 50; i++) { ! tmp = modelValue * Math.exp(correctionConstant * x0); ! f = tmp + (param + x0) / sigma - observedValue; ! fp = tmp * correctionConstant + 1 / sigma; ! if (fp == 0) { ! break; ! } ! x = x0 - f / fp; ! if (Math.abs(x - x0) < 0.000001) { ! x0 = x; ! break; ! } ! x0 = x; ! } ! return x0; ! } ! /* Compute one iteration of GIS and retutn log-likelihood.*/ private double nextIteration() { *************** *** 350,373 **** int numEvents = 0; int numCorrect = 0; ! for (TID = 0; TID < numTokens; TID++) { ! // TID, modeldistribution and PID are globals used in ! // the updateModelExpects procedure. They need to be set. GISModel.eval(contexts[TID], modelDistribution, evalParams); for (int j = 0; j < contexts[TID].length; j++) { ! PID = contexts[TID][j]; ! int[] activeOutcomes = modelExpects[PID].getOutcomes(); for (int aoi=0;aoi<activeOutcomes.length;aoi++) { ! OID = activeOutcomes[aoi]; ! modelExpects[PID].updateParameter(aoi,modelDistribution[OID] * numTimesEventsSeen[TID]); } ! if (_useSlackParameter) { ! for (OID = 0; OID < numOutcomes; OID++) { ! if (!modelExpects[PID].contains(OID)) { ! CFMOD += modelDistribution[OID] * numTimesEventsSeen[TID]; } } } } ! if (_useSlackParameter) CFMOD += (evalParams.correctionConstant - contexts[TID].length) * numTimesEventsSeen[TID]; --- 402,424 ---- int numEvents = 0; int numCorrect = 0; ! for (int TID = 0; TID < numTokens; TID++) { ! prior.logPrior(modelDistribution,contexts[TID]); GISModel.eval(contexts[TID], modelDistribution, evalParams); for (int j = 0; j < contexts[TID].length; j++) { ! int pi = contexts[TID][j]; ! int[] activeOutcomes = modelExpects[pi].getOutcomes(); for (int aoi=0;aoi<activeOutcomes.length;aoi++) { ! int oi = activeOutcomes[aoi]; ! modelExpects[pi].updateParameter(aoi,modelDistribution[oi] * numTimesEventsSeen[TID]); } ! if (useSlackParameter) { ! for (int oi = 0; oi < numOutcomes; oi++) { ! if (!modelExpects[pi].contains(oi)) { ! CFMOD += modelDistribution[oi] * numTimesEventsSeen[TID]; } } } } ! if (useSlackParameter) CFMOD += (evalParams.correctionConstant - contexts[TID].length) * numTimesEventsSeen[TID]; *************** *** 376,382 **** if (printMessages) { int max = 0; ! for (OID = 1; OID < numOutcomes; OID++) { ! if (modelDistribution[OID] > modelDistribution[max]) { ! max = OID; } } --- 427,433 ---- if (printMessages) { int max = 0; ! for (int oi = 1; oi < numOutcomes; oi++) { ! if (modelDistribution[oi] > modelDistribution[max]) { ! max = oi; } } *************** *** 390,403 **** // compute the new parameter values ! for (PID = 0; PID < numPreds; PID++) { ! double[] observed = observedExpects[PID].getParameters(); ! double[] model = modelExpects[PID].getParameters(); ! int[] activeOutcomes = params[PID].getOutcomes(); for (int aoi=0;aoi<activeOutcomes.length;aoi++) { ! params[PID].updateParameter(aoi,(Math.log(observed[aoi])) - Math.log(model[aoi])); ! modelExpects[PID].setParameter(aoi,0.0); // re-initialize to 0.0's } } ! if (CFMOD > 0.0 && _useSlackParameter) evalParams.correctionParam += (cfObservedExpect - Math.log(CFMOD)); --- 441,459 ---- // compute the new parameter values ! for (int pi = 0; pi < numPreds; pi++) { ! double[] observed = observedExpects[pi].getParameters(); ! double[] model = modelExpects[pi].getParameters(); ! int[] activeOutcomes = params[pi].getOutcomes(); for (int aoi=0;aoi<activeOutcomes.length;aoi++) { ! if (useGaussianSmoothing) { ! params[pi].updateParameter(aoi,gaussianUpdate(pi,aoi,numEvents,evalParams.correctionConstant)); ! } ! else { ! params[pi].updateParameter(aoi,((Math.log(observed[aoi])) - Math.log(model[aoi]))); ! } ! modelExpects[pi].setParameter(aoi,0.0); // re-initialize to 0.0's } } ! if (CFMOD > 0.0 && useSlackParameter) evalParams.correctionParam += (cfObservedExpect - Math.log(CFMOD)); |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:52:22
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv22238/src/java/opennlp/maxent Modified Files: DataIndexer.java Log Message: fixed javadoc Index: DataIndexer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/DataIndexer.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** DataIndexer.java 10 May 2004 03:11:54 -0000 1.12 --- DataIndexer.java 9 Nov 2006 20:52:14 -0000 1.13 *************** *** 35,40 **** /** ! * Returns an array indicating the number of outcomes found with a particular event. ! * @return an array indexed by event index indicating the number of outcomes found with a particular event. */ public int[] getOutcomeList(); --- 35,40 ---- /** ! * Returns an array indicating the outcome index for each event. ! * @return an array indicating the outcome index for each event. */ public int[] getOutcomeList(); |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:52:19
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv22733/src/java/opennlp/maxent Modified Files: GISModel.java Log Message: upadted to use priors. Index: GISModel.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISModel.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** GISModel.java 8 Nov 2006 22:03:43 -0000 1.16 --- GISModel.java 9 Nov 2006 20:53:49 -0000 1.17 *************** *** 23,27 **** import java.text.DecimalFormat; - /** * A maximum entropy model which has been trained using the Generalized --- 23,26 ---- *************** *** 36,69 **** /** The names of the outcomes. */ private final String[] ocNames; - /** The number of outcomes. */ - //private final int numOutcomes; private DecimalFormat df; private EvalParameters evalParams; ! public GISModel (Context[] _params, ! String[] predLabels, ! String[] _ocNames, ! int _correctionConstant, ! double _correctionParam) { ! ! pmap = new TObjectIndexHashMap(predLabels.length); ! for (int i=0; i<predLabels.length; i++) ! pmap.put(predLabels[i], i); ! ! ocNames = _ocNames; ! evalParams = new EvalParameters(_params,_correctionParam,_correctionConstant,ocNames.length); } ! ! private static Context[] convertToContexts(TIntParamHashMap[] params) { ! Context[] contexts = new Context[params.length]; ! for (int pi=0;pi<params.length;pi++) { ! int[] activeOutcomes = params[pi].keys(); ! double[] activeParameters = new double[activeOutcomes.length]; ! for (int oi=0;oi<activeParameters.length;oi++) { ! activeParameters[oi] = params[pi].get(activeOutcomes[oi]); ! } ! contexts[pi] = new Context(activeOutcomes,activeParameters); } ! return contexts; } --- 35,72 ---- /** The names of the outcomes. */ private final String[] ocNames; private DecimalFormat df; private EvalParameters evalParams; + private Prior prior; + ! /** ! * Creates a new model with the specified parameters, outcome names, and predicate/feature labels. ! * @param params The parameters of the model. ! * @param predLabels The names of the predicates used in this model. ! * @param outcomeNames The names of the outcomes this model predicts. ! * @param correctionConstant The maximum number of active features which occur in an event. ! * @param correctionParam The parameter associated with the correction feature. ! */ ! public GISModel (Context[] params, String[] predLabels, String[] outcomeNames, int correctionConstant, double correctionParam) { ! this(params,predLabels,outcomeNames,correctionConstant,correctionParam, new UniformPrior(outcomeNames.length)); } ! ! /** ! * Creates a new model with the specified parameters, outcome names, and predicate/feature labels. ! * @param params The parameters of the model. ! * @param predLabels The names of the predicates used in this model. ! * @param outcomeNames The names of the outcomes this model predicts. ! * @param correctionConstant The maximum number of active features which occur in an event. ! * @param correctionParam The parameter associated with the correction feature. ! * @param prior The prior to be used with this model. ! */ ! public GISModel (Context[] params, String[] predLabels, String[] outcomeNames, int correctionConstant,double correctionParam, Prior prior) { ! this.pmap = new TObjectIndexHashMap(predLabels.length); ! for (int i=0; i<predLabels.length; i++) { ! pmap.put(predLabels[i], i); } ! this.ocNames = outcomeNames; ! this.evalParams = new EvalParameters(params,correctionParam,correctionConstant,ocNames.length); ! this.prior = prior; } *************** *** 89,93 **** * @param context The integer values of the predicates which have been observed at * the present decision point. ! * @param outsums This is where the distribution is stored. * @param model The set of parametes used in this computation. * @return The normalized probabilities for the outcomes given the --- 92,96 ---- * @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 *************** *** 97,109 **** * getOutcome(int i). */ ! public static double[] eval(int[] context, double[] outsums, EvalParameters model) { ! int numOutcomes = model.numOutcomes; Context[] params = model.params; ! double constant = model.correctionConstant; ! double constantInverse = model.constantInverse; ! double correctionParam = model.correctionParam; ! double iprob = model.iprob; ! for (int oid = 0; oid < numOutcomes; oid++) { ! outsums[oid] = iprob; model.numfeats[oid] = 0; } --- 100,106 ---- * getOutcome(int i). */ ! public static double[] eval(int[] context, double[] prior, EvalParameters model) { Context[] params = model.params; ! for (int oid = 0; oid < model.numOutcomes; oid++) { model.numfeats[oid] = 0; } *************** *** 118,135 **** int oid = activeOutcomes[j]; model.numfeats[oid]++; ! outsums[oid] += constantInverse * activeParameters[j]; } } } ! double SUM = 0.0; ! for (int oid = 0; oid < numOutcomes; oid++) { ! outsums[oid] = Math.exp(outsums[oid]+((1.0 - ((double) model.numfeats[oid] / constant)) * correctionParam)); ! SUM += outsums[oid]; } ! for (int oid = 0; oid < numOutcomes; oid++) ! outsums[oid] /= SUM; ! return outsums; } --- 115,138 ---- int oid = activeOutcomes[j]; model.numfeats[oid]++; ! prior[oid] += activeParameters[j]; } } } ! double normal = 0.0; ! for (int oid = 0; oid < model.numOutcomes; oid++) { ! if (model.correctionParam != 0) { ! prior[oid] = Math.exp(prior[oid]*model.constantInverse+((1.0 - ((double) model.numfeats[oid] / model.correctionConstant)) * model.correctionParam)); ! } ! else { ! prior[oid] = Math.exp(prior[oid]*model.constantInverse); ! } ! normal += prior[oid]; } ! for (int oid = 0; oid < model.numOutcomes; oid++) { ! prior[oid] /= normal; ! } ! return prior; } *************** *** 152,155 **** --- 155,159 ---- scontexts[i] = pmap.get(context[i]); } + prior.logPrior(outsums, scontexts); return GISModel.eval(scontexts,outsums,evalParams); } *************** *** 239,243 **** * which is returned by this method: * ! * <li>index 0: gnu.trove.TIntDoubleHashMap[] containing the model * parameters * <li>index 1: java.util.Map containing the mapping of model predicates --- 243,247 ---- * which is returned by this method: * ! * <li>index 0: opennlp.maxent.Context[] containing the model * parameters * <li>index 1: java.util.Map containing the mapping of model predicates *************** *** 327,331 **** this.constantInverse = 1.0 / correctionConstant; this.iprob = Math.log(1.0/numOutcomes); ! } ! } --- 331,334 ---- this.constantInverse = 1.0 / correctionConstant; this.iprob = Math.log(1.0/numOutcomes); ! } } |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:38:43
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv23739/src/java/opennlp/maxent Added Files: Prior.java Log Message: Interface to allow priors to be used. --- NEW FILE: Prior.java --- package opennlp.maxent; /** * This interface allows one to implement a prior distribution for use in * maximum entropy model training. * @author Tom Morton * */ public interface Prior { /** * Populates the specified array with the the log of the distribution for the specified context. * The returned array will be overwritten and needs to be re-initialized with every call to this method. * @param dist An array to be populated with the log of the prior distribution. * @param context The indices of the contextual predicates for an event. */ public void logPrior(double[] dist, int[] context); } |
From: Thomas M. <tsm...@us...> - 2006-11-13 18:38:42
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent/io In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv25048/src/java/opennlp/maxent/io Modified Files: GISModelWriter.java Log Message: fixed some indentation. Index: GISModelWriter.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/io/GISModelWriter.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** GISModelWriter.java 6 Oct 2005 11:04:16 -0000 1.6 --- GISModelWriter.java 9 Nov 2006 20:58:19 -0000 1.7 *************** *** 71,117 **** */ public void persist() throws IOException { ! ! // the type of model (GIS) ! writeUTF("GIS"); ! ! // the value of the correction constant ! writeInt(CORRECTION_CONSTANT); ! ! // the value of the correction constant ! writeDouble(CORRECTION_PARAM); ! ! // the mapping from outcomes to their integer indexes ! writeInt(OUTCOME_LABELS.length); ! ! for (int i=0; i<OUTCOME_LABELS.length; i++) ! writeUTF(OUTCOME_LABELS[i]); ! ! // the mapping from predicates to the outcomes they contributed to. ! // The sorting is done so that we actually can write this out more ! // compactly than as the entire list. ! ComparablePredicate[] sorted = sortValues(); ! List compressed = compressOutcomes(sorted); ! ! writeInt(compressed.size()); ! ! for (int i=0; i<compressed.size(); i++) { ! List a = (List)compressed.get(i); ! writeUTF(a.size() ! + ((ComparablePredicate)a.get(0)).toString()); ! } ! ! // the mapping from predicate names to their integer indexes ! writeInt(PARAMS.length); ! ! String pred; ! for (int i=0; i<sorted.length; i++) ! writeUTF(sorted[i].name); ! ! // write out the parameters ! for (int i=0; i<sorted.length; i++) ! for (int j=0; j<sorted[i].params.length; j++) ! writeDouble(sorted[i].params[j]); ! ! close(); } --- 71,116 ---- */ public void persist() throws IOException { ! ! // the type of model (GIS) ! writeUTF("GIS"); ! ! // the value of the correction constant ! writeInt(CORRECTION_CONSTANT); ! ! // the value of the correction constant ! writeDouble(CORRECTION_PARAM); ! ! // the mapping from outcomes to their integer indexes ! writeInt(OUTCOME_LABELS.length); ! ! for (int i=0; i<OUTCOME_LABELS.length; i++) ! writeUTF(OUTCOME_LABELS[i]); ! ! // the mapping from predicates to the outcomes they contributed to. ! // The sorting is done so that we actually can write this out more ! // compactly than as the entire list. ! ComparablePredicate[] sorted = sortValues(); ! List compressed = compressOutcomes(sorted); ! ! writeInt(compressed.size()); ! ! for (int i=0; i<compressed.size(); i++) { ! List a = (List)compressed.get(i); ! writeUTF(a.size() ! + ((ComparablePredicate)a.get(0)).toString()); ! } ! ! // the mapping from predicate names to their integer indexes ! writeInt(PARAMS.length); ! ! for (int i=0; i<sorted.length; i++) ! writeUTF(sorted[i].name); ! ! // write out the parameters ! for (int i=0; i<sorted.length; i++) ! for (int j=0; j<sorted[i].params.length; j++) ! writeDouble(sorted[i].params[j]); ! ! close(); } *************** *** 152,172 **** protected List compressOutcomes (ComparablePredicate[] sorted) { ! ! ComparablePredicate cp = sorted[0]; ! List outcomePatterns = new ArrayList(); ! List newGroup = new ArrayList(); ! for (int i=0; i<sorted.length; i++) { ! if (cp.compareTo(sorted[i]) == 0) { ! newGroup.add(sorted[i]); ! } else { ! cp = sorted[i]; ! outcomePatterns.add(newGroup); ! newGroup = new ArrayList(); ! newGroup.add(sorted[i]); ! } ! } ! outcomePatterns.add(newGroup); ! ! return outcomePatterns; } --- 151,169 ---- protected List compressOutcomes (ComparablePredicate[] sorted) { ! ComparablePredicate cp = sorted[0]; ! List outcomePatterns = new ArrayList(); ! List newGroup = new ArrayList(); ! for (int i=0; i<sorted.length; i++) { ! if (cp.compareTo(sorted[i]) == 0) { ! newGroup.add(sorted[i]); ! } else { ! cp = sorted[i]; ! outcomePatterns.add(newGroup); ! newGroup = new ArrayList(); ! newGroup.add(sorted[i]); ! } ! } ! outcomePatterns.add(newGroup); ! return outcomePatterns; } |
From: Thomas M. <tsm...@us...> - 2006-11-08 23:19:00
|
Update of /cvsroot/maxent/maxent/src/java/opennlp/maxent In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv15186/src/java/opennlp/maxent Modified Files: GISTrainer.java GISModel.java Log Message: Refactored parameter data structures so that static eval method could be used in both training and testing. Index: GISTrainer.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISTrainer.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** GISTrainer.java 24 Nov 2005 02:39:47 -0000 1.19 --- GISTrainer.java 8 Nov 2006 22:03:43 -0000 1.20 *************** *** 98,106 **** /** The maximum number of feattures fired in an event. Usually refered to a C.*/ ! private int constant; /** Stores inverse of constant, 1/C. */ ! private double constantInverse; /** The correction parameter of the model. */ ! private double correctionParam; /** Observed expectation of correction feature. */ private double cfObservedExpect; --- 98,106 ---- /** The maximum number of feattures fired in an event. Usually refered to a C.*/ ! //private int constant; /** Stores inverse of constant, 1/C. */ ! //private double constantInverse; /** The correction parameter of the model. */ ! //private double correctionParam; /** Observed expectation of correction feature. */ private double cfObservedExpect; *************** *** 119,122 **** --- 119,123 ---- double iprob; + EvalParameters evalParams; /** * Creates a new <code>GISTrainer</code> instance which does *************** *** 185,196 **** // determine the correction constant and its inverse ! constant = contexts[0].length; for (TID = 1; TID < contexts.length; TID++) { ! if (contexts[TID].length > constant) { ! constant = contexts[TID].length; } } - constantInverse = 1.0 / constant; - display("done.\n"); --- 186,195 ---- // determine the correction constant and its inverse ! int correctionConstant = contexts[0].length; for (TID = 1; TID < contexts.length; TID++) { ! if (contexts[TID].length > correctionConstant) { ! correctionConstant = contexts[TID].length; } } display("done.\n"); *************** *** 229,233 **** modelExpects = new MutableContext[numPreds]; observedExpects = new MutableContext[numPreds]; ! int[] activeOutcomes = new int[numOutcomes]; int[] outcomePattern; --- 228,232 ---- modelExpects = new MutableContext[numPreds]; observedExpects = new MutableContext[numPreds]; ! evalParams = new EvalParameters(params,0,correctionConstant,numOutcomes); int[] activeOutcomes = new int[numOutcomes]; int[] outcomePattern; *************** *** 286,290 **** } } ! cfvalSum += (constant - contexts[TID].length) * numTimesEventsSeen[TID]; } if (cfvalSum == 0) { --- 285,289 ---- } } ! cfvalSum += (correctionConstant - contexts[TID].length) * numTimesEventsSeen[TID]; } if (cfvalSum == 0) { *************** *** 294,299 **** cfObservedExpect = Math.log(cfvalSum); } - - correctionParam = 0.0; } predCount = null; // don't need it anymore --- 293,296 ---- *************** *** 309,313 **** /*************** Create and return the model ******************/ ! return new GISModel(params, predLabels, outcomeLabels, constant, correctionParam); } --- 306,310 ---- /*************** Create and return the model ******************/ ! return new GISModel(params, predLabels, outcomeLabels, correctionConstant, evalParams.correctionParam); } *************** *** 343,390 **** numTimesEventsSeen = null; contexts = null; ! } ! ! /** ! * Use this model to evaluate a context and populate the specified outsums array with the ! * likelihood of each outcome given that context. ! * ! * @param context The integers of the predicates which have been ! * observed at the present decision point. ! */ ! public void eval(int[] context, double[] outsums) { ! for (int oid = 0; oid < numOutcomes; oid++) { ! outsums[oid] = iprob; ! numfeats[oid] = 0; ! } ! int[] activeOutcomes; ! double[] activeParameters; ! for (int i = 0; i < context.length; i++) { ! Context predParams = params[context[i]]; ! activeOutcomes = predParams.getOutcomes(); ! activeParameters = predParams.getParameters(); ! for (int j = 0; j < activeOutcomes.length; j++) { ! int oid = activeOutcomes[j]; ! numfeats[oid]++; ! outsums[oid] += constantInverse * activeParameters[j]; ! } ! } ! ! double SUM = 0.0; ! for (int oid = 0; oid < numOutcomes; oid++) { ! if (_useSlackParameter) { ! outsums[oid] = Math.exp(outsums[oid]+((1.0 - ((double) numfeats[oid] / constant)) * correctionParam)); ! } ! else { ! outsums[oid] = Math.exp(outsums[oid]); ! } ! SUM += outsums[oid]; ! } ! ! for (int oid = 0; oid < numOutcomes; oid++) ! outsums[oid] /= SUM; ! ! } ! ! /* Compute one iteration of GIS and retutn log-likelihood.*/ --- 340,344 ---- numTimesEventsSeen = null; contexts = null; ! } /* Compute one iteration of GIS and retutn log-likelihood.*/ *************** *** 399,403 **** // TID, modeldistribution and PID are globals used in // the updateModelExpects procedure. They need to be set. ! eval(contexts[TID], modelDistribution); for (int j = 0; j < contexts[TID].length; j++) { PID = contexts[TID][j]; --- 353,357 ---- // TID, modeldistribution and PID are globals used in // the updateModelExpects procedure. They need to be set. ! GISModel.eval(contexts[TID], modelDistribution, evalParams); for (int j = 0; j < contexts[TID].length; j++) { PID = contexts[TID][j]; *************** *** 416,420 **** } if (_useSlackParameter) ! CFMOD += (constant - contexts[TID].length) * numTimesEventsSeen[TID]; loglikelihood += Math.log(modelDistribution[outcomes[TID]]) * numTimesEventsSeen[TID]; --- 370,374 ---- } if (_useSlackParameter) ! CFMOD += (evalParams.correctionConstant - contexts[TID].length) * numTimesEventsSeen[TID]; loglikelihood += Math.log(modelDistribution[outcomes[TID]]) * numTimesEventsSeen[TID]; *************** *** 446,450 **** } if (CFMOD > 0.0 && _useSlackParameter) ! correctionParam += (cfObservedExpect - Math.log(CFMOD)); display(". loglikelihood=" + loglikelihood + "\t" + ((double) numCorrect / numEvents) + "\n"); --- 400,404 ---- } if (CFMOD > 0.0 && _useSlackParameter) ! evalParams.correctionParam += (cfObservedExpect - Math.log(CFMOD)); display(". loglikelihood=" + loglikelihood + "\t" + ((double) numCorrect / numEvents) + "\n"); Index: GISModel.java =================================================================== RCS file: /cvsroot/maxent/maxent/src/java/opennlp/maxent/GISModel.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** GISModel.java 24 Nov 2005 02:39:07 -0000 1.15 --- GISModel.java 8 Nov 2006 22:03:43 -0000 1.16 *************** *** 32,52 **** */ public final class GISModel implements MaxentModel { - /** Mapping between outcomes and paramater values for each context. - * The integer representation of the context can be found using <code>pmap</code>.*/ - private final Context[] params; /** Maping between predicates/contexts and an integer representing them. */ private final TObjectIndexHashMap pmap; /** The names of the outcomes. */ private final String[] ocNames; - private final double correctionConstant; - private final double correctionParam; /** The number of outcomes. */ ! private final int numOutcomes; ! private final double iprob; ! private final double fval; private DecimalFormat df; - private int[] numfeats; - public GISModel (Context[] _params, String[] predLabels, --- 32,44 ---- */ public final class GISModel implements MaxentModel { /** Maping between predicates/contexts and an integer representing them. */ private final TObjectIndexHashMap pmap; /** The names of the outcomes. */ private final String[] ocNames; /** The number of outcomes. */ ! //private final int numOutcomes; private DecimalFormat df; + private EvalParameters evalParams; public GISModel (Context[] _params, String[] predLabels, *************** *** 59,83 **** pmap.put(predLabels[i], i); - params = _params; ocNames = _ocNames; ! correctionConstant = (double)_correctionConstant; ! correctionParam = _correctionParam; ! ! numOutcomes = ocNames.length; ! iprob = Math.log(1.0/numOutcomes); ! fval = 1.0/correctionConstant; ! numfeats = new int[numOutcomes]; ! } ! ! /* ! public GISModel (TIntParamHashMap[] _params, ! String[] predLabels, ! String[] _ocNames, ! int _correctionConstant, ! double _correctionParam) { ! this(convertToContexts(_params),predLabels,_ocNames,_correctionConstant,_correctionParam); } ! */ ! private static Context[] convertToContexts(TIntParamHashMap[] params) { Context[] contexts = new Context[params.length]; --- 51,58 ---- pmap.put(predLabels[i], i); ocNames = _ocNames; ! evalParams = new EvalParameters(_params,_correctionParam,_correctionConstant,ocNames.length); } ! private static Context[] convertToContexts(TIntParamHashMap[] params) { Context[] contexts = new Context[params.length]; *************** *** 106,110 **** */ public final double[] eval(String[] context) { ! return(eval(context,new double[numOutcomes])); } --- 81,135 ---- */ public final double[] eval(String[] context) { ! return(eval(context,new double[evalParams.numOutcomes])); ! } ! ! /** ! * 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 outsums This is where the distribution is stored. ! * @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[] outsums, EvalParameters model) { ! int numOutcomes = model.numOutcomes; ! Context[] params = model.params; ! double constant = model.correctionConstant; ! double constantInverse = model.constantInverse; ! double correctionParam = model.correctionParam; ! double iprob = model.iprob; ! for (int oid = 0; oid < numOutcomes; oid++) { ! outsums[oid] = iprob; ! model.numfeats[oid] = 0; ! } ! int[] activeOutcomes; ! double[] activeParameters; ! for (int i = 0; i < context.length; i++) { ! if (context[i] >= 0) { ! Context predParams = params[context[i]]; ! activeOutcomes = predParams.getOutcomes(); ! activeParameters = predParams.getParameters(); ! for (int j = 0; j < activeOutcomes.length; j++) { ! int oid = activeOutcomes[j]; ! model.numfeats[oid]++; ! outsums[oid] += constantInverse * activeParameters[j]; ! } ! } ! } ! ! double SUM = 0.0; ! for (int oid = 0; oid < numOutcomes; oid++) { ! outsums[oid] = Math.exp(outsums[oid]+((1.0 - ((double) model.numfeats[oid] / constant)) * correctionParam)); ! SUM += outsums[oid]; ! } ! ! for (int oid = 0; oid < numOutcomes; oid++) ! outsums[oid] /= SUM; ! return outsums; } *************** *** 123,158 **** */ public final double[] eval(String[] context, double[] outsums) { ! int[] activeOutcomes; ! double[] activeParameters; ! for (int oid=0; oid<numOutcomes; oid++) { ! outsums[oid] = iprob; ! numfeats[oid] = 0; ! } ! for (int i=0; i<context.length; i++) { ! int contextIndex = pmap.get(context[i]); ! if (contextIndex >= 0) { ! Context predParams = params[contextIndex]; ! activeOutcomes = predParams.getOutcomes(); ! activeParameters = predParams.getParameters(); ! for (int j=0; j<activeOutcomes.length; j++) { ! int oid = activeOutcomes[j]; ! numfeats[oid]++; ! outsums[oid] += activeParameters[j]; ! } ! } ! } ! ! double normal = 0.0; ! for (int oid=0; oid<numOutcomes; oid++) { ! outsums[oid] = Math.exp((outsums[oid]*fval) ! + ((1.0 -(numfeats[oid]/correctionConstant)) ! * correctionParam)); ! normal += outsums[oid]; ! } ! ! for (int oid=0; oid<numOutcomes; oid++) ! outsums[oid] /= normal; ! ! return outsums; } --- 148,156 ---- */ public final double[] eval(String[] context, double[] outsums) { ! int[] scontexts = new int[context.length]; ! for (int i=0; i<context.length; i++) { ! scontexts[i] = pmap.get(context[i]); ! } ! return GISModel.eval(scontexts,outsums,evalParams); } *************** *** 231,235 **** public int getNumOutcomes() { ! return(numOutcomes); } --- 229,233 ---- public int getNumOutcomes() { ! return(evalParams.numOutcomes); } *************** *** 257,265 **** public final Object[] getDataStructures () { Object[] data = new Object[5]; ! data[0] = params; data[1] = pmap; data[2] = ocNames; ! data[3] = new Integer((int)correctionConstant); ! data[4] = new Double(correctionParam); return data; } --- 255,263 ---- public final Object[] getDataStructures () { Object[] data = new Object[5]; ! data[0] = evalParams.params; data[1] = pmap; data[2] = ocNames; ! data[3] = new Integer((int)evalParams.correctionConstant); ! data[4] = new Double(evalParams.correctionParam); return data; } *************** *** 283,284 **** --- 281,331 ---- } } + + /** + * This class encapsulates the varibales used in producing probabilities from a model + * and facilitaes passing these variables to the eval method. Variables are declared + * non-private so that they may be accessed and updated without a method call for efficiency + * reasons. + * @author Tom Morton + * + */ + class EvalParameters { + + /** Mapping between outcomes and paramater values for each context. + * The integer representation of the context can be found using <code>pmap</code>.*/ + Context[] params; + /** The number of outcomes being predicted. */ + final int numOutcomes; + /** The maximum number of feattures fired in an event. Usually refered to a C. + * This is used to normalize the number of features which occur in an event. */ + double correctionConstant; + + /** Stores inverse of the correction constant, 1/C. */ + final double constantInverse; + /** The correction parameter of the model. */ + double correctionParam; + /** Log of 1/C; initial value of probabilities. */ + final double iprob; + + /** Stores the number of features that get fired for each outcome in an event. + * This is over-written for each event evaluation, but declared once for efficiency.*/ + int[] numfeats; + + /** + * Creates a set of paramters which can be evaulated with the eval method. + * @param params The parameters of the model. + * @param correctionParam The correction paramter. + * @param correctionConstant The correction constant. + * @param numOutcomes The number of outcomes. + */ + public EvalParameters(Context[] params, double correctionParam, double correctionConstant, int numOutcomes) { + this.params = params; + this.correctionParam = correctionParam; + this.numOutcomes = numOutcomes; + this.numfeats = new int[numOutcomes]; + this.correctionConstant = correctionConstant; + this.constantInverse = 1.0 / correctionConstant; + this.iprob = Math.log(1.0/numOutcomes); + } + + } |