Skip to main content

InferencePerceptron

Struct InferencePerceptron 

Source
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_weights: [[V; OUT]; HID]

Hidden-to-output weights: hidden_weights[hidden][output].

§hidden_bias_weights: [V; OUT]

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>

Source

pub const fn new( input_weights: [[V; HID]; IN], input_bias_weights: [V; HID], hidden_weights: [[V; OUT]; HID], hidden_bias_weights: [V; OUT], ) -> Self

A network with these weights, laid out as the MultilayerPerceptron weight getters return them.

Source§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> InferencePerceptron<V, H, O, IN, HID, OUT>
where V: Scalar, H: Activation<V>, O: Activation<V>,

Source

pub fn feed_forward(&self, inputs: &[V; IN]) -> [V; OUT]

One forward pass: the output-layer activations for inputs.

Trait Implementations§

Source§

impl<V: Copy, H, O, const IN: usize, const HID: usize, const OUT: usize> Clone for InferencePerceptron<V, H, O, IN, HID, OUT>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V: Copy, H, O, const IN: usize, const HID: usize, const OUT: usize> Copy for InferencePerceptron<V, H, O, IN, HID, OUT>

Source§

impl<V: Debug, H, O, const IN: usize, const HID: usize, const OUT: usize> Debug for InferencePerceptron<V, H, O, IN, HID, OUT>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V: Eq, H, O, const IN: usize, const HID: usize, const OUT: usize> Eq for InferencePerceptron<V, H, O, IN, HID, OUT>

Source§

impl<V: PartialEq, H, O, const IN: usize, const HID: usize, const OUT: usize> PartialEq for InferencePerceptron<V, H, O, IN, HID, OUT>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> Forget for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> Freeze for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> Move for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> RefUnwindSafe for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> Send for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> Sync for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> Unpin for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> UnsafeUnpin for InferencePerceptron<V, H, O, IN, HID, OUT>

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> UnwindSafe for InferencePerceptron<V, H, O, IN, HID, OUT>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.