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: LThe layers, first to last.
Implementations§
Source§impl<L> InferenceNetwork<L>
impl<L> InferenceNetwork<L>
Source§impl<L: InferenceLayers> InferenceNetwork<L>
impl<L: InferenceLayers> InferenceNetwork<L>
Sourcepub fn feed_forward(&self, inputs: &L::Inputs) -> L::Outputs
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more