LIBMF Rust
LIBMF - large-scale sparse matrix factorization - for Rust
Check out Disco for higher-level collaborative filtering
Installation
Add this line to your application’s Cargo.toml under [dependencies]:
= "0.2"
Getting Started
Prep your data in the format row_index, column_index, value
let mut data = new;
data.push;
data.push;
data.push;
Fit a model
let model = params.fit.unwrap;
Make predictions
model.predict;
Get the latent factors (these approximate the training matrix)
model.p;
model.q;
// or
model.p_iter;
model.q_iter;
Get the bias (average of all elements in the training matrix)
model.bias;
Save the model to a file
model.save.unwrap;
Load the model from a file
let model = load.unwrap;
Pass a validation set
let model = params.fit_eval.unwrap;
Cross-Validation
Perform cross-validation
let avg_error = params.cv.unwrap;
Parameters
Set parameters - default values below
params
.loss // loss function
.factors // number of latent factors
.threads // number of threads used
.bins // number of bins
.iterations // number of iterations
.lambda_p1 // coefficient of L1-norm regularization on P
.lambda_p2 // coefficient of L2-norm regularization on P
.lambda_q1 // coefficient of L1-norm regularization on Q
.lambda_q2 // coefficient of L2-norm regularization on Q
.learning_rate // learning rate
.alpha // importance of negative entries
.c // desired value of negative entries
.nmf // perform non-negative MF (NMF)
.quiet; // no outputs to stdout
Loss Functions
For real-valued matrix factorization
Loss::RealL2- squared error (L2-norm)Loss::RealL1- absolute error (L1-norm)Loss::RealKL- generalized KL-divergence
For binary matrix factorization
Loss::BinaryLog- logarithmic errorLoss::BinaryL2- squared hinge lossLoss::BinaryL1- hinge loss
For one-class matrix factorization
Loss::OneClassRow- row-oriented pair-wise logarithmic lossLoss::OneClassCol- column-oriented pair-wise logarithmic lossLoss::OneClassL2- squared error (L2-norm)
Metrics
Calculate RMSE (for real-valued MF)
model.rmse;
Calculate MAE (for real-valued MF)
model.mae;
Calculate generalized KL-divergence (for non-negative real-valued MF)
model.gkl;
Calculate logarithmic loss (for binary MF)
model.logloss;
Calculate accuracy (for binary MF)
model.accuracy;
Calculate MPR (for one-class MF)
model.mpr;
Calculate AUC (for one-class MF)
model.auc;
Example
Download the MovieLens 100K dataset.
Add these lines to your application’s Cargo.toml under [dependencies]:
= "1"
= { = "1", = ["derive"] }
And use:
use ReaderBuilder;
use Deserialize;
use File;
Reference
Specify the initial capacity for a matrix
let mut data = with_capacity;
Resources
Upgrading
0.2.0
Use
let model = params.factors.fit.unwrap;
instead of
let mut model = new;
model.factors = 20;
model.fit;
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development: