• Stars
    star
    169
  • Rank 224,453 (Top 5 %)
  • Language
    Rust
  • License
    MIT License
  • Created almost 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Autodifferentiation package in Rust.

wyrm

Crates.io badge Docs.rs badge Build Status

A reverse mode, define-by-run, low-overhead autodifferentiation library.

Features

Performs backpropagation through arbitrary, define-by-run computation graphs, emphasizing low overhead estimation of sparse, small models on the CPU.

Highlights:

  1. Low overhead.
  2. Built-in support for sparse gradients.
  3. Define-by-run.
  4. Trivial Hogwild-style parallelisation, scaling linearly with the number of CPU cores available.

Quickstart

The following defines a univariate linear regression model, then backpropagates through it.

let slope = ParameterNode::new(random_matrix(1, 1));
let intercept = ParameterNode::new(random_matrix(1, 1));

let x = InputNode::new(random_matrix(1, 1));
let y = InputNode::new(random_matrix(1, 1));

let y_hat = slope.clone() * x.clone() + intercept.clone();
let mut loss = (y.clone() - y_hat).square();

To optimize the parameters, create an optimizer object and go through several epochs of learning:

let mut optimizer = SGD::new().learning_rate(0.1);

for _ in 0..num_epochs {
    let x_value: f32 = rand::random();
    let y_value = 3.0 * x_value + 5.0;

    // You can re-use the computation graph
    // by giving the input nodes new values.
    x.set_value(x_value);
    y.set_value(y_value);

    loss.forward();
    loss.backward(1.0);

    optimizer.step(loss.parameters());
}

You can use rayon to fit your model in parallel, by first creating a set of shared parameters, then building a per-thread copy of the model:

let slope_param = Arc::new(HogwildParameter::new(random_matrix(1, 1)));
let intercept_param = Arc::new(HogwildParameter::new(random_matrix(1, 1)));
let num_epochs = 10;

(0..rayon::current_num_threads())
    .into_par_iter()
       .for_each(|_| {
           let slope = ParameterNode::shared(slope_param.clone());
           let intercept = ParameterNode::shared(intercept_param.clone());
           let x = InputNode::new(random_matrix(1, 1));
           let y = InputNode::new(random_matrix(1, 1));
           let y_hat = slope.clone() * x.clone() + intercept.clone();
           let mut loss = (y.clone() - y_hat).square();

           let optimizer = SGD::new().learning_rate(0.1);

           for _ in 0..num_epochs {
               let x_value: f32 = rand::random();
               let y_value = 3.0 * x_value + 5.0;

               x.set_value(x_value);
               y.set_value(y_value);

               loss.forward();
               loss.backward(1.0);

               optimizer.step(loss.parameters());
           }
       });

BLAS support

You should enable BLAS support to get (much) better performance out of matrix-multiplication-heavy workloads. To do so, add the following to your Cargo.toml:

ndarray = { version = "0.11.0", features = ["blas", "serde-1"] }
blas-src = { version = "0.1.2", default-features = false, features = ["openblas"] }
openblas-src = { version = "0.5.6", default-features = false, features = ["cblas"] }

Fast numerics

Enable the fast-math option to use fast approximations to transcendental functions. This should give substantial speed gains in networks that are exp, ln, or tanh-heavy.

License: MIT

More Repositories

1

spotlight

Deep recommender models using PyTorch.
Python
2,857
star
2

glove-python

Toy Python implementation of http://www-nlp.stanford.edu/projects/glove/
Python
1,232
star
3

rustlearn

Machine learning crate for Rust
Rust
598
star
4

triplet_recommendations_keras

An example of doing MovieLens recommendations using triplet loss in Keras
Jupyter Notebook
417
star
5

sbr-go

Recommender systems for Go
Go
169
star
6

sbr-rs

Deep recommender systems for Rust
Rust
115
star
7

netrex

Neural network recommendation models in PyTorch
Python
57
star
8

recommender_datasets

A common format and repository for various recommender datasets.
Python
38
star
9

mixture

TeX
26
star
10

python-rustlearn

Calling rustlearn from Python
Python
26
star
11

explicit-vs-implicit

An experiment on explicit vs implicit feedback recommenders
Jupyter Notebook
26
star
12

binge

Recommendation models that use binary rather than floating point operations at prediction time.
TeX
21
star
13

fizzbuzz

A FizzBuzz solver using a Rust autodifferentiation library.
Rust
20
star
14

dictionarylearning

Online learning of sparse dictionaries
Java
13
star
15

dynarray

Dynamic Numpy arrays
Python
13
star
16

hugo-blog

Rust
8
star
17

BayesianTestJS

Rudimentary Bayesian Beta-Bernoulli A/B testing inference and visualization code.
JavaScript
6
star
18

BetaJS

Beta distribution probability density function (PDF) for JavaScript
JavaScript
3
star
19

sbr-sys

Rust
2
star
20

spotlight-recipe

Conda recipes for Spotlight
Shell
2
star
21

blas_cython

Python
1
star
22

lightfm_datasets

Datasets for the LightFM package
Python
1
star
23

sketchy

Python
1
star
24

wheedle

Rust
1
star
25

fictional-octo-chainsaw

1
star