A machine learning library in Rust from scratch.

Overview

Machine Learning in Rust

Learn the Rust programming language through implementing classic machine learning algorithms. This project is self-completed without relying on any third-party libraries, serving as a bootstrap machine learning library.

:Actively seeking code reviews and welcome suggestions on fixing bugs or code refactoring. Please feel free to share your ideas. Happy to accept advice!

Basics

  1. NdArray Module, just as the name. It has implemented broadcast, matrix operations, permute and etc. in arbitrary dimension. SIMD is used in matrix multiplication thanks to auto vectorizing by Rust.
  2. Dataset Module, supporting customized loading data, re-format, normalize, shuffle and Dataloader. Several popular dataset pre-processing recipes are available.

Algorithms

  1. Decision Tree, supporting both classification and regression tasks. Info gains like gini or entropy are provided.
  2. Logistic Regression, supporting regularization (Lasso, Ridge and L-inf)
  3. Linear Regression, same as logistic regression, but for regression tasks.
  4. Naive Bayes, free to handle discrete or continuous feature values.
  5. SVM, with linear kernel using SGD and Hinge Loss to optimize.
  6. nn Module, containing linear(MLP) and some activation functions which could be freely stacked and optimized by gradient back propagations.
  7. KNN, supporting both KdTree and vanilla BruteForceSearch.
  8. K-Means, clustering data with an unsupervised learning approach

Start

Let's use KNN algorithm to solve a classification task. More examples can be found in examples directory.

  1. create some synthetic data for tests

    use std::collections::HashMap;
    
    let features = vec![
        vec![0.6, 0.7, 0.8],
        vec![0.7, 0.8, 0.9],
        vec![0.1, 0.2, 0.3],
    ];
    let labels = vec![0, 0, 1];
    // so it is a binary classifiction task, 0 is for the large label, 1 is for the small label
    let mut label_map = HashMap::new();
    label_map.insert(0, "large".to_string());
    label_map.insert(1, "small".to_string());
  2. convert the data to the dataset

    use mlinrust::dataset::Dataset;
    
    let dataset = Dataset::new(features, labels, Some(label_map));
  3. split the dataset into train and valid sets and normalize them by Standard normalization

    let mut temp =  dataset.split_dataset(vec![2.0, 1.0], 0); // [2.0, 1.0] is the split fraction, 0 is the seed
    let (mut train_dataset, mut valid_dataset) = (temp.remove(0), temp.remove(0));
    
    use mlinrust::dataset::utils::{normalize_dataset, ScalerType};
    
    normalize_dataset(&mut train_dataset, ScalerType::Standard);
    normalize_dataset(&mut valid_dataset, ScalerType::Standard);
  4. build and train our KNN model using KdTree

    use mlinrust::model::knn::{KNNAlg, KNNModel, KNNWeighting};
    
    // KdTree is one implementation of KNN; 1 defines the k of neighbours; Weighting decides the way of ensemble prediction; train_dataset is for training KNN; Some(2) is the param of minkowski distance
    let model = KNNModel::new(KNNAlg::KdTree, 1, Some(KNNWeighting::Distance), train_dataset, Some(2));
  5. evaluate the model

    use mlinrust::utils::evaluate;
    
    let (correct, acc) = evaluate(&valid_dataset, &model);
    println!("evaluate results\ncorrect {correct} / total {}, acc = {acc:.5}", test_dataset.len());

Todo

  1. model weights serialization for saving and loading
  2. Boosting/bagging
  3. matrix multiplication with multi threads
  4. refactor codes, sincerely request for comments from senior developers

Reference

  1. scikit-learn
  2. The book, 机器学习西瓜书 by Prof. Zhihua Zhou

Thanks

The rust community. I received many help from rust-lang Discord.

License

Under GPL-v3 license. And commercial use is strictly prohibited.

You might also like...
A Machine Learning Framework for High Performance written in Rust
A Machine Learning Framework for High Performance written in Rust

polarlight polarlight is a machine learning framework for high performance written in Rust. Key Features TBA Quick Start TBA How To Contribute Contrib

Example of Rust API for Machine Learning

rust-machine-learning-api-example Example of Rust API for Machine Learning API example that uses resnet224 to infer images received in base64 and retu

High-level non-blocking Deno bindings to the rust-bert machine learning crate.

bertml High-level non-blocking Deno bindings to the rust-bert machine learning crate. Guide Introduction The ModelManager class manages the FFI bindin

Machine learning Neural Network in Rust

vinyana vinyana - stands for mind in pali language. Goal To implement a simple Neural Network Library in order to understand the maths behind it. This

Source Code for 'Practical Machine Learning with Rust' by Joydeep Bhattacharjee

Apress Source Code This repository accompanies Practical Machine Learning with Rust by Joydeep Bhattacharjee (Apress, 2020). Download the files as a z

An example of using TensorFlow rust bindings to serve trained machine learning models via Actix Web
An example of using TensorFlow rust bindings to serve trained machine learning models via Actix Web

Serving TensorFlow with Actix-Web This repository gives an example of training a machine learning model using TensorFlow2.0 Keras in python, exporting

🏆 A ranked list of awesome machine learning Rust libraries.

best-of-ml-rust 🏆 A ranked list of awesome machine learning Rust libraries. This curated list contains 180 awesome open-source projects with a total

Machine learning crate in Rust
Machine learning crate in Rust

DeepRust - Machine learning in Rust Vision To create a deeplearning crate in rust aiming to create a great experience for ML researchers & developers

BudouX-rs is a rust port of BudouX (machine learning powered line break organizer tool).

BudouX-rs BudouX-rs is a rust port of BudouX (machine learning powered line break organizer tool). Note: This project contains the deliverables of the

Owner
Chi Zuo
Pursuing a Master's degree in CS.
Chi Zuo
Machine Learning library for Rust

rusty-machine This library is no longer actively maintained. The crate is currently on version 0.5.4. Read the API Documentation to learn more. And he

James Lucas 1.2k Dec 31, 2022
Machine Learning Library for Rust

autograph Machine Learning Library for Rust undergoing maintenance Features Portable accelerated compute Run SPIR-V shaders on GPU's that support Vulk

null 223 Jan 1, 2023
Mars is a rust machine learning library. [Goal is to make Simple as possible]

Mars Mars (ma-rs) is an blazingly fast rust machine learning library. Simple and Powerful! ?? ?? Contribution: Feel free to build this project. This i

KoBruh 3 Dec 25, 2022
convolutions-rs is a crate that provides a fast, well-tested convolutions library for machine learning

convolutions-rs convolutions-rs is a crate that provides a fast, well-tested convolutions library for machine learning written entirely in Rust with m

null 10 Jun 28, 2022
A machine learning library for supervised training of parametrized models

Vikos Vikos is a library for supervised training of parameterized, regression, and classification models Design Goals Model representations, cost func

Blue Yonder GmbH 10 May 10, 2022
A Rust machine learning framework.

Linfa linfa (Italian) / sap (English): The vital circulating fluid of a plant. linfa aims to provide a comprehensive toolkit to build Machine Learning

Rust-ML 2.2k Jan 2, 2023
Machine learning crate for Rust

rustlearn A machine learning package for Rust. For full usage details, see the API documentation. Introduction This crate contains reasonably effectiv

Maciej Kula 547 Dec 28, 2022
Machine learning in Rust.

Rustml Rustml is a library for doing machine learning in Rust. The documentation of the project with a descprition of the modules can be found here. F

null 60 Dec 15, 2022
Rust based Cross-GPU Machine Learning

HAL : Hyper Adaptive Learning Rust based Cross-GPU Machine Learning. Why Rust? This project is for those that miss strongly typed compiled languages.

Jason Ramapuram 83 Dec 20, 2022
Fwumious Wabbit, fast on-line machine learning toolkit written in Rust

Fwumious Wabbit is a very fast machine learning tool built with Rust inspired by and partially compatible with Vowpal Wabbit (much love! read more abo

Outbrain 115 Dec 9, 2022