Skip to main content

LstmNetwork

Struct LstmNetwork 

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

An LSTM network: IN inputs -> HID LSTM cells (cell candidate activation H) -> OUT outputs (activation O).

C++: LstmNeuralNetwork<ValueType, IN, HiddenLayers<HID>, OUT, TransferFunctions> with H and O the transfer functions’ hidden and output activations. See the module documentation for the cell equations and an example.

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> LstmNetwork<V, H, O, IN, HID, OUT, Sgd>
where V: Scalar, H: Activation<V>, O: Activation<V>, Sigmoid: Activation<V>, Tanh: 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 C++ constructor’s order: each input’s connections, the input bias’s, each hidden cell’s connections to the outputs, the hidden bias’s, the output neurons’ (one unused each), and last each recurrent connection.

Source§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> LstmNetwork<V, H, O, IN, HID, OUT, Opt>
where V: Scalar, H: Activation<V>, O: Activation<V>, Sigmoid: Activation<V>, Tanh: 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> 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 reset_state(&mut self)

Clear the recurrent state: every cell’s output h and state c become 0, so the next feed_forward starts a new sequence (C++ resetState). Weights and optimizer state are kept.

Source

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

One time step: load inputs, latch the previous step’s hidden outputs as h[t-1], update every cell and compute the outputs. Read them 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): bias term + (0 + sum of h * w), the sum feed_forward forms (it calls this), recomputed from the hidden outputs at no memory cost.

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_outputs(&self) -> [V; HID]

The hidden cells’ outputs h from the latest feed_forward.

Source

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

The hidden cells’ states c from the latest feed_forward.

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 training step for the latest forward pass (C++ ChainBackPropagationThruTimePolicy): the error is propagated to the gates of the current step and to the weights from the inputs, the biases and h[t-1], not further back in time.

Source§

impl<V: Scalar, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> LstmNetwork<V, H, O, IN, HID, OUT, Opt>

Weight getters and setters. Each takes or returns a whole matrix, so none can panic. Gates are indexed as GATE_CANDIDATE, GATE_INPUT, GATE_FORGET and GATE_OUTPUT.

Source

pub fn optimizer(&self) -> &Opt

The optimizer, with its state.

Source

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

[input][cell][gate]: the weights from each input to each gate.

Source

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

[cell][gate]: the input bias weights.

Source

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

[from cell][to cell][gate]: the weights from each cell’s previous output h[t-1] to each gate.

Source

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

[cell][output]: the weights from the cells to the outputs.

Source

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

[output]: the hidden bias weights.

Source

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

Replace the input weights (see input_weights).

Source

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

Replace the input bias weights.

Source

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

Replace the recurrent weights (see recurrent_weights).

Source

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

Replace the weights from the cells to the outputs.

Source

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

Replace the hidden bias weights.

Auto Trait Implementations§

§

impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt> Freeze for LstmNetwork<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 LstmNetwork<V, H, O, IN, HID, OUT, Opt>

§

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