LIBFFM - field-aware factorization machines - in Rust

Overview

LIBFFM Rust

LIBFFM - field-aware factorization machines - in Rust

Build Status

Getting Started

LIBFFM Rust is available as a Rust library and a command line tool.

Rust Library

Installation

Add this line to your application鈥檚 Cargo.toml under [dependencies]:

libffm = "0.1"

How to Use

Prep your data in LIBFFM format

0 0:0:1 1:1:1
1 0:2:1 1:3:1

Train a model

let model = libffm::Model::train("train.ffm").unwrap();

Use a validation set and early stopping to prevent overfitting

let model = libffm::Model::params()
    .auto_stop(true)
    .train_eval("train.ffm", "valid.ffm")
    .unwrap();

Make predictions

let (predictions, loss) = model.predict("test.ffm").unwrap();

Save the model to a file

model.save("model.bin").unwrap();

Load the model from a file

let model = libffm::Model::load("model.bin").unwrap();

Training Options

let model = libffm::Model::params()
    .learning_rate(0.2)      // learning rate
    .lambda(0.00002)         // regularization parameter
    .iterations(15)          // number of iterations
    .factors(4)              // number of latent factors
    .quiet(false)            // quiet mode (no output)
    .normalization(true)     // use instance-wise normalization
    .auto_stop(false)        // stop at the iteration that achieves the best validation loss
    .on_disk(false)          // on-disk training
    .train("train.ffm");     // train or train_eval

Command Line Tool

Installation

Run:

cargo install libffm --features cli

How to Use

Prep your data in LIBFFM format

0 0:0:1 1:1:1
1 0:2:1 1:3:1

Train a model

ffm-train train.ffm model.bin

Use a validation set and early stopping to prevent overfitting

ffm-train -p valid.ffm --auto-stop train.ffm model.bin

Make predictions

ffm-predict test.ffm model.bin output.txt

Training Options

FLAGS:
        --auto-stop    Stop at the iteration that achieves the best validation loss (must be used with -p)
        --in-memory    Enable in-memory training
        --no-norm      Disable instance-wise normalization
        --quiet        Quiet mode (no output)

OPTIONS:
    -r 
   
                   Set learning rate [default: 0.2]
    -k 
    
                 Set number of latent factors [default: 4]
    -t 
     
               Set number of iterations [default: 15]
    -l 
      
                   Set regularization parameter [default: 0.00002]
    -s 
       
         Set number of threads [default: 1] -p 
        
          Set path to the validation set 
        
       
      
     
    
   

Credits

This library was ported from the LIBFFM C++ library and is available under the same license.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/libffm-rust.git
cd libffm-rust
cargo test
cargo run --bin ffm-train --features cli
cargo run --bin ffm-predict --features cli
You might also like...
馃悁 Building a federated alternative to reddit in rust

Lemmy A link aggregator / Reddit clone for the fediverse. Join Lemmy 路 Documentation 路 Report Bug 路 Request Feature 路 Releases 路 Code of Conduct About

Applied offensive security with Rust
Applied offensive security with Rust

Black Hat Rust - Early Access Deep dive into offensive security with the Rust programming language Buy the book now! Summary Whether in movies or main

Rholang runtime in rust

Rholang Runtime A rholang runtime written in Rust.

Easy-to-use optional function arguments for Rust

OptArgs uses const generics to ensure compile-time correctness. I've taken the liberty of expanding and humanizing the macros in the reference examples.

A language server for lua written in rust

lua-analyzer lua-analyzer is a lsp server for lua. This is mostly for me to learn the lsp protocol and language analysis so suggestions are helpful. T

Rust library that can be reset if you think it's slow

GoodbyeKT Rust library that can be reset if you think it's slow

Cargo - The Rust package manager

Cargo downloads your Rust project鈥檚 dependencies and compiles your project.

A copypastable guide to implementing simple derive macros in Rust.

A copypastable guide to implementing simple derive macros in Rust. The goal Let's say we have a trait with a getter trait MyTrait {

Rust ABI safe code generator

CGlue offers an easy way to ABI (application binary interface) safety. Just a few annotations and your trait is ready to go!

Comments
  • Decouple IO train/predict

    Decouple IO train/predict

    Hi @ankane. I was looking for some Rust based FM / FFM and I found your crate. Good job! It looks quite clean. I would like to test it on my problem. However I would like to skip serialization of my data before I call train/predict. If someone is using Rust and your crate as a library it could be easier to maybe pass the data itself than save it to disk first - which could be a lengthy process. In general it is almost always a good idea to decouple input and output from the actual algorithm.

    I propose to change the signatures of the methods to something like this

    pub fn train(&mut self, x: Vec<Vec<Node>>, y: Vec<f32>) // of course using references instead of Vec would be probably a good idea
    pub fn predict(&self, x: Vec<Vec<Node>>) 
    pub fn train_from_file(...)
    pub fn predict_from_file(...)
    

    Actually... when I think about it more it perhaps is more complicated than I thought because there should be some Iterator implemented to read the data from the disk. So in fact the signature for x would be more like

    train(..., data: I)
    where
        I: IntoIterator<Item = (&[Node], &f32)>
    

    And then there should be an Iterator implemented for file based data.

    If you are ok with such a change I could contribute this. Let me know your thoughts.

    opened by pjankiewicz 2
Owner
Andrew Kane
Andrew Kane
Cassette A simple, single-future, non-blocking executor intended for building state machines.

Cassette A simple, single-future, non-blocking executor intended for building state machines. Designed to be no-std and embedded friendly. This execut

James Munns 50 Jan 2, 2023
Frame is a markdown language for creating state machines (automata) in 7 programming languages as well as generating UML documentation.

Frame Language Transpiler v0.5.1 Hi! So very glad you are interested in Frame. Frame system design markdown language for software architects and engin

Mark Truluck 35 Dec 31, 2022
Envwoman is an application, to sync your .env-files across multiple machines

Envwoman is an application, to sync your .env-files across multiple machines. The main goal is to make Envwoman secure and trustworthy, so everything is open-source and the data will never in plain-text on the server. Encryption happens client-sided via aes-gcm.

Mawoka 3 Sep 28, 2022
First Git on Rust is reimplementation with rust in order to learn about rust, c and git.

First Git on Rust First Git on Rust is reimplementation with rust in order to learn about rust, c and git. Reference project This project refer to the

Nobkz 1 Jan 28, 2022
A stupid macro that compiles and executes Rust and spits the output directly into your Rust code

inline-rust This is a stupid macro inspired by inline-python that compiles and executes Rust and spits the output directly into your Rust code. There

William 19 Nov 29, 2022
Learn-rust - An in-depth resource to learn Rust 馃

Learning Rust ?? Hello friend! ?? Welcome to my "Learning Rust" repo, a home for my notes as I'm learning Rust. I'm structuring everything into lesson

Lazar Nikolov 7 Jan 28, 2022
A highly modular Bitcoin Lightning library written in Rust. Its Rust-Lightning, not Rusty's Lightning!

Rust-Lightning is a Bitcoin Lightning library written in Rust. The main crate, lightning, does not handle networking, persistence, or any other I/O. Thus, it is runtime-agnostic, but users must implement basic networking logic, chain interactions, and disk storage. More information is available in the About section.

Lightning Dev Kit 850 Jan 3, 2023
Telegram bot help you to run Rust code in Telegram via Rust playground

RPG_BOT (Rust Playground Bot) Telegram bot help you to run Rust code in Telegram via Rust playground Bot interface The bot supports 3 straightforward

TheAwiteb 8 Dec 6, 2022
`Debug` in rust, but only supports valid rust syntax and outputs nicely formatted using pretty-please

dbg-pls A Debug-like trait for rust that outputs properly formatted code Showcase Take the following code: let code = r#" [ "Hello, World!

Conrad Ludgate 12 Dec 22, 2022
Playing with web dev in Rust. This is a sample Rust microservice that can be deployed on Kubernetes.

Playing with web dev in Rust. This is a sample Rust microservice that can be deployed on Kubernetes.

Andr茅 Gomes 10 Nov 17, 2022