Skip to main content

Module training

Module training 

Source
Expand description

Training controls for the trainable networks, ported from tinymind (C++) cpp/weightDecay.hpp, cpp/learningRateSchedule.hpp, cpp/earlyStopping.hpp and cpp/teacherForcing.hpp.

  • WeightDecay (L2) shrinks every connection weight, but no bias weight, by learning rate * lambda * weight before each update. With plain SGD (no momentum) this is the gradient step of an L2 penalty (lambda / 2) * w^2; the shrink does not enter the momentum terms, so with momentum, and under Adam and RMSprop, it is decoupled weight decay (AdamW). In fixed point it keeps weights from growing until they overflow.
  • Scheduled with a StepDecay multiplies the learning rate by a factor every interval training steps. Read the current rate with the network’s learning_rate(). Any other schedule is a call to set_learning_rate between steps.
  • EarlyStopping watches an error (training or validation) and says when it has not improved for patience checks in a row. It is not tied to a network; the training loop asks it.
  • ScheduledSampling picks, for each step of a sequence, whether a recurrent network’s next input is the true value or its own previous prediction, moving from always-true to always-prediction over a number of training steps (teacher forcing with a decaying ratio). Like EarlyStopping, the training loop asks it.

WeightDecay and Scheduled wrap an optimizer (Sgd, Adam, …) and are passed to with_optimizer on MultilayerPerceptron, NeuralNetwork or LstmNetwork, as tinymind (C++) takes L2WeightDecay and StepDecaySchedule as policies. A network built without them compiles no decay or schedule code and stores nothing for them. With them, fixed-point results are bit-identical to the C++ network built with the same policies. tinymind (C++)’s policies are fixed-point only, so for f32 and f64 there is no C++ counterpart to compare with.

The wrappers are part of the network’s optimizer, which is fixed when the network is built: to change lambda or the schedule, build the wrapper with the values wanted, or adjust the rate between steps with set_learning_rate.

use tinymind::training::{Scheduled, StepDecay, WeightDecay};
use tinymind::{MultilayerPerceptron, Q16_16, Sgd, Sigmoid, Tanh};

// tinymind (C++)'s defaults for Q16.16: lambda 66/65536 (0.001), and the
// rate times 0.9 every 1000 steps.
let optimizer = Scheduled::new(
    WeightDecay::new(Sgd, Q16_16::from_raw(66)),
    StepDecay::<Q16_16>::default(),
);
let mut seed = 1u32;
let mut nn = MultilayerPerceptron::<Q16_16, Tanh, Sigmoid, 2, 4, 1, _>::with_optimizer(
    || {
        seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345);
        Q16_16::from_raw((seed >> 16) as i32 % 32_769 - 16_384)
    },
    optimizer,
);

let samples = [([0, 0], 0), ([0, 1], 1), ([1, 0], 1), ([1, 1], 0)];
for step in 0..2_500 {
    let (x, t) = samples[step % 4];
    nn.feed_forward(&x.map(Q16_16::from_int));
    nn.train(&[Q16_16::from_int(t)]);
}
// Two decays so far: 1/4 * 0.9 * 0.9, each product floored.
let factor = Q16_16::from_raw(58_982);
assert_eq!(nn.learning_rate(), Q16_16::from_raw(16_384) * factor * factor);
assert_eq!(nn.optimizer().schedule.steps_since_decay(), 500);

Structs§

EarlyStopping
Early stopping (C++ EarlyStopping): reports when the error has not improved for patience checks in a row.
Scheduled
A learning-rate schedule around an optimizer (C++ StepDecaySchedule as the LearningRateSchedulePolicy): after each update the network’s learning rate becomes schedule.step(rate).
ScheduledSampling
Scheduled sampling for training a recurrent network on its own outputs (C++ ScheduledSampling, cpp/teacherForcing.hpp).
StepDecay
Step-decay learning-rate schedule (C++ StepDecaySchedule): every interval training steps the learning rate is multiplied by factor.
WeightDecay
L2 weight decay around an optimizer (C++ L2WeightDecay): before each update every connection weight, but no bias weight, becomes w - (learning rate * lambda) * w, then optimizer takes its step.