[][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.

#![feature(try_from)]

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(())
}

Structs

ModelFile

Parsing result of a model file used to instantiate a [SVM].

Problem

A single problem a DenseSVM or SparseSVM should classify.

SVMCore

Generic support vector machine core, used by [DenseSVM] and [SparseSVM].

Enums

Error

Possible error types when classifying with a [SVMCore].

Solution

The result of a classification

Traits

Predict

Implemented by [DenseSVM] and [SparseSVM] to predict a Problem.

Type Definitions

DenseProblem

Problems produced for DenseSVMs.

DenseSVM

Start here to classify dense models with highest performance.

SparseProblem

Problems produced for SparseSVMs.

SparseSVM

Use this to load any libSVM model with normal performance.