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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::fmt::Debug;

use crate::{
    layer::{Layer, ParameterableLayer},
    linalg::{Matrix, MatrixTrait, Scalar},
    loss::Loss, monitor::TM,
};

use self::params::NetworkParams;

pub mod params;

#[derive(Debug)]
pub struct Network {
    // May be one or more layers inside
    // A layer is a layer as long as it implements the Layer trait
    layers: Vec<Box<dyn NetworkLayer>>,
}

impl Network {
    pub fn new(layers: Vec<Box<dyn NetworkLayer>>) -> Self {
        Self { layers }
    }

    pub fn get_params(&self) -> NetworkParams {
        let mut params = Vec::new();
        for layer in self.layers.iter() {
            layer.as_learnable_layer().map(|l| {
                params.push(l.get_learnable_parameters());
            });
        }
        NetworkParams(params)
    }

    pub fn load_params(&mut self, params: &NetworkParams) {
        for (layer, params) in self.layers.iter_mut().zip(params.0.iter()) {
            layer.as_learnable_layer_mut().map(|l| {
                l.set_learnable_parameters(params);
            });
        }
    }

    /// `input` has shape `(i,)` where `i` is the number of inputs.
    pub fn predict(&mut self, input: &Vec<Scalar>) -> Vec<Scalar> {
        self.layers.iter_mut().for_each(|l| {
            l.as_dropout_layer().map(|l| {
                l.disable_dropout();
            });
        });

        self.layers
            .forward(Matrix::from_column_vector(input))
            .get_column(0)
    }

    /// `input` has shape `(i,)` where `i` is the number of inputs.
    ///
    /// `y` has shape `(j,)` where `j` is the number of outputs.
    pub fn predict_evaluate(
        &mut self,
        input: Vec<Scalar>,
        y: Vec<Scalar>,
        loss: &Loss,
    ) -> (Vec<Scalar>, Scalar) {
        let preds: Vec<_> = self.predict(&input);
        let loss = loss.loss_vec(&vec![y], &vec![preds.clone()]);

        (preds, loss)
    }

    /// `inputs` has shape `(n, i)` where `n` is the number of samples and `i` is the number of inputs.
    ///
    /// Returns `preds` which has shape `(n, j)` where `n` is the number of samples and `j` is the number of outputs.
    pub fn predict_many(&mut self, inputs: &Vec<Vec<Scalar>>, batch_size: usize) -> Vec<Vec<Scalar>> {
        TM::start("predmany");
        TM::start("init");
        self.layers.iter_mut().for_each(|l| {
            l.as_dropout_layer().map(|l| l.disable_dropout());
        });

        let mut preds = vec![];
        let mut i = 0;
        let x_batches: Vec<_> = inputs.chunks(batch_size).map(|c| c.to_vec()).collect();
        let n_batches = x_batches.len();
        TM::end();

        TM::start("batches");
        for input_batch in x_batches.into_iter()
        {
            TM::start(format!("{}/{}", i, n_batches));
            let input_batch_matrix = Matrix::from_column_leading_matrix(&input_batch);
            let pred = self.layers.forward(input_batch_matrix);
            preds.extend(pred.get_data_col_leading());
            i += 1;
            TM::end();
        }
        TM::end();
        TM::end();

        preds
    }

    /// `inputs` has shape `(n, i)` where `n` is the number of samples and `i` is the number of inputs.
    ///
    /// `ys` has shape `(n, j)` where `n` is the number of samples and `j` is the number of outputs.
    ///
    /// Returns a tuple of:
    /// - `preds` which has shape `(n, j)` where `n` is the number of samples and `j` is the number of outputs.
    /// - `avg_loss` which is the average loss over all samples.
    /// - `std_loss` which is the standard deviation of the loss over all samples.
    pub fn predict_evaluate_many(
        &mut self,
        inputs: &Vec<Vec<Scalar>>,
        ys: &Vec<Vec<Scalar>>,
        loss: &Loss,
        batch_size: usize
    ) -> (Vec<Vec<Scalar>>, Scalar, Scalar) {
        TM::start("predevmany");
        TM::start("init");
        self.layers.iter_mut().for_each(|l| {
            l.as_dropout_layer().map(|l| l.disable_dropout());
        });

        let mut losses = vec![];
        let mut preds = vec![];
        let mut i = 0;
        let x_batches: Vec<_> = inputs.chunks(batch_size).map(|c| c.to_vec()).collect();
        let y_batches: Vec<_> = ys.chunks(batch_size).map(|c| c.to_vec()).collect();
        let n_batches = x_batches.len();
        TM::end();
        
        TM::start("batches");
        for (input_batch, y_true_batch) in
            x_batches.into_iter().zip(y_batches.into_iter())
        {
            TM::start(format!("{}/{}", i, n_batches));
            let input_batch_matrix = Matrix::from_column_leading_matrix(&input_batch);
            let pred = self.layers.forward(input_batch_matrix);
            let y_true_batch_matrix = Matrix::from_column_leading_matrix(&y_true_batch);
            let e = loss.loss(&y_true_batch_matrix, &pred);

            losses.push(e);
            preds.extend(pred.get_data_col_leading());
            i += 1;
            TM::end();
        }
        TM::end();

        TM::start("stats");
        let avg_loss = losses.iter().sum::<Scalar>() / losses.len() as Scalar;
        let std_loss = losses
            .iter()
            .fold(0., |acc, x| acc + (x - avg_loss).powi(2))
            / losses.len() as Scalar;
        TM::end();
        
        TM::end_with_message(format!("avg_loss: {}, std_loss: {}", avg_loss, std_loss));
        (preds, avg_loss, std_loss)
    }

    /// `x_train` has shape `(i, n)` where `n` is the number of samples and `i` is the number of inputs.
    ///
    /// `y_train` has shape `(j, n)` where `n` is the number of samples and `j` is the number of outputs.
    ///
    /// Returns the average loss over all samples.
    pub fn train(
        &mut self,
        epoch: usize,
        x_train: &Vec<Vec<Scalar>>,
        y_train: &Vec<Vec<Scalar>>,
        loss: &Loss,
        batch_size: usize,
    ) -> Scalar {
        TM::start("train");
        TM::start("init");
        self.layers.iter_mut().for_each(|l| {
            l.as_dropout_layer().map(|l| l.enable_dropout());
        });

        let mut error = 0.;
        let mut i = 0;
        let x_train_batches: Vec<_> = x_train.chunks(batch_size).map(|c| c.to_vec()).collect();
        let y_train_batches: Vec<_> = y_train.chunks(batch_size).map(|c| c.to_vec()).collect();
        let n_batches = x_train_batches.len();
        TM::end();
        
        TM::start("batches");
        for (input_batch, y_true_batch) in
            x_train_batches.into_iter().zip(y_train_batches.into_iter())
        {
            TM::start(format!("{}/{}", i, n_batches));
            let input_batch_matrix = Matrix::from_column_leading_matrix(&input_batch);

            let pred = self.layers.forward(input_batch_matrix);
            
            let y_true_batch_matrix = Matrix::from_column_leading_matrix(&y_true_batch);
            let e = loss.loss(&y_true_batch_matrix, &pred);

            error += e;

            let error_gradient = loss.loss_prime(&y_true_batch_matrix, &pred);
            self.layers.backward(epoch, error_gradient.clone());
            i += 1;
            TM::end_with_message(format!("error: {:.4} total_error: {:.4}", e, error));
        }
        error /= i as Scalar;
        TM::end();
        TM::end_with_message(format!("avg_error: {:.4}", error));
        error
    }
}

impl Layer for Vec<Box<dyn NetworkLayer>> {
    fn forward(&mut self, input: Matrix) -> Matrix {
        TM::start("net.forw");
        let mut output = input;
        let n_layers = self.len();
        for (i, layer) in self.iter_mut().enumerate() {
            TM::start(format!("layer[{}/{}]", i+1, n_layers));
            output = layer.forward(output);
            TM::end();
        }
        TM::end();
        output
    }

    fn backward(&mut self, epoch: usize, error_gradient: Matrix) -> Matrix {
        TM::start("net.back");
        let mut error_gradient = error_gradient;
        for (i, layer) in self.iter_mut().enumerate().rev() {
            TM::start(format!("layer[{}]", i+1));
            error_gradient = layer.backward(epoch, error_gradient);
            TM::end();
        }
        TM::end();
        error_gradient
    }
}

pub trait NetworkLayer: Layer + ParameterableLayer + Debug + Send {}