Libsvm precomputed kernels
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
LIBSVM supports precomputed kernels for cases where you want to supply the kernel matrix yourself instead of asking LIBSVM to evaluate a built-in kernel such as linear, polynomial, or RBF. This is useful when your similarity function is custom, expensive to compute, or defined outside the usual vector-space kernels.
The main challenge is formatting the training and test data exactly the way LIBSVM expects. Once the matrix layout is right, using a precomputed kernel is straightforward.
When Precomputed Kernels Make Sense
A precomputed kernel is appropriate when you already know the pairwise similarity between samples. Typical examples include:
- graph kernels
- string kernels
- alignment-based similarities
- kernels computed in another library or another language
In these cases, LIBSVM does not need the original feature vectors. It only needs the kernel values between each sample and the training set.
The Required LIBSVM Format
When using a precomputed kernel, train LIBSVM with -t 4. Each row must begin with the label and then a special 0: field containing the row id, usually a 1-based serial number.
A tiny training file for three samples looks like this:
Interpretation:
- the first row is sample
1 - '
1:1.0isK(x1, x1)' - '
2:0.3isK(x1, x2)' - '
3:-0.2isK(x1, x3)'
The test file has the same idea, except each row contains kernel values between one test sample and every training sample.
The important rule is that columns 1..n correspond to the training samples in the exact same order used during training.
Training and Prediction Commands
Once the files are prepared, train and predict like this:
The -t 4 flag tells LIBSVM that the input already represents kernel values. If you forget that flag, LIBSVM will interpret the matrix as ordinary sparse features and the model will be meaningless.
Building a Precomputed Kernel File in Python
The easiest way to avoid formatting mistakes is to generate the file programmatically. Here is a runnable Python example that computes a linear kernel and writes it in LIBSVM's precomputed format.
This example uses a linear kernel only to demonstrate the format. In a real application, the kernel function could be any similarity function that produces a valid kernel matrix.
Practical Constraints
Precomputed kernels trade flexibility for memory usage. An n x n training matrix grows quadratically, so large training sets can become expensive to store and compute.
Also remember that SVM theory assumes the kernel behaves like a valid kernel matrix. If the matrix is noisy or not positive semidefinite, model behavior can be unstable or poor even though the file format is technically correct.
Common Pitfalls
- Forgetting
-t 4, which makes LIBSVM parse the matrix as normal features. - Misordering the training samples so the test kernel columns no longer match the training kernel columns.
- Omitting the special
0:row id field that LIBSVM expects for precomputed kernels. - Using a huge kernel matrix without considering the quadratic memory cost.
- Supplying an arbitrary similarity matrix that is not a sensible kernel for an SVM.
Summary
- Use precomputed kernels in LIBSVM when you want to supply similarity values directly.
- Train with
svm-train -t 4and keep the training-sample order fixed. - Each row must include the label, a
0:row id, and kernel values against the training set. - Generate the files programmatically when possible to avoid formatting errors.
- Precomputed kernels are powerful, but they can be memory-heavy and depend on kernel quality.
Related reading
- libsvm Shrinking Heuristics
- libsvm with precomputed kernel How do I compute the classification scores?
- Lightgbm classifier with gpu
- LightGBM train vs update vs refit
- Lime vs TreeInterpreter for interpreting decision tree
- Limit number of cores used in Keras
- Limit Tensorflow CPU and Memory usage
- Linear algebra application in Machine Learning
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.