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>
impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> MultilayerPerceptron<V, H, O, IN, HID, OUT, Sgd>
Sourcepub fn new(random_weight: impl FnMut() -> V) -> Self
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>
impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> MultilayerPerceptron<V, H, O, IN, HID, OUT, Opt>
Sourcepub fn with_optimizer(random_weight: impl FnMut() -> V, optimizer: Opt) -> Self
pub fn with_optimizer(random_weight: impl FnMut() -> V, optimizer: Opt) -> Self
Sourcepub fn set_learning_rate(&mut self, rate: V)
pub fn set_learning_rate(&mut self, rate: V)
Replace the learning rate (default 2^-2, tinymind (C++)’s
DefaultNetworkInitializer).
Sourcepub fn set_momentum_rate(&mut self, rate: V)
pub fn set_momentum_rate(&mut self, rate: V)
Replace the momentum rate (default 2^-1).
Sourcepub fn set_acceleration_rate(&mut self, rate: V)
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).
Sourcepub fn set_gradient_clip(&mut self, range: Option<(V, V)>)
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)).
Sourcepub fn learning_rate(&self) -> V
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.
Sourcepub fn feed_forward(&mut self, inputs: &[V; IN])
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.
Sourcepub fn learned_values(&self) -> [V; OUT]
pub fn learned_values(&self) -> [V; OUT]
The output-layer activations from the latest
feed_forward.
Sourcepub fn output_pre_activations(&self) -> [V; OUT]
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.
Sourcepub fn predicted_class(&self) -> usize
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.
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.
Sourcepub fn calculate_error(&self, targets: &[V; OUT]) -> V
pub fn calculate_error(&self, targets: &[V; OUT]) -> V
Mean-squared error against targets (C++ MeanSquaredErrorCalculator).
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.
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.
Sourcepub fn optimizer(&self) -> &Opt
pub fn optimizer(&self) -> &Opt
The optimizer, for example to read Adam::timestep.
Sourcepub fn input_weights(&self) -> [[V; HID]; IN]
pub fn input_weights(&self) -> [[V; HID]; IN]
Input-to-hidden weights: input_weights()[input][hidden].
Sourcepub fn input_bias_weights(&self) -> [V; HID]
pub fn input_bias_weights(&self) -> [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].
Sourcepub fn set_input_weights(&mut self, weights: &[[V; HID]; IN])
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.
Sourcepub fn set_input_bias_weights(&mut self, weights: &[V; HID])
pub fn set_input_bias_weights(&mut self, weights: &[V; HID])
Replace the input-layer bias weights (C++
setInputLayerBiasWeightForConnection).
Replace the hidden-to-output weights (C++
setHiddenLayerWeightForNeuronAndConnection).
Replace the hidden-layer bias weights (C++
setHiddenLayerBiasNeuronWeightForConnection).
Sourcepub fn set_weights(&mut self, from: &InferencePerceptron<V, H, O, IN, HID, OUT>)
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.
Sourcepub fn to_inference(&self) -> InferencePerceptron<V, H, O, IN, HID, OUT>
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.