In this example, we see how to instantiate a Partial Tree Kernel, and use it on a Question Classification dataset. The PTK function will compute a similarity measure diretly on a Tree Representation of an example. For example, the second representation in the next code snippet is a representation of a tree with round brackets. Note that, the boundaries for a Tree Representation is |BT| |ET|.
The :tree (and :bow on the vector representation) are used in KeLP to assign a label to each representation. During the kernel definition phase, these labels are used to indicate which representation must be used during the kernel computation.
1 |
NUM |BV:bow|8263:1.0 6890:1.0 2041:1.0 276:1.0 95:1.0 38:1.0 33:1.0 4:1.0 |EV| |BT:tree|(ROOT(PRD(WRB(how::w))(AMOD(RB(far::r))))(VBZ(be::v))(SBJ(PRP(it::p)))(TMP(IN(from::i))(PMOD(NNP(denver::n)))(PMOD(TO(to::t))(PMOD(NNP(aspen::n))))))|ET| |
The following code, reads a dataset compliant with these representations, instantiate a Partial Tree Kernel with some parameters (refers to the documentation for details).
1 2 3 4 5 6 7 8 9 10 |
SimpleDataset trainingSet = new SimpleDataset(); trainingSet.populate("qcBOWTREE.train.txt"); SimpleDataset testSet = new SimpleDataset(); testSet.populate("qcBOWTREE.test.txt"); float c = 1.0f; // Define a new Partial Tree Kernel Kernel kernel = new PartialTreeKernel(0.4f, 0.4f, 1, "tree"); // get the labels on the training set List<Label> classes = trainingSet.getClassificationLabels(); </label> |
In this example, the learning algorithm used is a SVM implementaion based on the C-SVM implementation of LibSVM.
1 2 3 4 5 |
// Instantiate a prototype for a binary svm solver BinaryCSvmClassification svmSolver = new BinaryCSvmClassification(); svmSolver.setKernel(kernel); svmSolver.setCp(c); svmSolver.setCn(c); |
Here, we introduce also the concept of multi-classification. A One-vs-All classifier is instantiated. It must know what is the base algorithm to be applied, and what are the labels to be learned.
The OneVsAllLearning class will be responsible to “copy” the base algorithms, and to perform the learning according to the One-Vs-All strategy.
1 2 3 4 5 |
// Instantiate a OneVsAll classifier OneVsAllLearning ovaLearner = new OneVsAllLearning(); ovaLearner.setBaseAlgorithm(svmSolver); ovaLearner.setLabels(labels); ovaLearner.learn(trainingSet); |
Then, a PredictionFunction can be obtained, in particular a OneVsAllClassifier object.
1 2 3 4 5 6 7 8 9 10 11 12 |
OneVsAllClassifier f = ovaLearner.getPredictionFunction(); // Do some test int correct = 0; int howmany = testSet.getNumberOfExamples(); for (Example e: testSet.getExamples()) { OneVsAllClassificationOutput predict = f.predict(e); // the OneVsAllClassifer returns a List of predicted Labels, where the first is the argmax if (e.isExampleOf(predict.getPredictedClasses().get(0))) correct++; } float accuracy = (float)correct/(float)howmany; |