ml_kit 1.0.0

A Machine Learning library for Rust
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//!
//! Stochastic Gradient Descent Trainer for Neural Networks
//!
use std::ops::{AddAssign, SubAssign};

use matrix_kit::dynamic::matrix::Matrix;

use crate::math::loss::LFI;
use crate::{math::activation::AFI, models::neuralnet::NeuralNet};

use super::dataset::{DataItem, DataSet};
use super::learning_rate::GradientUpdateSchedule;

use crate::models::convneuralnet::{ConvLayer, ConvNeuralNet, FullLayer, Layer, PoolLayer};

/// An SGD trainer that trains a neural network
pub struct SGDTrainer<DI: DataItem> {
    /// The dataset on which we train
    pub training_data_set: DataSet<DI>,

    /// The dataset on which we test
    pub testing_data_set: DataSet<DI>,

    /// The loss function used
    pub loss_function: LFI,
}

/// A Gradient representation
#[derive(Clone, Debug)]
pub struct NNGradient {
    pub derivatives: NeuralNet,
}

/// A Gradient representation for CNNs
#[derive(Clone)]
pub struct CNNGradient {
    pub derivatives: Vec<Layer>,
}

impl SubAssign<NNGradient> for NeuralNet {
    fn sub_assign(&mut self, rhs: NNGradient) {
        for layer in 0..self.weights.len() {
            self.weights[layer] -= rhs.derivatives.weights[layer].clone();
            self.biases[layer] -= rhs.derivatives.biases[layer].clone();
        }
    }
}

impl SubAssign<CNNGradient> for ConvNeuralNet {
    fn sub_assign(&mut self, rhs: CNNGradient) {
        for (layer_index, layer) in self.layers.iter_mut().enumerate() {
            match (layer, &rhs.derivatives[layer_index]) {
                (Layer::Conv(conv), Layer::Conv(grad)) => {
                    // Conv Layers
                    for (filter_index, filter) in conv.filters.iter_mut().enumerate() {
                        for (depth_index, depth) in filter.iter_mut().enumerate() {
                            *depth -= grad.filters[filter_index][depth_index].clone();
                        }
                    }
                    conv.biases -= grad.biases.clone();
                }
                (Layer::Full(full), Layer::Full(grad)) => {
                    // FC Layers
                    full.weights -= grad.weights.clone();
                    full.biases -= grad.biases.clone();
                }
                _ => {} // Pool layers
            }
        }
    }
}

// These are so we can compute averages of gradients!

impl AddAssign for NNGradient {
    fn add_assign(&mut self, rhs: Self) {
        for layer in 0..self.derivatives.weights.len() {
            self.derivatives.weights[layer] += rhs.derivatives.weights[layer].clone();
            self.derivatives.biases[layer] += rhs.derivatives.biases[layer].clone();
        }
    }
}

impl AddAssign for CNNGradient {
    fn add_assign(&mut self, rhs: Self) {
        for (layer_index, layer) in self.derivatives.iter_mut().enumerate() {
            match (layer, &rhs.derivatives[layer_index]) {
                (Layer::Conv(conv), Layer::Conv(grad)) => {
                    // Conv Layers
                    for (filter_index, filter) in conv.filters.iter_mut().enumerate() {
                        for (depth_index, depth) in filter.iter_mut().enumerate() {
                            *depth += grad.filters[filter_index][depth_index].clone();
                        }
                    }
                    conv.biases += grad.biases.clone();
                }
                (Layer::Full(full), Layer::Full(grad)) => {
                    // FClayers
                    full.weights += grad.weights.clone();
                    full.biases += grad.biases.clone();
                }
                _ => {} // Pool layers
            }
        }
    }
}

impl PartialEq for NNGradient {
    fn eq(&self, other: &Self) -> bool {
        if self.derivatives.shape() != other.derivatives.shape() {
            return false;
        } else {
            for l in 0..self.derivatives.weights.len() {
                if self.derivatives.weights[l] != other.derivatives.weights[l] {
                    return false;
                }

                if self.derivatives.biases[l] != other.derivatives.biases[l] {
                    return false;
                }
            }

            return true;
        }
    }
}

impl NNGradient {
    pub fn from_nn_shape(neuralnet: NeuralNet) -> NNGradient {
        let mut grad = NNGradient {
            derivatives: neuralnet,
        };

        for layer in 0..grad.derivatives.weights.len() {
            grad.derivatives.weights[layer] = Matrix::from_index_def(
                grad.derivatives.weights[layer].row_count(),
                grad.derivatives.weights[layer].col_count(),
                &mut |_, _| 0.0,
            );
            grad.derivatives.biases[layer] = Matrix::from_index_def(
                grad.derivatives.biases[layer].row_count(),
                1,
                &mut |_, _| 0.0,
            );
        }

        grad
    }

    pub fn norm(&self) -> f64 {
        let mut norm_squared = 0.0;

        for layer in 0..self.derivatives.weights.len() {
            norm_squared += self.derivatives.weights[layer].l2_norm_squared();
        }

        norm_squared.sqrt()
    }

    pub fn set_length(&mut self, length: f64) {
        let norm = self.norm();
        for layer in 0..self.derivatives.weights.len() {
            self.derivatives.weights[layer] /= norm;
            self.derivatives.weights[layer] *= length;
            self.derivatives.biases[layer] /= norm;
            self.derivatives.biases[layer] *= length;
        }
    }

    pub fn as_vec(&self) -> Matrix<f64> {
        let mut grad = Matrix::new(self.derivatives.parameter_count(), 1);

        let mut i = 0;
        for l in 0..self.derivatives.weights.len() {
            for r in 0..self.derivatives.weights[l].row_count() {
                for c in 0..self.derivatives.weights[l].col_count() {
                    grad.set(i, 0, self.derivatives.weights[l].get(r, c));
                    i += 1;
                }
            }

            for b in 0..self.derivatives.biases[l].row_count() {
                grad.set(i, 0, self.derivatives.biases[l].get(b, 0));
                i += 1;
            }
        }

        debug_assert_eq!(i, self.derivatives.parameter_count());

        grad
    }

    pub fn from_vec(grad: Matrix<f64>, shape: Vec<usize>) -> NNGradient {
        let mut derivatives =
            NeuralNet::from_shape(shape.clone(), vec![AFI::Identity; shape.len() - 1]);

        let mut i = 0;
        for l in 0..derivatives.weights.len() {
            for r in 0..derivatives.weights[l].row_count() {
                for c in 0..derivatives.weights[l].col_count() {
                    derivatives.weights[l].set(r, c, grad.get(i, 0));
                    i += 1;
                }
            }

            for b in 0..derivatives.biases[l].row_count() {
                derivatives.biases[l].set(b, 0, grad.get(i, 0));
                i += 1;
            }
        }

        NNGradient { derivatives }
    }
}

impl CNNGradient {
    pub fn from_cnn_shape(cnn: &ConvNeuralNet) -> CNNGradient {
        let mut derivatives = Vec::new();

        for layer in &cnn.layers {
            match layer {
                Layer::Conv(conv) => {
                    // Conv layer -> zero gradients
                    let mut zero_filters = Vec::new();
                    for filter in &conv.filters {
                        let mut zero_filter = Vec::new();
                        for depth in filter {
                            zero_filter.push(Matrix::new(depth.row_count(), depth.col_count()));
                        }
                        zero_filters.push(zero_filter);
                    }
                    derivatives.push(Layer::Conv(ConvLayer::new(
                        zero_filters,
                        Matrix::new(conv.biases.row_count(), 1),
                        conv.act_func.clone(),
                        conv.stride,
                        conv.padding,
                    )));
                }
                Layer::Pool(pool) => {
                    // Pool layers -> no params
                    derivatives.push(Layer::Pool(PoolLayer::new(
                        pool.pool_type.clone(),
                        pool.w_rows,
                        pool.w_cols,
                        pool.stride,
                    )));
                }
                Layer::Full(full) => {
                    // Fc Layer -> zero gradients
                    derivatives.push(Layer::Full(FullLayer::new(
                        Matrix::new(full.weights.row_count(), full.weights.col_count()),
                        Matrix::new(full.biases.row_count(), 1),
                        full.act_func.clone(),
                    )));
                }
            }
        }

        CNNGradient { derivatives }
    }

    pub fn from_cnn(cnn: &ConvNeuralNet) -> CNNGradient {
        let mut derivatives = Vec::new();

        for layer in &cnn.layers {
            match layer {
                Layer::Conv(conv) => {
                    derivatives.push(Layer::Conv(ConvLayer::new(
                        conv.d_filters[0].clone(),
                        conv.d_biases[0].clone(),
                        conv.act_func.clone(),
                        conv.stride,
                        conv.padding,
                    )));
                }
                Layer::Pool(pool) => {
                    // Pool layers -> no params
                    derivatives.push(Layer::Pool(PoolLayer::new(
                        pool.pool_type.clone(),
                        pool.w_rows,
                        pool.w_cols,
                        pool.stride,
                    )));
                }
                Layer::Full(full) => {
                    // Fc Layer -> zero gradients
                    derivatives.push(Layer::Full(FullLayer::new(
                        full.d_weights[0].clone(),
                        full.d_biases[0].clone(),
                        full.act_func.clone(),
                    )));
                }
            }
        }

        CNNGradient { derivatives }
    }

    pub fn norm(&self) -> f64 {
        let mut norm_squared = 0.0;

        for layer in &self.derivatives {
            match layer {
                Layer::Conv(conv) => {
                    // sum^2 norms
                    for filter in &conv.filters {
                        for depth in filter {
                            norm_squared += depth.l2_norm_squared();
                        }
                    }
                    norm_squared += conv.biases.l2_norm_squared();
                }
                Layer::Full(full) => {
                    norm_squared += full.weights.l2_norm_squared();
                    norm_squared += full.biases.l2_norm_squared();
                }
                _ => {}
            }
        }

        norm_squared.sqrt()
    }

    pub fn set_length(&mut self, length: f64) {
        let norm = self.norm();
        if norm == 0.0 {
            return;
        }

        for layer in &mut self.derivatives {
            match layer {
                // Conv Layer
                Layer::Conv(conv) => {
                    for filter in &mut conv.filters {
                        for depth in filter {
                            *depth = depth.clone() * (length / norm);
                        }
                    }
                    conv.biases = conv.biases.clone() * (length / norm);
                }
                // FCLayer
                Layer::Full(full) => {
                    full.weights = full.weights.clone() * (length / norm);
                    full.biases = full.biases.clone() * (length / norm);
                }
                _ => {} //Pool Layer
            }
        }
    }
}

#[cfg(test)]
mod grad_tests {
    use rand::Rng;

    use crate::{math::activation::AFI, models::neuralnet::NeuralNet};

    use super::NNGradient;

    #[test]
    fn test_creation_inversion() {
        let mut rng = rand::rng();

        for _ in 0..10 {
            let layers = rng.random_range(3..100);
            let mut shape = vec![0; layers];
            for l in 0..layers {
                shape[l] = rng.random_range(3..100);
            }

            let derivatives =
                NeuralNet::random_network(shape.clone(), vec![AFI::Identity; layers - 1]);

            let gradient = NNGradient { derivatives };
            let grad_vector = gradient.as_vec();
            let new_gradient = NNGradient::from_vec(grad_vector, shape);

            assert_eq!(gradient, new_gradient);
        }
    }
}

impl<DI: DataItem> SGDTrainer<DI> {
    pub fn new(
        training_data_set: DataSet<DI>,
        testing_data_set: DataSet<DI>,
        loss_function: LFI,
    ) -> SGDTrainer<DI> {
        SGDTrainer {
            training_data_set,
            testing_data_set,
            loss_function,
        }
    }

    // MARK: Training

    /// Computes the gradient for a single training sample. The "gradient" is
    /// represented as its own neural network, but a "derivative" neural network,
    /// if you will. (This just keeps everything organized)
    ///
    /// TODO: Generalize this to think about more than just squared loss
    pub fn compute_gradient(&self, training_item: DI, neuralnet: &NeuralNet) -> NNGradient {
        let mut gradient = NNGradient {
            derivatives: neuralnet.clone(),
        };

        let layers = neuralnet.layer_count() - 1; // The number of non-input layers. (Denotes as L in the writeup)

        let (z, a) = neuralnet.compute_raw_and_full_layers(training_item.input());
        let dot_sigma_z: Vec<Matrix<f64>> = (1..=layers)
            .map(|l| z[l].applying_to_all(&|x| neuralnet.activation_functions[l - 1].derivative(x)))
            .collect();

        let mut gradient_wrt_activations = a.clone(); // This is basically our DP table!

        // Base case of DP table, compute all dC/da for each activation in the final layer
        gradient_wrt_activations[layers] = self
            .loss_function
            .derivative(&a[layers], &training_item.correct_output());
        gradient.derivatives.biases[layers - 1] =
            dot_sigma_z[layers - 1].hadamard(gradient_wrt_activations[layers].clone());
        gradient.derivatives.weights[layers - 1] =
            gradient.derivatives.biases[layers - 1].clone() * a[layers - 1].transpose();

        // the rest now! I want the indices to actually match the indices in the writeup as closely as possible.

        for layer in (0..layers).rev() {
            gradient_wrt_activations[layer] = neuralnet.weights[layer].transpose().clone()
                * dot_sigma_z[layer].hadamard(gradient_wrt_activations[layer + 1].clone());
            gradient.derivatives.biases[layer] =
                dot_sigma_z[layer].hadamard(gradient_wrt_activations[layer + 1].clone());
            gradient.derivatives.weights[layer] =
                gradient.derivatives.biases[layer].clone() * a[layer].transpose().clone();
        }

        gradient
    }

    /// Performs a step of GD on a mini-batch of data, returning the size
    /// of the gradient vector (before rescaling) so we can see how far from a local minimum we are.
    pub fn sgd_batch_step<GUS: GradientUpdateSchedule<NNGradient>>(
        &self,
        batch: Vec<DI>,
        neuralnet: &mut NeuralNet,
        gus: &mut GUS,
    ) -> f64 {
        let mut gradient = NNGradient::from_nn_shape(neuralnet.clone());

        for item in batch {
            gradient += self.compute_gradient(item, neuralnet);
        }

        let original_length = gradient.norm();

        // Now normalize that gradient!
        gus.next_gradient(&mut gradient);

        *neuralnet -= gradient;

        original_length
    }

    /// Runs Gradient Descent on this Data Set, outputting
    /// a neural network
    ///
    /// * `neuralnet` - The neural network to train
    /// * `lrs` - A learning rate schedule
    /// * `epochs` - the number of epochs to run
    /// * `batch_size` - the number of training items in each batch
    pub fn train_sgd<GUS: GradientUpdateSchedule<NNGradient>>(
        &self,
        neuralnet: &mut NeuralNet,
        gus: &mut GUS,
        epochs: usize,
        batch_size: usize,
        verbose: bool,
    ) {
        // Repeat for all epochs!

        for epoch in 0..epochs {
            if verbose {
                println!("Training on epoch {}...", epoch);
            }

            for batch in self.training_data_set.all_minibatches(batch_size) {
                self.sgd_batch_step(batch, neuralnet, gus);
            }
        }

        if verbose {
            println!("Completed all epochs of training.");
        }
    }

    pub fn compute_cnn_gradient(&self, training_item: DI, cnn: &ConvNeuralNet) -> CNNGradient {
        cnn.clone().compute_gradient(
            &training_item.input(),
            training_item.correct_output(),
            &self.loss_function,
        )
    }

    /// Performs a step of SGD on a mini-batch of data for a CNN
    pub fn sgd_cnn_batch_step<GUS: GradientUpdateSchedule<CNNGradient>>(
        &self,
        batch: Vec<DI>,
        cnn: &mut ConvNeuralNet,
        gus: &mut GUS,
    ) -> f64 {
        // First, compute sum of gradients for all training items in the batch
        let mut gradient = CNNGradient::from_cnn_shape(cnn);

        for item in batch {
            gradient += self.compute_cnn_gradient(item, cnn);
        }

        let original_length = gradient.norm();

        //gus.next_gradient(&mut gradient);

        *cnn -= gradient;

        original_length
    }

    /// Trains a CNN using SGD
    pub fn train_cnn_sgd<GUS: GradientUpdateSchedule<CNNGradient>>(
        &self,
        cnn: &mut ConvNeuralNet,
        gus: &mut GUS,
        epochs: usize,
        batch_size: usize,
        verbose: bool,
    ) {
        for epoch in 0..epochs {
            if verbose {
                println!("Training CNN on epoch {}...", epoch);
            }

            for batch in self.training_data_set.all_minibatches(batch_size) {
                self.sgd_cnn_batch_step(batch, cnn, gus);
            }
        }

        if verbose {
            println!("Completed all epochs of CNN training.");
        }
    }

    // MARK: Testing

    /// The average cost over all TESTING examples
    pub fn cost(&self, network: &NeuralNet) -> f64 {
        let mut average_cost = 0.0;

        let ds = &self.testing_data_set;

        for item in ds.data_items.clone() {
            let (x, y) = (item.input(), item.correct_output());
            let a = network.compute_final_layer(x);
            average_cost += self.loss_function.loss(&a, &y);
        }

        average_cost / (ds.data_items.len() as f64)
    }

    /// The accuracy, as a percentage of testing items classified correctly
    pub fn accuracy(&self, network: &NeuralNet) -> f64 {
        let mut num_correct = 0;

        for item in self.testing_data_set.data_items.clone() {
            let (guess, _) = network.classify(item.input());

            if guess == item.label() {
                num_correct += 1;
            }
        }

        (num_correct as f64) / (self.testing_data_set.data_items.len() as f64)
    }

    /// Samples a few data items and prints to the screen the behavior
    /// of the network
    pub fn display_behavior(&self, network: &NeuralNet, num_items: usize) {
        println!(
            "Displaying network performance on {} testing items",
            num_items
        );

        for item in self.testing_data_set.random_sample(num_items) {
            println!("---Training Label: {} ---", item.name());
            println!("{:?}", item);
            println!("Network output: {:?}", network.classify(item.input()));
        }

        println!("--------------------");
        println!("Final cost: {}", self.cost(network));
        println!("Classification accuracy: {}", self.accuracy(network));
    }
}

#[cfg(test)]
mod sgd_tests {

    #[test]
    fn test_stuff() {}
}