A neural network crate

Overview

RustNN

Build Status

An easy to use neural network library written in Rust.

Crate

Documentation

Description

RustNN is a feedforward neural network library. The library generates fully connected multi-layer artificial neural networks that are trained via backpropagation. Networks are trained using an incremental training mode.

XOR example

This example creates a neural network with 2 nodes in the input layer, a single hidden layer containing 3 nodes, and 1 node in the output layer. The network is then trained on examples of the XOR function. All of the methods called after train(&examples) are optional and are just used to specify various options that dictate how the network should be trained. When the go() method is called the network will begin training on the given examples. See the documentation for the NN and Trainer structs for more details.

use nn::{NN, HaltCondition};

// create examples of the XOR function
// the network is trained on tuples of vectors where the first vector
// is the inputs and the second vector is the expected outputs
let examples = [
    (vec![0f64, 0f64], vec![0f64]),
    (vec![0f64, 1f64], vec![1f64]),
    (vec![1f64, 0f64], vec![1f64]),
    (vec![1f64, 1f64], vec![0f64]),
];

// create a new neural network by passing a pointer to an array
// that specifies the number of layers and the number of nodes in each layer
// in this case we have an input layer with 2 nodes, one hidden layer
// with 3 nodes and the output layer has 1 node
let mut net = NN::new(&[2, 3, 1]);
    
// train the network on the examples of the XOR function
// all methods seen here are optional except go() which must be called to begin training
// see the documentation for the Trainer struct for more info on what each method does
net.train(&examples)
    .halt_condition( HaltCondition::Epochs(10000) )
    .log_interval( Some(100) )
    .momentum( 0.1 )
    .rate( 0.3 )
    .go();
    
// evaluate the network to see if it learned the XOR function
for &(ref inputs, ref outputs) in examples.iter() {
    let results = net.run(inputs);
    let (result, key) = (results[0].round(), outputs[0]);
    assert!(result == key);
}
Comments
  • Build is failing

    Build is failing

    RustNN$ cargo build --verbose
        Updating registry `https://github.com/rust-lang/crates.io-index`
       Compiling threadpool v0.1.4
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/threadpool-0.1.4/src/lib.rs --crate-name threadpool --crate-type lib -g -C metadata=213781c0d8fba90b -C extra-filename=-213781c0d8fba90b --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/deps --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -Awarnings`
       Compiling libc v0.1.6
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.1.6/rust/src/liblibc/lib.rs --crate-name libc --crate-type lib -g --cfg feature="default" --cfg feature="cargo-build" -C metadata=9b7976990ae0dbd4 -C extra-filename=-9b7976990ae0dbd4 --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/deps --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -Awarnings`
       Compiling gcc v0.3.5
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/gcc-0.3.5/src/lib.rs --crate-name gcc --crate-type lib -g -C metadata=982b24959a427c6e -C extra-filename=-982b24959a427c6e --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/deps --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -Awarnings`
       Compiling rustc-serialize v0.3.14
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.14/src/lib.rs --crate-name rustc_serialize --crate-type lib -g -C metadata=9ef26f158d5284e0 -C extra-filename=-9ef26f158d5284e0 --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/deps --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -Awarnings`
       Compiling rand v0.3.8
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.3.8/src/lib.rs --crate-name rand --crate-type lib -g -C metadata=b924d9fc5b3eb5b8 -C extra-filename=-b924d9fc5b3eb5b8 --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/deps --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps --extern libc=/home/swiesend/workspace/rust/RustNN/target/debug/deps/liblibc-9b7976990ae0dbd4.rlib -Awarnings`
       Compiling time v0.1.25
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/time-0.1.25/build.rs --crate-name build_script_build --crate-type bin -C prefer-dynamic -g --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/build/time-e758cbe877e9589d --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps --extern gcc=/home/swiesend/workspace/rust/RustNN/target/debug/deps/libgcc-982b24959a427c6e.rlib -Awarnings`
         Running `/home/swiesend/workspace/rust/RustNN/target/debug/build/time-e758cbe877e9589d/build-script-build`
         Running `rustc /home/swiesend/.cargo/registry/src/github.com-1ecc6299db9ec823/time-0.1.25/src/lib.rs --crate-name time --crate-type lib -g -C metadata=e758cbe877e9589d -C extra-filename=-e758cbe877e9589d --out-dir /home/swiesend/workspace/rust/RustNN/target/debug/deps --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps --extern libc=/home/swiesend/workspace/rust/RustNN/target/debug/deps/liblibc-9b7976990ae0dbd4.rlib -Awarnings -L native=/home/swiesend/workspace/rust/RustNN/target/debug/build/time-e758cbe877e9589d/out -l static=time_helpers`
       Compiling nn v0.1.5 (file:///home/swiesend/workspace/rust/RustNN)
         Running `rustc src/lib.rs --crate-name nn --crate-type lib -g --out-dir /home/swiesend/workspace/rust/RustNN/target/debug --emit=dep-info,link -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug -L dependency=/home/swiesend/workspace/rust/RustNN/target/debug/deps --extern time=/home/swiesend/workspace/rust/RustNN/target/debug/deps/libtime-e758cbe877e9589d.rlib --extern threadpool=/home/swiesend/workspace/rust/RustNN/target/debug/deps/libthreadpool-213781c0d8fba90b.rlib --extern rustc_serialize=/home/swiesend/workspace/rust/RustNN/target/debug/deps/librustc_serialize-9ef26f158d5284e0.rlib --extern rand=/home/swiesend/workspace/rust/RustNN/target/debug/deps/librand-b924d9fc5b3eb5b8.rlib -L native=/home/swiesend/workspace/rust/RustNN/target/debug/build/time-e758cbe877e9589d/out`
    src/lib.rs:70:18: 70:28 error: unresolved import `threadpool::ScopedPool`. There is no `ScopedPool` in `threadpool`
    src/lib.rs:70 use threadpool::{ScopedPool};
                                   ^~~~~~~~~~
    error: aborting due to previous error
    Could not compile `nn`.
    

    Hey Jack,

    I'm very new to rust and still don't know how to attack build issues. Can you help me out with that one? I don't understand well why its not working, although the ScopedPool seems to be implemented in the threadpool lib as pub struct ScopedPool<'pool> : https://github.com/rust-lang/threadpool/blob/master/src/lib.rs

    rustc --version
    rustc 1.0.0-beta.4 (850151a75 2015-04-30) (built 2015-04-30)
    
    opened by swiesend 4
  • Index out of bounds error with more than three layers

    Index out of bounds error with more than three layers

    Hi,

    When I change the layer of xor test NN from &[2,3,1] to &[2,3,3,1] , it panics at this line with the following trace:

    thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcollections/vec.rs:1338
    stack backtrace:
       1:     0x7f238e58c7e9 - sys::backtrace::write::ha14749a03a5342e69AD
       2:     0x7f238e5905e6 - panicking::on_panic::hee4fd6319a5ce222fRJ
       3:     0x7f238e57a3f3 - rt::unwind::begin_unwind_inner::h7cec0e68cf4dd4e9WwJ
       4:     0x7f238e57a7df - rt::unwind::begin_unwind_fmt::h9b999773388b3dfaxvJ
       5:     0x7f238e58ffa7 - rust_begin_unwind
       6:     0x7f238e5bd0a4 - panicking::panic_fmt::h518b890fb69ee41c9qB
       7:     0x7f238e5bec9c - panicking::panic_bounds_check::hfe3fd03998902d4dfqB
       8:     0x7f238e4d8066 - vec::Vec<T>.Index<usize>::index::h10533351574849293586
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcollections/vec.rs:1338
       9:     0x7f238e4aff8e - NN::calculate_weight_updates::h496ec902d44cfbc4AOa
                            at src/lib.rs:538
      10:     0x7f238e4cf655 - NN::train_batch::closure.5782
                            at src/lib.rs:450
      11:     0x7f238e4cf33d - F.FnBox::call_box::h16158016883286431322
                            at /home/guersam/.cargo/registry/src/github.com-1ecc6299db9ec823/threadpool-0.1.2/src/lib.rs:26
      12:     0x7f238e546fee - spawn_scoped_in_pool::closure.3431
                            at /home/guersam/.cargo/registry/src/github.com-1ecc6299db9ec823/threadpool-0.1.2/src/lib.rs:225
      13:     0x7f238e546de6 - thunk::Thunk<'a, (), R>::new::closure.3426
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thunk.rs:27
      14:     0x7f238e546d41 - thunk::F.Invoke<A, R>::invoke::h3097253234199166430
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thunk.rs:54
      15:     0x7f238e53d6a7 - thunk::Thunk<'a, A, R>::invoke::h13280486025064970643
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thunk.rs:41
      16:     0x7f238e53cd63 - thread::Builder::spawn_inner::closure.3133
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thread/mod.rs:354
      17:     0x7f238e53ccee - rt::unwind::try::try_fn::__rust_abi::h7868465900712247904
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/rt/unwind.rs:139
      18:     0x7f238e53cc89 - rt::unwind::try::try_fn::h7868465900712247904
      19:     0x7f238e594818 - rust_try_inner
      20:     0x7f238e594805 - rust_try
      21:     0x7f238e53c07a - rt::unwind::try::h18352464926673870406
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/rt/unwind.rs:125
      22:     0x7f238e53bcb7 - thread::Builder::spawn_inner::closure.3000
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thread/mod.rs:354
      23:     0x7f238e53dca4 - thunk::Thunk<'a, (), R>::new::closure.3155
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thunk.rs:27
      24:     0x7f238e53dbdc - thunk::F.Invoke<A, R>::invoke::h13792676425105660100
                            at /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libstd/thunk.rs:54
      25:     0x7f238e58f2c1 - sys::thread::create::thread_start::h5bfe55b0d48e24ddIqI
      26:     0x7f238d5580a4 - start_thread
      27:     0x7f238dd71cfc - __clone
      28:                0x0 - <unknown>
    
    
    opened by guersam 1
  • Clean up mut not necessary warnings

    Clean up mut not necessary warnings

    On rustc 1.9.0 (e4e8b6668 2016-05-18) I get the following warnings:

    src/lib.rs:394:21: 394:35 warning: variable does not need to be mutable, #[warn(unused_mut)] on src/lib.rs:410:25: 410:46 warning: variable does not need to be mutable, #[warn(unused_mut)] on src/lib.rs:410                     let mut prev_layer_result;
    src/lib.rs:394                 let mut node_error;
    src/lib.rs:410:25: 410:46 warning: variable does not need to be mutable, #[warn(unused_mut)] on by default
    src/lib.rs:410                     let mut prev_layer_result;
                                           ^~~~~~~~~~~~~~~~~~~~~
    

    This commit just gets rid of those warnings.

    opened by dbravender 0
  • Improvement and feature addition and Update from deprecated libraries to new ones

    Improvement and feature addition and Update from deprecated libraries to new ones

    At first, I did not plan to create a pull request and to keep everything clean and abstract. But I think the improvements got really useful, so I decided to clean up and update the doc and README accordingly, but I don't know, if everything is still good enough. If there is something minor not ok, please tell me, so I can fix and you can merge safely.

    opened by FlixCoder 3
  • Relicense under dual MIT/Apache-2.0

    Relicense under dual MIT/Apache-2.0

    This issue was automatically generated. Feel free to close without ceremony if you do not agree with re-licensing or if it is not possible for other reasons. Respond to @cmr with any questions or concerns, or pop over to #rust-offtopic on IRC to discuss.

    You're receiving this because someone (perhaps the project maintainer) published a crates.io package with the license as "MIT" xor "Apache-2.0" and the repository field pointing here.

    TL;DR the Rust ecosystem is largely Apache-2.0. Being available under that license is good for interoperation. The MIT license as an add-on can be nice for GPLv2 projects to use your code.

    Why?

    The MIT license requires reproducing countless copies of the same copyright header with different names in the copyright field, for every MIT library in use. The Apache license does not have this drawback. However, this is not the primary motivation for me creating these issues. The Apache license also has protections from patent trolls and an explicit contribution licensing clause. However, the Apache license is incompatible with GPLv2. This is why Rust is dual-licensed as MIT/Apache (the "primary" license being Apache, MIT only for GPLv2 compat), and doing so would be wise for this project. This also makes this crate suitable for inclusion and unrestricted sharing in the Rust standard distribution and other projects using dual MIT/Apache, such as my personal ulterior motive, the Robigalia project.

    Some ask, "Does this really apply to binary redistributions? Does MIT really require reproducing the whole thing?" I'm not a lawyer, and I can't give legal advice, but some Google Android apps include open source attributions using this interpretation. Others also agree with it. But, again, the copyright notice redistribution is not the primary motivation for the dual-licensing. It's stronger protections to licensees and better interoperation with the wider Rust ecosystem.

    How?

    To do this, get explicit approval from each contributor of copyrightable work (as not all contributions qualify for copyright, due to not being a "creative work", e.g. a typo fix) and then add the following to your README:

    ## License
    
    Licensed under either of
    
     * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
     * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
    
    at your option.
    
    ### Contribution
    
    Unless you explicitly state otherwise, any contribution intentionally submitted
    for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
    additional terms or conditions.
    

    and in your license headers, if you have them, use the following boilerplate (based on that used in Rust):

    // Copyright 2016 RustNN Developers
    //
    // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
    // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
    // http://opensource.org/licenses/MIT>, at your option. This file may not be
    // copied, modified, or distributed except according to those terms.
    

    It's commonly asked whether license headers are required. I'm not comfortable making an official recommendation either way, but the Apache license recommends it in their appendix on how to use the license.

    Be sure to add the relevant LICENSE-{MIT,APACHE} files. You can copy these from the Rust repo for a plain-text version.

    And don't forget to update the license metadata in your Cargo.toml to:

    license = "MIT OR Apache-2.0"
    

    I'll be going through projects which agree to be relicensed and have approval by the necessary contributors and doing this changes, so feel free to leave the heavy lifting to me!

    Contributor checkoff

    To agree to relicensing, comment with :

    I license past and future contributions under the dual MIT/Apache-2.0 license, allowing licensees to chose either at their option.
    

    Or, if you're a contributor, you can check the box in this repo next to your name. My scripts will pick this exact phrase up and check your checkbox, but I'll come through and manually review this issue later as well.

    • [ ] @jackm321
    • [ ] @guersam
    opened by emberian 0
Owner
Jack Montgomery
Jack Montgomery
Simple neural network library for classification written in Rust.

Cogent A note I continue working on GPU stuff, I've made some interesting things there, but ultimately it made me realise this is far too monumental a

Jonathan Woollett-Light 41 Dec 25, 2022
Rust wrapper for the Fast Artificial Neural Network library

fann-rs Rust wrapper for the Fast Artificial Neural Network (FANN) library. This crate provides a safe interface to FANN on top of the low-level bindi

Andreas Fackler 12 Jul 17, 2022
A neural network, and tensor dynamic automatic differentiation implementation for Rust.

Corgi A neural network, and tensor dynamic automatic differentiation implementation for Rust. BLAS The BLAS feature can be enabled, and requires CBLAS

Patrick Song 20 Nov 7, 2022
Simple Neural Network on rust

Simple Artificial Neural Network A crate that implements simple usage of dense neural networks. Instalation Add this to your dependencies on Cargo.tom

null 6 Jul 1, 2022
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

Alexandru Olaru 3 Dec 26, 2022
SelfOrgMap 5 Nov 4, 2020
n2 is a library implementation of a feedforward, backpropagation artificial neural network.

n2 is a library implementation of a feedforward, backpropagation artificial neural network. Usage Add the following to the [dependencies] section o

Søren Mortensen 0 Feb 21, 2021
A light wheight Neural Network library with a focus on ease of use and speed.

Smarty Pants This goal of this library is to: Produce NeuralNetworks that will always give the same result when given the same input. Provide methods

Coding Wizard 3 Mar 7, 2022
A neural network model that can approximate any non-linear function by using the random search algorithm for the optimization of the loss function.

random_search A neural network model that can approximate any non-linear function by using the random search algorithm for the optimization of the los

ph04 2 Apr 1, 2022
Neural network implementations from scratch in Rust.

Neural Network from Scratch Neural network implementations from scratch in Rust. Setup & Run Dataset used is mnist. Download the 4 archives and extrac

Mohammad Rahhal 6 Dec 29, 2022
Build neural network models in Cairo 1.0

Explore ML in Cairo 1.0 Build neural network models in Cairo 1.0 to perform inference. The calculations are performed using i33 values, and the outcom

null 17 Mar 27, 2023
A fun, hackable, GPU-accelerated, neural network library in Rust, written by an idiot

Tensorken: A Fun, Hackable, GPU-Accelerated, Neural Network library in Rust, Written by an Idiot (work in progress) Understanding deep learning from t

Kurt Schelfthout 44 May 6, 2023
An experimental Neural Network trainer/visualizer in Rust

DeepRender An experimental Neural Network trainer/visualizer in Rust Try it on your browser! https://msakuta.github.io/DeepRender/ Training on a funct

Masahiro Sakuta 6 Jun 12, 2023
The CBNF neural network header format.

cbnf The CBNF neural network header format. What is CBNF? CBNF is a neural network header format for use with efficiently updatable neural networks fo

Cosmo Bobak 2 Jul 11, 2023
Neural networks in Rust

deeplearn-rs Deep learning in Rust! This is my first shot at this. It's mostly just a proof of concept right now. The API will change. Status We have

Theodore DeRego 199 Oct 23, 2022
A simple neural net implementation.

PROPHET - Neural Network Library Linux Windows Codecov Coveralls Docs Crates.io A simple neural net implementation written in Rust with a focus on cac

Robin Freyler 41 Sep 16, 2022
Rust implementation of real-coded GA for solving optimization problems and training of neural networks

revonet Rust implementation of real-coded genetic algorithm for solving optimization problems and training of neural networks. The latter is also know

Yury Tsoy 19 Aug 11, 2022
Compile-time creation of neural networks with Rust

GAMMA Compile-time creation of neural networks with Rust Description This is for now just a showcase project of what can be done with const generics i

Aitor Ruano 354 Jan 1, 2023
Compile-time creation of neural networks

Mushin: Compile-time creation of neural networks Mushin is a Japanese term used in martial arts that refers to the state of mind obtained by practice.

Aitor Ruano 354 Jan 1, 2023