pub struct InferencePerceptron<V, H, O, const IN: usize, const HID: usize, const OUT: usize> {
pub input_weights: [[V; HID]; IN],
pub input_bias_weights: [V; HID],
pub hidden_weights: [[V; OUT]; HID],
pub hidden_bias_weights: [V; OUT],
/* private fields */
}Expand description
An inference-only multilayer perceptron: IN inputs -> HID hidden neurons
(activation H) -> OUT outputs (activation O), holding only its weights.
C++: MultilayerPerceptron<ValueType, IN, 1, HID, OUT, TransferFunctions, false>, the untrainable network. Train offline, or on a host with
MultilayerPerceptron, then ship this: it stores one value per connection
instead of four, has no per-neuron state, and has no training code. Its
outputs are bit-identical to the trainable network’s with the same weights.
feed_forward takes &self and keeps nothing between
calls, and new is a const fn, so a trained network can be a
static (in flash on a microcontroller):
use tinymind::{InferencePerceptron, Q8_8, Sigmoid, Tanh};
const fn q(x: i16) -> Q8_8 {
Q8_8::from_int(x as i32)
}
// XOR by hand: hidden neuron 0 is OR, hidden neuron 1 is AND, and the output
// is "OR and not AND". Trained weights, e.g. from
// `MultilayerPerceptron::to_inference` on a host, go here the same way.
static XOR: InferencePerceptron<Q8_8, Tanh, Sigmoid, 2, 2, 1> = InferencePerceptron::new(
[[q(4), q(4)], [q(4), q(4)]], // input -> hidden
[q(-2), q(-6)], // input bias -> hidden
[[q(6)], [q(-6)]], // hidden -> output
[q(-3)], // hidden bias -> output
);
let half = Q8_8::from_raw(128);
for (a, b, want) in [(0, 0, false), (0, 1, true), (1, 0, true), (1, 1, false)] {
let [y] = XOR.feed_forward(&[q(a), q(b)]);
assert_eq!(y > half, want);
}Fields§
§input_weights: [[V; HID]; IN]Input-to-hidden weights: input_weights[input][hidden].
input_bias_weights: [V; HID]Input-layer bias to hidden neuron weights: input_bias_weights[hidden].
Hidden-to-output weights: hidden_weights[hidden][output].
Hidden-layer bias to output neuron weights: hidden_bias_weights[output].
Implementations§
Source§impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> InferencePerceptron<V, H, O, IN, HID, OUT>
impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> InferencePerceptron<V, H, O, IN, HID, OUT>
Source§impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> InferencePerceptron<V, H, O, IN, HID, OUT>
impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> InferencePerceptron<V, H, O, IN, HID, OUT>
Sourcepub fn feed_forward(&self, inputs: &[V; IN]) -> [V; OUT]
pub fn feed_forward(&self, inputs: &[V; IN]) -> [V; OUT]
One forward pass: the output-layer activations for inputs.