pub struct ScheduledSampling { /* private fields */ }Expand description
Scheduled sampling for training a recurrent network on its own outputs
(C++ ScheduledSampling, cpp/teacherForcing.hpp).
When a network is trained to predict a sequence one step ahead, each
step’s input can be the true value (teacher forcing) or the network’s own
previous prediction. Training only on true values leaves the network
unprepared for its own errors when it later runs free; scheduled sampling
starts with the true value every time and, over total_decay_steps
training steps, moves linearly to the prediction every time.
select_input picks one of the two. Inside the
decay window it rolls draw() % 1000 and returns the true value when the
roll is below (total - step) * 1000 / total; from step total on it
returns the prediction without calling draw. tinymind (C++) rolls with
rand(); pass the same generator, for example
|| rng.next_i32() as u32 with GlibcRand, to
reproduce a C++ run. Call step once per training step.
use tinymind::rand::GlibcRand;
use tinymind::training::ScheduledSampling;
let mut rng = GlibcRand::new(7);
let mut sampler = ScheduledSampling::new(1000);
let (truth, prediction) = (1.0, 0.5);
// Step 0: the ratio is 1, so the true value always wins.
assert_eq!(sampler.select_input(truth, prediction, || rng.next_i32() as u32), truth);
for _ in 0..1000 {
sampler.step();
}
// After the decay window, always the prediction.
assert_eq!(sampler.teacher_forcing_ratio(), 0.0);
assert_eq!(sampler.select_input(truth, prediction, || unreachable!()), prediction);Implementations§
Source§impl ScheduledSampling
impl ScheduledSampling
Sourcepub const fn new(total_decay_steps: usize) -> Self
pub const fn new(total_decay_steps: usize) -> Self
A schedule that decays from always-true to always-prediction over
total_decay_steps training steps. With 0 it returns the prediction
from the start.
Sourcepub fn select_input<V>(
&self,
ground_truth: V,
prediction: V,
draw: impl FnOnce() -> u32,
) -> V
pub fn select_input<V>( &self, ground_truth: V, prediction: V, draw: impl FnOnce() -> u32, ) -> V
Return ground_truth or prediction for the next input, as described
on the type. draw is called once inside the decay window and never
after it.
Sourcepub fn teacher_forcing_ratio(&self) -> f64
pub fn teacher_forcing_ratio(&self) -> f64
The share of inputs that are true values: 1 at step 0, falling
linearly to 0 at total_decay_steps.
Sourcepub fn current_step(&self) -> usize
pub fn current_step(&self) -> usize
Training steps taken so far, at most total_decay_steps.
Sourcepub fn total_decay_steps(&self) -> usize
pub fn total_decay_steps(&self) -> usize
The length of the decay window.