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>
impl<V, H, O, const IN: usize, const HID: usize, const OUT: usize> LstmNetwork<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 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>,
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>,
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> 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 reset_state(&mut self)
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.
Sourcepub fn feed_forward(&mut self, inputs: &[V; IN])
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.
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):
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.
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 cells’ outputs h from the latest
feed_forward.
Sourcepub fn cell_states(&self) -> [V; HID]
pub fn cell_states(&self) -> [V; HID]
The hidden cells’ states c from the latest
feed_forward.
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: 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.
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.
Sourcepub fn input_weights(&self) -> [[[V; 4]; HID]; IN]
pub fn input_weights(&self) -> [[[V; 4]; HID]; IN]
[input][cell][gate]: the weights from each input to each gate.
Sourcepub fn input_bias_weights(&self) -> [[V; 4]; HID]
pub fn input_bias_weights(&self) -> [[V; 4]; HID]
[cell][gate]: the input bias weights.
Sourcepub fn recurrent_weights(&self) -> [[[V; 4]; HID]; HID]
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.
[cell][output]: the weights from the cells to the outputs.
[output]: the hidden bias weights.
Sourcepub fn set_input_weights(&mut self, weights: &[[[V; 4]; HID]; IN])
pub fn set_input_weights(&mut self, weights: &[[[V; 4]; HID]; IN])
Replace the input weights (see input_weights).
Sourcepub fn set_input_bias_weights(&mut self, weights: &[[V; 4]; HID])
pub fn set_input_bias_weights(&mut self, weights: &[[V; 4]; HID])
Replace the input bias weights.
Sourcepub fn set_recurrent_weights(&mut self, weights: &[[[V; 4]; HID]; HID])
pub fn set_recurrent_weights(&mut self, weights: &[[[V; 4]; HID]; HID])
Replace the recurrent weights (see
recurrent_weights).
Replace the weights from the cells to the outputs.
Replace the hidden bias weights.