Skip to main content

NeuralNetwork

Struct NeuralNetwork 

Source
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>

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

Source

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

As new, trained with optimizer, for example Adam.

Source§

impl<V: Scalar, L: Layers<Value = V>, Opt: Optimizer<V>> NeuralNetwork<L, Opt>

Source

pub fn optimizer(&self) -> &Opt

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

Source

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

Replace the learning rate (default 2^-2).

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). C++ GradientClipByValue<ValueType, I, F> is Some((-b, b)) with b = QValue(I, F); 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: &L::Inputs)

Run one forward pass. Read the result with learned_values.

Source

pub fn learned_values(&self) -> L::Outputs

The output layer’s activations from the latest forward pass.

Source

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.

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 calculate_error(&self, targets: &L::Outputs) -> V

Mean-squared error of the latest forward pass against targets (C++ MeanSquaredErrorCalculator).

Source

pub fn train(&mut self, targets: &L::Outputs)

One back-propagation step towards targets for the latest forward pass (C++ ChainBackPropagationPolicy).

Source

pub fn layers(&self) -> &L

The layers, to read their weights (nn.layers().0.weights()).

Source

pub fn layers_mut(&mut self) -> &mut L

The layers, to load weights (nn.layers_mut().0.set_weights(..)).

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<L, Opt> Freeze for NeuralNetwork<L, Opt>
where L: Freeze, <L as LayersImpl>::Inputs: Freeze, <L as LayersImpl>::Value: Freeze, Opt: Freeze,

§

impl<L, Opt> RefUnwindSafe for NeuralNetwork<L, Opt>
where L: RefUnwindSafe, <L as LayersImpl>::Inputs: RefUnwindSafe, <L as LayersImpl>::Value: RefUnwindSafe, Opt: RefUnwindSafe,

§

impl<L, Opt> Send for NeuralNetwork<L, Opt>
where L: Send, <L as LayersImpl>::Inputs: Send, <L as LayersImpl>::Value: Send, Opt: Send,

§

impl<L, Opt> Sync for NeuralNetwork<L, Opt>
where L: Sync, <L as LayersImpl>::Inputs: Sync, <L as LayersImpl>::Value: Sync, Opt: Sync,

§

impl<L, Opt> Unpin for NeuralNetwork<L, Opt>
where L: Unpin, <L as LayersImpl>::Inputs: Unpin, <L as LayersImpl>::Value: Unpin, Opt: Unpin,

§

impl<L, Opt> UnsafeUnpin for NeuralNetwork<L, Opt>
where L: UnsafeUnpin, <L as LayersImpl>::Inputs: UnsafeUnpin, <L as LayersImpl>::Value: UnsafeUnpin, Opt: UnsafeUnpin,

§

impl<L, Opt> UnwindSafe for NeuralNetwork<L, Opt>
where L: UnwindSafe, <L as LayersImpl>::Inputs: UnwindSafe, <L as LayersImpl>::Value: 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.