Expand description
A trainable feed-forward network with up to seven hidden layers, ported
from tinymind (C++)’s NeuralNetwork<ValueType, Inputs, HiddenLayers<...>, Outputs, ...> (cpp/neuralnet.hpp, the chain-based path).
The C++ network takes its hidden-layer sizes as a variadic template
(HiddenLayers<8, 6>). Rust has no variadic generics, so here a network is
a tuple of Dense layers, each sized by const generics:
use tinymind::{Dense, NeuralNetwork, Q16_16, Relu, Sigmoid};
// 4 inputs -> 8 ReLU -> 6 ReLU -> 3 sigmoid outputs.
type Net = NeuralNetwork<(
Dense<Q16_16, Relu, 4, 8>,
Dense<Q16_16, Relu, 8, 6>,
Dense<Q16_16, Sigmoid, 6, 3>,
)>;
let mut seed = 1u32;
let mut nn = Net::new(|| {
// Any source of small initial weights; here a tiny LCG in [-0.5, 0.5].
seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345);
Q16_16::from_raw((seed >> 16) as i32 % 32_769 - 16_384)
});
let x = [Q16_16::ONE, Q16_16::ZERO, Q16_16::ONE, Q16_16::ZERO];
let target = [Q16_16::ONE, Q16_16::ZERO, Q16_16::ZERO];
for _ in 0..100 {
nn.feed_forward(&x);
nn.train(&target);
}
nn.feed_forward(&x);
let [a, b, c] = nn.learned_values();
assert!(a > b && a > c);
assert_eq!(nn.predicted_class(), 0);To pick a class, rank the output logits with predicted_class()
(output_pre_activations() returns them) rather than learned_values():
the fixed-point sigmoid table is flat beyond ±6, so confident outputs tie.
Adjacent layers must agree (Dense<_, _, 4, 8> then Dense<_, _, 8, _>),
which the compiler checks. Each layer has its own activation; tinymind (C++)’s
network uses one activation for every hidden layer, which is the special
case where they are all the same. Tuples of 1 to 8 layers are supported.
Training is SGD with momentum by default, or Adam or RMSprop (crate::optimizer,
through NeuralNetwork::with_optimizer), with optional gradient clipping
(NeuralNetwork::set_gradient_clip) and optional L2 weight decay and a
step-decay learning-rate schedule (crate::training, wrapping the
optimizer; fixed point only on the C++ side), and every result is bit-identical to the C++
network with the same layer sizes, activations and initial weights (and to
MultilayerPerceptron for one hidden layer).
A trained network becomes an InferenceNetwork of InferenceDense
layers, its weights only, with NeuralNetwork::to_inference (and back with
NeuralNetwork::set_weights); that form can be a static in flash.
The value type is any Scalar: a Q format, f32 or f64.
ReLU and linear layers need nothing more; Tanh and
Sigmoid on floats need the std or libm feature.
use tinymind::{Dense, Linear, NeuralNetwork, Relu};
// A floating-point regression: 2 inputs -> 8 ReLU -> 1 linear output, y = a - b.
let mut seed = 7u32;
let mut nn = NeuralNetwork::<(Dense<f64, Relu, 2, 8>, Dense<f64, Linear, 8, 1>)>::new(|| {
seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345);
f64::from(seed >> 16) / 65_536.0 - 0.5
});
nn.set_learning_rate(0.05);
for step in 0..4000 {
let (a, b) = (f64::from(step % 7) / 7.0, f64::from(step % 5) / 5.0);
nn.feed_forward(&[a, b]);
nn.train(&[a - b]);
}
nn.feed_forward(&[0.8, 0.2]);
assert!((nn.learned_values()[0] - 0.6).abs() < 0.1);Structs§
- Dense
- A fully connected layer:
INinputs (plus a bias) toOUTneurons with activationA, and the training state of every connection. - Inference
Dense - A fully connected layer holding only its weights: the inference-only form
of
Dense(C++IsTrainable = false), forInferenceNetwork. - Inference
Network - An inference-only feed-forward network over a tuple of
InferenceDenselayers: tinymind (C++)’sNeuralNetwork<..., HiddenLayers<...>, ...>withIsTrainable = false, and the any-depth form ofInferencePerceptron. - Neural
Network - A trainable feed-forward network over a tuple of
Denselayers (C++NeuralNetwork<..., HiddenLayers<...>, ...>). See the module documentation for an example.
Traits§
- Inference
Layers - A stack of layers
InferenceNetworkcan run: a tuple of 1 to 8InferenceDenselayers in which each layer’sOUTis the next layer’sIN. Implemented by the crate only. - Layers
- A stack of layers
NeuralNetworkcan run: a tuple of 1 to 8Denselayers in which each layer’sOUTis the next layer’sIN. Implemented by the crate only.