[][src]Crate ffsvm

FFSVM stands for "Really Fast Support Vector Machine", a libSVM compatible classifier. It allows you to load models trained by libSVM's svm-train, and use them from your Rust code.

Background

Support Vector Machines (SVMs) are a class of relatively simple and fast machine learning algorithms. They have

  • few parameters (making them easy to tune),
  • good generalization properties (making them good learners with limited data) and
  • overall good classification accuracy.

LibSVM is a relatively portable, general purpose SVM implementation written in C++ that includes tools for training, as well as tools and code for classification.

FFSVM is a library that can load such models trained by libSVM's svm-train and offers a number of benefits:

Features

FFSVM

  • loads almost all libSVM types (C-SVC, ν-SVC, ε-SVR, ν-SVR) and kernels (linear, poly, RBF and sigmoid)
  • produces practically same classification results as libSVM
  • optimized for SIMD and can be mixed seamlessly with Rayon
  • written in 100% Rust, but can be loaded from any language (via FFI)
  • allocation-free during classification for dense SVMs
  • 2.5x - 14x faster than libSVM for dense SVMs
  • extremely low classification times for small models (e.g., 128 SV, 16 dense attributes, linear ~ 500ns)
  • successfully used in Unity and VR projects (Windows & Android)
  • free of unsafe code ;)

FFSVM is not, however, a full libSVM replacement. Instead, it assumes you use svm-train at home (see Usage below), and ship a working model with your library or application.

Usage

If you have a libSVM model

In this example we assume you already have a libSVM that was trained with svm-train. If you haven't created a model yet, check out the FAQ on how to get started.

use ffsvm::*;
use std::convert::TryFrom;

fn main() -> Result<(), Error> {
    // Replace `SAMPLE_MODEL` with a `&str` to your model.
    let svm = DenseSVM::try_from(SAMPLE_MODEL)?;

    let mut problem = Problem::from(&svm);
    let features = problem.features();

    features[0] = 0.55838;
    features[1] = -0.157895;
    features[2] = 0.581292;
    features[3] = -0.221184;

    svm.predict_value(&mut problem)?;

    assert_eq!(problem.solution(), Solution::Label(42));

    Ok(())
}

Performance Tips

  • For a ~50% performance boost consider compiling your application with more aggressive CPU flags (e.g., export RUSTFLAGS="-C target-feature=+avx2" in case you run on a modern x86 CPU).
  • For a further x-fold performance increase, create a number of Problem structures, and process them with Rayon's par_iter.

Structs

DenseSVM

A SVM using SIMD intrinsics optimized for speed.

ModelFile

Parsing result of a model file used to instantiate a DenseSVM or SparseSVM.

Problem

A single problem a SVM should classify.

SparseSVM

A SVM optimized for large models with many empty attributes.

Enums

Error

Possible error types when classifying with one of the SVMs.

Solution

The result of a classification

Traits

Predict

Implemented by DenseSVM and SparseSVM to predict a Problem.

Type Definitions

DenseProblem

Problems produced for DenseSVMs.

SparseProblem

Problems produced for SparseSVMs.