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: Fixed, L: Layers<Value = V> + Default> NeuralNetwork<L, Sgd>
impl<V: Fixed, 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: Fixed, L: Layers<Value = V> + Default, Opt: Optimizer<V>> NeuralNetwork<L, Opt>
impl<V: Fixed, 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: Fixed, L: Layers<Value = V>, Opt: Optimizer<V>> NeuralNetwork<L, Opt>
impl<V: Fixed, 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 gradient to [min, max] before the update, or
None (the default). C++ GradientClipByValue<ValueType, I, F> is
Some((QValue(-I, F), QValue(I, F))); the default
GradientClipByValue<ValueType> is Some((-1.0, 1.0)). Bias weights are
never clipped, as in the C++.
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 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(&self) -> &L
pub fn layers(&self) -> &L
The layers, to read their weights (nn.layers().0.weights()).
Examples found in repository?
182pub fn evaluate(
183 nn: &mut Autoencoder,
184 signal: &mut Signal,
185 out: &mut dyn Write,
186 each: &mut dyn FnMut(&str, i32),
187) -> Result<u32, fmt::Error> {
188 let mut hash = Fnv::new();
189 let (l0, l1) = nn.layers();
190 for row in l0.weights() {
191 for w in row {
192 hash.add(w.raw());
193 }
194 }
195 for w in l0.bias_weights() {
196 hash.add(w.raw());
197 }
198 for row in l1.weights() {
199 for w in row {
200 hash.add(w.raw());
201 }
202 }
203 for w in l1.bias_weights() {
204 hash.add(w.raw());
205 }
206
207 // Threshold: twice the largest error on normal validation windows.
208 let mut max_normal = 0;
209 for _ in 0..NORMAL_WINDOWS {
210 let e = score(nn, &signal.window(Kind::Normal)).raw();
211 hash.add(e);
212 max_normal = max_normal.max(e);
213 }
214 let threshold = max_normal.saturating_mul(2);
215 writeln!(
216 out,
217 "threshold={threshold} (2 x the largest error on {NORMAL_WINDOWS} normal windows, raw Q16.16)"
218 )?;
219
220 let mut flagged = |kind: Kind, name: &str, n: u32, hash: &mut Fnv| {
221 let mut count = 0;
222 for _ in 0..n {
223 let e = score(nn, &signal.window(kind)).raw();
224 hash.add(e);
225 each(name, e);
226 count += u32::from(e > threshold);
227 }
228 count
229 };
230 let fp = flagged(Kind::Normal, "normal", NORMAL_WINDOWS, &mut hash);
231 writeln!(out, "normal: flagged {fp}/{NORMAL_WINDOWS} (false alarms)")?;
232 for (kind, name) in FAULTS {
233 let tp = flagged(kind, name, FAULT_WINDOWS, &mut hash);
234 writeln!(out, "{name}: flagged {tp}/{FAULT_WINDOWS}")?;
235 }
236 writeln!(out, "hash=0x{:08X}", hash.0)?;
237 Ok(hash.0)
238}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(..)).