1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
   Appellation: train <module>
   Contrib: FL03 <jo3mccain@icloud.com>
*/

pub trait ApplyGradient {
    type Gradient;

    fn apply_gradient(&mut self, gradient: &Self::Gradient);
}

/// [Backward] describes an object capable of backward propagation.
pub trait Backward {
    type Output;

    fn backward(&self) -> Self::Output;
}

pub trait Compile {
    type Dataset;

    fn compile(&mut self, dataset: &Self::Dataset);
}

pub trait Train: Compile {
    type Output;

    fn train(&mut self) -> Self::Output;
}

impl<S> Backward for Option<S>
where
    S: Backward,
{
    type Output = Option<S::Output>;

    fn backward(&self) -> Self::Output {
        match self {
            Some(s) => Some(s.backward()),
            None => None,
        }
    }
}