Skip to content

Kernel HowTo

davidpicard edited this page May 25, 2012 · 1 revision

This page shows how to create a new Kernel class. Given a sample of generic type T, the library provides a parent class for all kernel functions:

public abstract class Kernel<T> implements Serializable {

		/** compute the kernel similarity between two elements
		 * of input space
		 */
		public abstract double valueOf(T t1, T t2);
	}

To create a new kernel, one has basically to extend this class and implement the valueOf() method. For example, the source code below corresponds to a Gaussian kernel on bags of vectors of double:

public class DoubleBagGaussian extends Kernel<List<double[]>> {
	@Override
	public double valueOf(List<double[]> l1, List<double[]> l2) {
		double sum = 0.;
		for(double[] t1 : l1)
		  for(double[] t2 : l2) {
    		    int min = Math.min(t1.length, t2.length);
    		    double d = 0.;
		    for(int i = 0 ; i < min ; i++) 
    			d += (t2[i]-t1[i])*(t2[i]-t1[i]);
    		    sum += Math.exp(-d/2.);
		}
		return sum;
	}
}

Currently, JKernelMachines provides the following kernels:

  • Standard kernels on vector (Linear, Gaussian, Triangle) for different types of vectors (int, float, double)
  • A wide variety of Gaussian kernels (Gaussian $\chi^2$ for distributions, subset of selected axes, etc)
  • Combination of kernels (weighted sum and weighted product of generic minor kernels)
  • Generic kernels on lists (ordered or unordered, weighted or not)
  • Kernels defined by a custom Gram matrix as well as kernels with various caching strategies

Clone this wiki locally