Learning Kernels Quickstart

Here we give some examples showing how to automatically create custom kernels using data. The kernels are generally created from a combination of base kernels, which can be specified by the user.

The examples here will use the electronics category from the sentiment analysis dataset of Blitzer et al., which we include with precomputed word level ngram features and binary as well as regression labels (1-5 stars). The data is arranged in LIBSVM format, which is compatible with all the learning algorithms used in these examples. The results shown here should be easily reproducible and serve as a good first exercise.

Note that these exercises have been constructed the mechanics behind using the automatic kernel selection tools. The kernels and parameters and used here are NOT necessarily the best for the one dataset used here.

Feature Weighted Kernels

The examples below consider the case when each base kernel corresponds to a single features. Such a set of base kernels occurs naturally when, for example, learning rational kernels as explained in Cortes et al. (MLSP 2008).

Correlation Kernel Example: The correlation based kernel weights each input feature by a quantity proportional to its correlation with the training labels. The following command will generate weighted features:

The --features flag forces the output of explicit feature vectors, rather than the kernel matrix, and the --sparse flag forces the use of sparse data-structure, which are both desirable in this case since the ngram-features are sparse. The --num_train flag indicates that the kernel selection algorithm should use only the first 1000 data-points for training, and thus allows us to use the remaining points as a holdout set for evaluating performance. Regularization on the kernel is imposed via the --ker_reg flag, which in the case of correlation kernels limits the kernel trace. Finally, the --lk_alg flag selects which type of kernel selection algorithm is used. The first argument indicates the input dataset and the second argument indicates the output file.

The weighted features can then be used to train and test an svm model via libsvm or liblinear:

L2 Regularized Linear Combination: Here we optimally weight the input features in order to maximize the kernel ridge regression (KRR) objective, subject to the L2 regularization constraint: ||mu - mu0|| < ker_reg||, where mu is the vector of squared weights and mu0 and ker_reg are user specified arguments.

The algorithm will iterate until the tolerance, which is set by the --tol flag, or maximum number of iterations is met. In this case mu0 is equal to zero (the default) and ker_reg is specified by the second argument to the function. Since this selection is algorithm specific, we should also specify the regularization parameter we will use in the second step via the --alg_reg flag. The --offset flag adds the constant indicated offset to the dataset input if one is not already included. Finally, the --tol flag indicates at what precision the iterative method should stop.

We then train and test using kernel ridge regression (KRR), with input and output arguments that have been made to closely resemble libsvm. One main difference is that the user must specify to use sparse data-structures. If the data is dense, it is better to use highly efficient dense blas routines instead by omitting the --sparse flag. To see a full list of command line arguments, run krr-train without any parameters.

Kernel Combinations with Explicit Features

Here we consider the case of combining several general base kernels that admit explicit feature mappings. In the case that these features are sparse, for example, we are able to very efficiently compute combinations in high dimensional features spaces.

In this example we find the best linear combination of 5 character-level ngram kernels (1-gram, 2-gram, ..., 5-gram) with respect to the SVM objective.

Here the argument elec.list is a file with the paths to each basekernel written on a separate line, and the combined kernel is written to the file elec.comb. The flag --alg_reg indicates the regularization parameter that will be used with SVM.

This will produce a kernel with many features, but which are sparse, thus liblinear is a good choice for training a model:

General Kernel Combinations

The final example listed here is regarding general combinations of kernels, where we combine the kernel matrices of the 5 ngram kernels. Of course, in practice this general kernel combination should be used when easy to represent explicit feature mapping are not available, such as with Guassian kernels.