Skip to main content

ScheduledSampling

Struct ScheduledSampling 

Source
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

Source

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.

Source

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.

Source

pub fn step(&mut self)

Advance one training step (no further than total_decay_steps).

Source

pub fn reset(&mut self)

Back to step 0: the true value every time.

Source

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.

Source

pub fn current_step(&self) -> usize

Training steps taken so far, at most total_decay_steps.

Source

pub fn total_decay_steps(&self) -> usize

The length of the decay window.

Trait Implementations§

Source§

impl Clone for ScheduledSampling

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ScheduledSampling

Source§

impl Debug for ScheduledSampling

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ScheduledSampling

Source§

impl PartialEq for ScheduledSampling

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ScheduledSampling

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.