Skip to main content

MultilayerPerceptron

Struct MultilayerPerceptron 

Source
pub struct MultilayerPerceptron<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt = Sgd> { /* private fields */ }
Expand description

IN inputs -> HID hidden neurons (activation H) -> OUT outputs (activation O).

C++: MultilayerPerceptron<ValueType, IN, 1, HID, OUT, TransferFunctions>.

Opt is the optimizer: Sgd (the default) or an adaptive one from optimizer, passed to with_optimizer.

Implementations§

Source§

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

Source

pub fn new(random_weight: impl FnMut() -> V) -> Self

Build the network, trained with Sgd, and initialise its weights from random_weight, in the same order as the C++ constructor: input neurons, input bias, hidden neurons, hidden bias, output neurons.

The C++ code reaches a static generateRandomWeight() policy that reads global rand() state; passing a closure makes that dependency explicit.

Source§

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

Source

pub fn with_optimizer(random_weight: impl FnMut() -> V, optimizer: Opt) -> Self

As new, trained with optimizer, for example Adam.

Source

pub fn set_learning_rate(&mut self, rate: V)

Replace the learning rate (default 2^-2, tinymind (C++)’s DefaultNetworkInitializer).

Source

pub fn set_momentum_rate(&mut self, rate: V)

Replace the momentum rate (default 2^-1).

Source

pub fn set_acceleration_rate(&mut self, rate: V)

Replace the acceleration rate, the weight on the delta weight from two steps back (default 2^-4).

Source

pub fn set_gradient_clip(&mut self, range: Option<(V, V)>)

Clip each weight’s and bias weight’s gradient to [min, max] before the update, or None (the default) to leave gradients alone. C++ GradientClipByValue<ValueType, I, F> is Some((-b, b)) with b = QValue(I, F), so the default GradientClipByValue<ValueType> is Some((-1.0, 1.0)).

Source

pub fn learning_rate(&self) -> V

The learning rate the next training step will use: the one set with set_learning_rate, as a Scheduled optimizer has decayed it so far.

Source

pub fn feed_forward(&mut self, inputs: &[V; IN])

Run one forward pass: load inputs, then compute the hidden and output activations. Read the result with learned_values.

Source

pub fn learned_values(&self) -> [V; OUT]

The output-layer activations from the latest feed_forward.

Source

pub fn output_pre_activations(&self) -> [V; OUT]

The output layer’s pre-activations (the logits) for the latest feed_forward (C++ getOutputPreActivations).

Recomputed from the hidden layer with the sum feed_forward forms, so they match it bit for bit and cost no memory.

Call it before train: training changes the weights it is recomputed from.

Source

pub fn predicted_class(&self) -> usize

The index of the largest output pre-activation for the latest feed_forward, ties to the lower index (C++ getPredictedClass).

Use it, not an argmax of learned_values, to pick a class: the fixed-point sigmoid table is flat beyond ±6, so confident outputs tie there and an argmax can only take the first.

Call it before train: training changes the weights it is recomputed from.

Source

pub fn hidden_values(&self) -> [V; HID]

The hidden-layer activations from the latest feed_forward (C++ getLastHiddenLayer().getOutputValueForNeuron): the embedding, when the trained network becomes a frozen model with a head on top.

Source

pub fn calculate_error(&self, targets: &[V; OUT]) -> V

Mean-squared error against targets (C++ MeanSquaredErrorCalculator).

Source

pub fn train(&mut self, targets: &[V; OUT])

One back-propagation step with the network’s optimizer (C++ ChainBackPropagationPolicy).

Source§

impl<V: Copy, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>

Weight getters. These need no activation bounds, so they live in their own impl block. Each returns a whole matrix rather than taking an index, so none of them can panic; the layout matches the order tinymind (C++)’s NetworkPropertiesFileManager writes the weights in.

Source

pub fn optimizer(&self) -> &Opt

The optimizer, for example to read Adam::timestep.

Source

pub fn input_weights(&self) -> [[V; HID]; IN]

Input-to-hidden weights: input_weights()[input][hidden].

Source

pub fn input_bias_weights(&self) -> [V; HID]

Input-layer bias to hidden neuron weights: input_bias_weights()[hidden].

Source

pub fn hidden_weights(&self) -> [[V; OUT]; HID]

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

Source

pub fn hidden_bias_weights(&self) -> [V; OUT]

Hidden-layer bias to output neuron weights: hidden_bias_weights()[output].

Source

pub fn set_input_weights(&mut self, weights: &[[V; HID]; IN])

Replace the input-to-hidden weights, laid out as input_weights returns them. Only the weights change; the training state (delta weights, gradients) is kept, as with the C++ setInputLayerWeightForNeuronAndConnection.

Source

pub fn set_input_bias_weights(&mut self, weights: &[V; HID])

Replace the input-layer bias weights (C++ setInputLayerBiasWeightForConnection).

Source

pub fn set_hidden_weights(&mut self, weights: &[[V; OUT]; HID])

Replace the hidden-to-output weights (C++ setHiddenLayerWeightForNeuronAndConnection).

Source

pub fn set_hidden_bias_weights(&mut self, weights: &[V; OUT])

Replace the hidden-layer bias weights (C++ setHiddenLayerBiasNeuronWeightForConnection).

Source

pub fn set_weights(&mut self, from: &InferencePerceptron<V, H, O, IN, HID, OUT>)

Load every weight from an inference network of the same shape, for example to fine-tune shipped weights on the device.

Source

pub fn to_inference(&self) -> InferencePerceptron<V, H, O, IN, HID, OUT>

The trained network as an InferencePerceptron: the same weights and the same outputs, bit for bit, without the training state.

Auto Trait Implementations§

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> Freeze for MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
where V: Freeze, Opt: Freeze,

§

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

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> Send for MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
where V: Send, Opt: Send,

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> Sync for MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
where V: Sync, Opt: Sync,

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> Unpin for MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
where V: Unpin, Opt: Unpin,

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> UnsafeUnpin for MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
where V: UnsafeUnpin, Opt: UnsafeUnpin,

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> UnwindSafe for MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
where V: UnwindSafe, Opt: 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.