1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Rust port of the tinymind (C++) neural network library. Both libraries run
//! networks in fixed point (Q formats) and in floating point (`f32` and `f64`).
//!
//! Scope: Q16.16 and Q8.8 arithmetic and a fixed-point square root ([`Sqrt`]); ReLU, [`Linear`] and the tanh and sigmoid
//! lookup-table activations; `f32` and `f64` as network value types (their
//! [`Tanh`] and [`Sigmoid`] need the `std` or `libm` feature, as do the
//! floating-point `AdamFloat` and `RmsPropFloat` and the `f32`/`f64` softmax
//! of `head::DenseHead`); trainable networks with up
//! to seven hidden layers
//! ([`NeuralNetwork`] over [`Dense`] layers, trained with SGD and momentum,
//! [`Adam`] or [`RmsProp`], with optional gradient clipping, L2 weight decay
//! and a step-decay learning-rate schedule ([`training`], which also has
//! early stopping; the decay and the schedule also apply to
//! [`MultilayerPerceptron`] and [`recurrent::LstmNetwork`])) and their
//! inference-only form ([`InferenceNetwork`] over [`InferenceDense`] layers:
//! the weights only, tinymind (C++)'s `IsTrainable = false`); a one-hidden-layer
//! perceptron that
//! trains on the device ([`MultilayerPerceptron`]) or only runs trained weights
//! ([`InferencePerceptron`], tinymind (C++)'s `IsTrainable = false`), int8
//! affine-quantized inference ([`int8`]: dense layers, 2D convolutions (standard,
//! depthwise and pointwise), pooling, elementwise addition and multiplication,
//! padding, concatenation, quantized activations,
//! a TFLite-exact softmax, LSTM and GRU cells, and a radix-2 FFT on 16-bit
//! samples), quantized decision trees and gradient-boosted
//! ensembles ([`tree`]), Q-learning, tabular or with a deep Q-network
//! ([`qlearn`]), LSTM recurrent
//! networks ([`recurrent::LstmNetwork`]), and head-only fine-tuning ([`head`]:
//! a trainable dense head and a nearest-class-mean head that adapt a frozen
//! model on the device from a few labelled samples). Every part produces the same bits as
//! tinymind (C++) (floating point: with `std` on a glibc host). [`rand`] re-implements glibc's `rand()`, which tinymind (C++)'s
//! examples use for weight initialisation and sampling, so they can be
//! reproduced bit for bit. The library is `no_std` and allocation-free, like tinymind (C++)'s
//! freestanding build.
//!
//! # Features
//!
//! - `std`: host-side int8 calibration (`int8::calibration`), and the math
//! (`tanh`, `exp`, `pow`, `sqrt`) of the `f32`/`f64` [`Tanh`], [`Sigmoid`],
//! `AdamFloat`, `RmsPropFloat` and `head::SoftmaxExp`, from the platform (bit-identical to
//! tinymind (C++) on a glibc host).
//! - `libm`: the same float math without `std`, from the `libm` crate (for a
//! Cortex-M4F, say); within a few ulps of glibc, not bit-identical.
//!
//! # Example
//!
//! Train a 2-4-1 network on XOR in Q16.16, as tinymind (C++)'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}");
//! }
//! ```
// Host-side calibration (`int8::calibration`) needs f32::exp and f32::tanh.
extern crate std;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Fixed;
pub use ;