Caching is an important feature of KeLP. In most of the cases, kernel computation between two examples are useful in different step of the learning process. If one caches this computation, the learning algorithm can be quicker, as computation already done can be accessed directly from a cache.
For this reason, KeLP implements two types of caching. One is the norm cache, that is useful to cache the values of the norm in the kernel space. The other is a kernel cache, that is the storage of the kernel computations.
In the following snippet, an example on how to use these two caches is provided. It refers to the Tree Kernels dataset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Kernel linear = new LinearKernel("bow"); FixIndexSquaredNormCache ncache = new FixIndexSquaredNormCache(6000); linear.setNormCache(ncache); linear = new NormalizationKernel(linear); Kernel ptk = new PartialTreeKernel("tree"); FixIndexSquaredNormCache ncache1 = new FixIndexSquaredNormCache(6000); ptk.setNormCache(ncache1); ptk = new NormalizationKernel(ptk); LinearKernelCombination linearCombination = new LinearKernelCombination(); linearCombination.addKernel(1.0f, linear); linearCombination.addKernel(1.0f, ptk); linearCombination.normalizeWeights(); KernelCache cache = new FixIndexKernelCache(6000); linearCombination.setKernelCache(cache); |
There are some general guidelines when using caches in KeLP.
When using kernel combination, the kernel cache is useful only on the outer kernels, that is the combination.
Norm cache are useful only when the norm in the kernel space is necessary. There are two cases when the norm cache can be beneficial. The first is when using a RBF kernel.
The other case is when one wants to normalize a kernel function, that is when using a NormalizationKernel. In this case, the norm cache must be assigned to the inner kernel of a NormalizationKernel, as reported in the previous snippet.