pub struct NeuralNetwork<L: Layers, Opt = Sgd> { /* private fields */ }Expand description
A trainable feed-forward network over a tuple of Dense layers (C++
NeuralNetwork<..., HiddenLayers<...>, ...>). See the module
documentation for an example.
Opt is the optimizer: Sgd (the default) or an adaptive one from
optimizer, passed to with_optimizer.
Implementations§
Source§impl<V: Scalar, L: Layers<Value = V> + Default> NeuralNetwork<L, Sgd>
impl<V: Scalar, L: Layers<Value = V> + Default> NeuralNetwork<L, 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 C++ order: each layer’s inputs and then its bias,
from the input side, then one draw per output neuron (the C++ output
neurons own an unused connection whose initialisation consumes a draw).
Source§impl<V: Scalar, L: Layers<Value = V> + Default, Opt: Optimizer<V>> NeuralNetwork<L, Opt>
impl<V: Scalar, L: Layers<Value = V> + Default, Opt: Optimizer<V>> NeuralNetwork<L, 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
Source§impl<V: Scalar, L: Layers<Value = V>, Opt: Optimizer<V>> NeuralNetwork<L, Opt>
impl<V: Scalar, L: Layers<Value = V>, Opt: Optimizer<V>> NeuralNetwork<L, Opt>
Sourcepub fn optimizer(&self) -> &Opt
pub fn optimizer(&self) -> &Opt
The optimizer, for example to read Adam::timestep.
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).
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). C++ GradientClipByValue<ValueType, I, F> is Some((-b, b)) with b = QValue(I, F); 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: &L::Inputs)
pub fn feed_forward(&mut self, inputs: &L::Inputs)
Run one forward pass. Read the result with
learned_values.
Sourcepub fn learned_values(&self) -> L::Outputs
pub fn learned_values(&self) -> L::Outputs
The output layer’s activations from the latest forward pass.
Sourcepub fn output_pre_activations(&self) -> L::Outputs
pub fn output_pre_activations(&self) -> L::Outputs
The output layer’s pre-activations (the logits) for the latest
feed_forward (C++ getOutputPreActivations).
Recomputed from the layer before the output 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.
Sourcepub fn calculate_error(&self, targets: &L::Outputs) -> V
pub fn calculate_error(&self, targets: &L::Outputs) -> V
Mean-squared error of the latest forward pass against targets (C++
MeanSquaredErrorCalculator).
Sourcepub fn train(&mut self, targets: &L::Outputs)
pub fn train(&mut self, targets: &L::Outputs)
One back-propagation step towards targets for the latest forward pass
(C++ ChainBackPropagationPolicy).
Sourcepub fn layers_mut(&mut self) -> &mut L
pub fn layers_mut(&mut self) -> &mut L
The layers, to load weights (nn.layers_mut().0.set_weights(..)).
Sourcepub fn to_inference(&self) -> InferenceNetwork<L::Inference>
pub fn to_inference(&self) -> InferenceNetwork<L::Inference>
The trained network as an InferenceNetwork: the same weights and the
same outputs, bit for bit, without the training state.
Sourcepub fn set_weights(&mut self, from: &InferenceNetwork<L::Inference>)
pub fn set_weights(&mut self, from: &InferenceNetwork<L::Inference>)
Load every weight from an inference network of the same shape, for example to keep training shipped weights on the device. The optimizer state and delta weights are left as they are.