Libsvm
precomputed kernels
machine learning
support vector machines
kernel methods

Libsvm precomputed kernels

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

text
1 0:1 1:1.0 2:0.3 3:-0.2
-1 0:2 1:0.3 2:1.0 3:0.1
1 0:3 1:-0.2 2:0.1 3:1.0

Interpretation:

  • the first row is sample 1
  • '1:1.0 is K(x1, x1)'
  • '2:0.3 is K(x1, x2)'
  • '3:-0.2 is K(x1, x3)'

The test file has the same idea, except each row contains kernel values between one test sample and every training sample.

text
1 0:1 1:0.8 2:0.1 3:-0.1
-1 0:2 1:0.2 2:0.9 3:0.0

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:

bash
svm-train -t 4 train.kernel
svm-predict test.kernel train.kernel.model predictions.txt

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.

python
1import numpy as np
2
3X_train = np.array([
4    [1.0, 0.0],
5    [0.0, 1.0],
6    [1.0, 1.0],
7])
8y_train = np.array([1, -1, 1])
9
10X_test = np.array([
11    [1.0, 0.5],
12    [0.2, 1.0],
13])
14y_test = np.array([1, -1])
15
16def linear_kernel(a, b):
17    return a @ b.T
18
19def write_precomputed(path, labels, kernel_matrix):
20    with open(path, "w", encoding="utf-8") as f:
21        for row_id, (label, row) in enumerate(zip(labels, kernel_matrix), start=1):
22            parts = [str(label), f"0:{row_id}"]
23            parts.extend(f"{j}:{value}" for j, value in enumerate(row, start=1))
24            f.write(" ".join(parts) + "\n")
25
26K_train = linear_kernel(X_train, X_train)
27K_test = linear_kernel(X_test, X_train)
28
29write_precomputed("train.kernel", y_train, K_train)
30write_precomputed("test.kernel", y_test, K_test)

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 4 and 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.

Course illustration
Course illustration

All Rights Reserved.