Skip to main content

BackpropModule

Trait BackpropModule 

Source
pub trait BackpropModule<X>: TracedModule<X> {
    type SelfGrads;

    // Required methods
    fn backprop(
        &self,
        trace: &<Self as TracedModule<X>>::Trace,
        grads_wrt_output: <Self as Module<X>>::Output,
    ) -> (X, Self::SelfGrads);
    fn update(
        &mut self,
        applyer: &mut impl GradApplyer,
        updates: Self::SelfGrads,
    ) -> Result<(), Error>;

    // Provided methods
    fn new_momentum(
        &self,
        params: TrainParams,
        momentum_coefficient: f32,
    ) -> Momentum<Self::SelfGrads>
       where <Self as BackpropModule<X>>::SelfGrads: Gradients { ... }
    fn new_rmsprop(
        &self,
        params: TrainParams,
        beta: f32,
    ) -> RMSProp<Self::SelfGrads>
       where <Self as BackpropModule<X>>::SelfGrads: Gradients,
             <Self::SelfGrads as Gradients>::Concrete: Float { ... }
    fn new_rmsprop_with_momentum(
        &self,
        params: TrainParams,
        momentum_coefficient: f32,
        beta: f32,
    ) -> RMSProp<Self::SelfGrads>
       where <Self as BackpropModule<X>>::SelfGrads: Gradients,
             <Self::SelfGrads as Gradients>::Concrete: Float { ... }
}
Expand description

Some sequential computation which can perform backprop on itself given trace state and the gradients of its outputs: computing parameter updates for itself.

Trainable layers implement this trait.

Relies on behavior from the TracedModule trait.

Required Associated Types§

Source

type SelfGrads

Type describing movement in the modules’ own parameters in response to backpropagation.

Required Methods§

Source

fn backprop( &self, trace: &<Self as TracedModule<X>>::Trace, grads_wrt_output: <Self as Module<X>>::Output, ) -> (X, Self::SelfGrads)

Computes gradients for this layer/module, given tracing state from forward execution, and the gradients of the output.

Returns the gradients with respect to the input to the layer/module, as well as gradients with respect to parameters (needed to call update()).

Source

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Applies a gradient update step, given (Self::SelfGrads) and a GradApplyer.

While updates describes the change in parameters, applyer is used to change the parameters of this layer, which may include applying any kind of clipping or regularization.

Provided Methods§

Source

fn new_momentum( &self, params: TrainParams, momentum_coefficient: f32, ) -> Momentum<Self::SelfGrads>
where <Self as BackpropModule<X>>::SelfGrads: Gradients,

Returns a GradApplyer object needed to train using SGD + momentum.

Source

fn new_rmsprop( &self, params: TrainParams, beta: f32, ) -> RMSProp<Self::SelfGrads>
where <Self as BackpropModule<X>>::SelfGrads: Gradients, <Self::SelfGrads as Gradients>::Concrete: Float,

Returns a GradApplyer object needed to train using rmsprop.

Source

fn new_rmsprop_with_momentum( &self, params: TrainParams, momentum_coefficient: f32, beta: f32, ) -> RMSProp<Self::SelfGrads>
where <Self as BackpropModule<X>>::SelfGrads: Gradients, <Self::SelfGrads as Gradients>::Concrete: Float,

Returns a GradApplyer object needed to train using rmsprop + momentum.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<Input, M1: BackpropModule<Input>, M2: BackpropModule<M1::Output>, M3: BackpropModule<M2::Output>, M4: BackpropModule<M3::Output>, M5: BackpropModule<M4::Output>, M6: BackpropModule<M5::Output>> BackpropModule<Input> for (M1, M2, M3, M4, M5, M6)

Source§

type SelfGrads = (<M1 as BackpropModule<Input>>::SelfGrads, <M2 as BackpropModule<<M1 as Module<Input>>::Output>>::SelfGrads, <M3 as BackpropModule<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::SelfGrads, <M4 as BackpropModule<<M3 as Module<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::Output>>::SelfGrads, <M5 as BackpropModule<<M4 as Module<<M3 as Module<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::Output>>::Output>>::SelfGrads, <M6 as BackpropModule<<M5 as Module<<M4 as Module<<M3 as Module<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::Output>>::Output>>::Output>>::SelfGrads)

Source§

fn backprop( &self, trace: &<Self as TracedModule<Input>>::Trace, next_grads: <Self as Module<Input>>::Output, ) -> (Input, Self::SelfGrads)

Source§

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Source§

impl<Input, M1: BackpropModule<Input>, M2: BackpropModule<M1::Output>, M3: BackpropModule<M2::Output>, M4: BackpropModule<M3::Output>, M5: BackpropModule<M4::Output>> BackpropModule<Input> for (M1, M2, M3, M4, M5)

Source§

type SelfGrads = (<M1 as BackpropModule<Input>>::SelfGrads, <M2 as BackpropModule<<M1 as Module<Input>>::Output>>::SelfGrads, <M3 as BackpropModule<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::SelfGrads, <M4 as BackpropModule<<M3 as Module<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::Output>>::SelfGrads, <M5 as BackpropModule<<M4 as Module<<M3 as Module<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::Output>>::Output>>::SelfGrads)

Source§

fn backprop( &self, trace: &<Self as TracedModule<Input>>::Trace, next_grads: <Self as Module<Input>>::Output, ) -> (Input, Self::SelfGrads)

Source§

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Source§

impl<Input, M1: BackpropModule<Input>, M2: BackpropModule<M1::Output>, M3: BackpropModule<M2::Output>, M4: BackpropModule<M3::Output>> BackpropModule<Input> for (M1, M2, M3, M4)

Source§

type SelfGrads = (<M1 as BackpropModule<Input>>::SelfGrads, <M2 as BackpropModule<<M1 as Module<Input>>::Output>>::SelfGrads, <M3 as BackpropModule<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::SelfGrads, <M4 as BackpropModule<<M3 as Module<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::Output>>::SelfGrads)

Source§

fn backprop( &self, trace: &<Self as TracedModule<Input>>::Trace, next_grads: <Self as Module<Input>>::Output, ) -> (Input, Self::SelfGrads)

Source§

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Source§

impl<Input, M1: BackpropModule<Input>, M2: BackpropModule<M1::Output>, M3: BackpropModule<M2::Output>> BackpropModule<Input> for (M1, M2, M3)

Source§

type SelfGrads = (<M1 as BackpropModule<Input>>::SelfGrads, <M2 as BackpropModule<<M1 as Module<Input>>::Output>>::SelfGrads, <M3 as BackpropModule<<M2 as Module<<M1 as Module<Input>>::Output>>::Output>>::SelfGrads)

Source§

fn backprop( &self, trace: &<Self as TracedModule<Input>>::Trace, next_grads: <Self as Module<Input>>::Output, ) -> (Input, Self::SelfGrads)

Source§

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Source§

impl<Input, M1: BackpropModule<Input>, M2: BackpropModule<M1::Output>> BackpropModule<Input> for (M1, M2)

Source§

type SelfGrads = (<M1 as BackpropModule<Input>>::SelfGrads, <M2 as BackpropModule<<M1 as Module<Input>>::Output>>::SelfGrads)

Source§

fn backprop( &self, trace: &<Self as TracedModule<Input>>::Trace, next_grads: <Self as Module<Input>>::Output, ) -> (Input, Self::SelfGrads)

Source§

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Source§

impl<Input, M1: BackpropModule<Input>> BackpropModule<Input> for (M1,)

Source§

type SelfGrads = (<M1 as BackpropModule<Input>>::SelfGrads,)

Source§

fn backprop( &self, trace: &<Self as TracedModule<Input>>::Trace, next_grads: <Self as Module<Input>>::Output, ) -> (Input, Self::SelfGrads)

Source§

fn update( &mut self, applyer: &mut impl GradApplyer, updates: Self::SelfGrads, ) -> Result<(), Error>

Implementors§

Source§

impl<E: Dtype + Float + MatMulImpl, const I: usize, const O: usize, A: Module<[E; O], Output = [E; O]> + TracedModule<[E; O]> + BackpropModule<[E; O]> + Default> BackpropModule<[E; I]> for GLU<E, I, O, A>

Source§

impl<E: Dtype, const I: usize, M: Default + Module<[E; I], Output = [E; I]> + TracedModule<[E; I]> + BackpropModule<[E; I]>> BackpropModule<[E; I]> for Residual<E, I, M>

Source§

impl<E: Dtype, const I: usize, M: Default + Module<[E; I]> + TracedModule<[E; I]> + BackpropModule<[E; I]>> BackpropModule<[E; I]> for LR<E, I, M>
where M::SelfGrads: Gradients,

Source§

impl<Input, M: TracedModule<Input, Trace = Input> + RevModule<Input> + BaseModule> BackpropModule<Input> for M

Source§

type SelfGrads = <M as RevModule<Input>>::SelfGrads