Download LBJava User Guide - Cognitive Computation Group

Transcript
3.3.3
Learning
As mentioned above, most classifiers are learned during the LBJ phase of compilation (see
Section 3.4 below). In addition, a classifier generated by the LBJ compiler can also continue
learning from labeled examples in the Java application. Since NewsgroupClassifier takes a
Post object as input, we merely have to get our hands on such an object, stick the label in the
newsgroup field (since that’s where the NewsgroupLabel classifier will look for it), and pass it
to the classifier’s learn(Object) method.
Now that we know how to get our classifier to learn, let’s see how to make it forget.
The contents of a classifier can be completely cleared out by calling the forget() method.
After this method is called, the classifier returns to the state it was in before it observed any
training examples. One reason to forget everything a classifier has learned is to try new learning
algorithm parameters (e.g. learning rates, thresholds, etc.). All LBJ learning algorithms provide
an inner class named Parameters that contains default settings for all their parameters. Simply
instantiate such an object, overwrite the parameters that need to be updated, and call the
setParameters(Parameters) method. For example:
ngClassifier.forget();
SparseAveragedPerceptron.Parameters ltuParameters =
new SparseAveragedPerceptron.Parameters();
ltuParameters.thickness = 12;
NewsgroupClassifier.Parameters parameters =
new NewsgroupClassifier.Parameters();
parameters.baseLTU = new SparseAveragedPerceptron(ltuParameters);
ngClassifier.setParameters(parameters);
This particular example is complicated by the fact that our newsgroup classifier is learned
using SparseNetworkLearner, an algorithm that uses another learning algorithm with its own
parameters as a subroutine. But the technique is the same. At this point, the classifier is
re-initialized with a new thickness setting and is ready for new training examples.
3.3.4
Saving Your Work
If we’ve done any forget()ing and/or learn()ing within our Java application, we’ll probably
be interested in saving what we learned at some point. No problem; simply call the save()
method.
classifier.save();
This operation overwrites the model and lexicon files that were originally generated by the
LBJ compiler. A model file stores the values of the learned parameters (not to be confused
with the manually set learning algorithm parameters mentioned above). A lexicon file stores
the classifier’s feature index, used for quick access to the learnable parameters when training for
multiple rounds. These files are written by the LBJ compiler and by the save() method (though
13