Skip to main content

Crate tinymind

Crate tinymind 

Source
Expand description

Rust port of the tinymind neural network library. So far only fixed-point value types are ported; tinymind also runs its networks with float and double.

Scope: Q16.16 and Q8.8 arithmetic and a fixed-point square root (Sqrt); ReLU, Linear and the tanh and sigmoid lookup-table activations; trainable networks with any number of hidden layers (NeuralNetwork over Dense layers, trained with SGD and momentum, Adam or RmsProp, with optional gradient clipping); a one-hidden-layer perceptron that trains on the device (MultilayerPerceptron) or only runs trained weights (InferencePerceptron, tinymind’s IsTrainable = false), and int8 affine-quantized inference (int8: dense layers, quantized activations and a TFLite-exact softmax). Every part produces the same bits as tinymind’s C++. The library is no_std and allocation-free, like tinymind’s freestanding build.

§Example

Train a 2-4-1 network on XOR in Q16.16, as tinymind’s examples/xor does (the xor example in this repository is the full port, bit-exact with the C++):

use tinymind::rand::GlibcRand;
use tinymind::{MultilayerPerceptron, Q16_16, Sigmoid, Tanh};

// 2 inputs, 4 tanh hidden neurons, 1 sigmoid output.
type Net = MultilayerPerceptron<Q16_16, Tanh, Sigmoid, 2, 4, 1>;

// Initial weights in [-0.5, 0.5] (raw Q16.16 units), from glibc-compatible rand().
let mut init = GlibcRand::new(1);
let mut nn = Net::new(|| Q16_16::from_raw(init.next_i32() % 65537 - 32768));

let mut rng = GlibcRand::new(7);
for _ in 0..20_000 {
    let (a, b) = (rng.next_i32() & 1, rng.next_i32() & 1);
    let target = [Q16_16::from_int(a ^ b)];
    nn.feed_forward(&[Q16_16::from_int(a), Q16_16::from_int(b)]);
    if !nn.calculate_error(&target).is_within_zero_tolerance() {
        nn.train(&target);
    }
}

for (a, b) in [(0, 0), (0, 1), (1, 0), (1, 1)] {
    nn.feed_forward(&[Q16_16::from_int(a), Q16_16::from_int(b)]);
    let p = nn.learned_values()[0].to_f64();
    assert_eq!(p >= 0.5, a ^ b == 1, "{a} ^ {b}: p = {p}");
}

Re-exports§

pub use activation::Activation;
pub use activation::Linear;
pub use activation::Relu;
pub use activation::Sigmoid;
pub use activation::Tanh;
pub use network::Dense;
pub use network::Layers;
pub use network::NeuralNetwork;
pub use neuralnet::InferencePerceptron;
pub use neuralnet::MultilayerPerceptron;
pub use optimizer::Adam;
pub use optimizer::Optimizer;
pub use optimizer::RmsProp;
pub use optimizer::Sgd;
pub use qformat::Fixed;
pub use qformat::Q8_8;
pub use qformat::Q16_16;
pub use qformat::QValue;
pub use qformat::Sqrt;

Modules§

activation
Activation policies, ported from tinymind cpp/activationFunctions.hpp, cpp/lookupTable.hpp and cpp/interpolate.hpp.
int8
Int8 affine-quantized inference, ported from tinymind cpp/qaffine.hpp, cpp/qdense.hpp, cpp/qpointwiseconv2d.hpp (per-channel dense), cpp/qactivations.hpp and cpp/qsoftmax_fixedpoint.hpp.
network
A trainable feed-forward network with any number of hidden layers, ported from tinymind’s NeuralNetwork<ValueType, Inputs, HiddenLayers<...>, Outputs, ...> (cpp/neuralnet.hpp, the chain-based path).
neuralnet
A multilayer perceptron with one hidden layer, ported from the NeuralNetwork / MultilayerPerceptron path of tinymind cpp/neuralnet.hpp, in two forms: MultilayerPerceptron trains on the device, and InferencePerceptron only runs a trained network (C++ IsTrainable = false).
optimizer
Weight-update rules for the trainable networks, ported from tinymind cpp/adam.hpp and cpp/rmsprop.hpp (C++ TransferFunctionOptimizerPolicy).
qformat
Signed fixed-point Q-format arithmetic, ported from tinymind cpp/qformat.hpp.
rand
A bit-exact re-implementation of glibc’s srand()/rand().