Skip to main content

InferenceNetwork

Struct InferenceNetwork 

Source
pub struct InferenceNetwork<L> {
    pub layers: L,
}
Expand description

An inference-only feed-forward network over a tuple of InferenceDense layers: tinymind (C++)’s NeuralNetwork<..., HiddenLayers<...>, ...> with IsTrainable = false, and the any-depth form of InferencePerceptron.

Train with NeuralNetwork (on a host or on the device), then ship this: one value per connection, no per-neuron state and no training code. Its outputs are bit-identical to the trainable network’s with the same weights. feed_forward takes &self, and new and InferenceDense::new are const fn, so a trained network can be a static:

use tinymind::{InferenceDense, InferenceNetwork, Q16_16, Relu, Sigmoid};

const fn q(x: i32) -> Q16_16 {
    Q16_16::from_int(x)
}

// 2 inputs -> 2 ReLU -> 2 ReLU -> 1 sigmoid output: XOR by hand (OR minus
// twice AND, then a margin). Weights from `NeuralNetwork::to_inference`
// go here the same way.
static XOR: InferenceNetwork<(
    InferenceDense<Q16_16, Relu, 2, 2>,
    InferenceDense<Q16_16, Relu, 2, 2>,
    InferenceDense<Q16_16, Sigmoid, 2, 1>,
)> = InferenceNetwork::new((
    InferenceDense::new([[q(1), q(1)], [q(1), q(1)]], [q(0), q(-1)]), // [OR-ish, AND]
    InferenceDense::new([[q(1), q(0)], [q(-2), q(0)]], [q(0), q(0)]), // OR - 2 AND
    InferenceDense::new([[q(8)], [q(0)]], [q(-4)]),
));

let half = Q16_16::from_raw(1 << 15);
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§

§layers: L

The layers, first to last.

Implementations§

Source§

impl<L> InferenceNetwork<L>

Source

pub const fn new(layers: L) -> Self

A network over these layers.

Source§

impl<L: InferenceLayers> InferenceNetwork<L>

Source

pub fn feed_forward(&self, inputs: &L::Inputs) -> L::Outputs

One forward pass: the output layer’s activations. Nothing is kept between calls.

Auto Trait Implementations§

§

impl<L> Freeze for InferenceNetwork<L>
where L: Freeze,

§

impl<L> RefUnwindSafe for InferenceNetwork<L>
where L: RefUnwindSafe,

§

impl<L> Send for InferenceNetwork<L>
where L: Send,

§

impl<L> Sync for InferenceNetwork<L>
where L: Sync,

§

impl<L> Unpin for InferenceNetwork<L>
where L: Unpin,

§

impl<L> UnsafeUnpin for InferenceNetwork<L>
where L: UnsafeUnpin,

§

impl<L> UnwindSafe for InferenceNetwork<L>
where L: UnwindSafe,

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> 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, 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.