runn 0.1.1

Runn is a feature-rich, easy-to-use library for building, training, and evaluating feed-forward neural networks in 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
use log::{info, warn};
use serde::{Deserialize, Serialize};
use typetag;

use crate::{error::NetworkError, Metrics};

use super::EarlyStopper;

#[derive(Serialize, Deserialize, Clone)]
struct FlexibleEarlyStopper {
    patience: usize,
    min_delta: f32,
    best: f32,
    wait: usize,
    stopped_epoch: isize,
    stop_training: bool,
    monitor_metric: MonitorMetric,
    target: Option<f32>,
    smoothing_factor: Option<f32>,
    smoothed_value: Option<f32>,
}

impl FlexibleEarlyStopper {
    fn get_current_value(&self, loss: f32, metric_result: &Metrics) -> Option<f32> {
        match self.monitor_metric {
            MonitorMetric::Loss => Some(loss), // Loss is always valid
            MonitorMetric::Accuracy => {
                if let Metrics::Classification(metrics) = metric_result {
                    Some(metrics.accuracy)
                } else {
                    warn!("Early stopping is set to monitor accuracy, but the metric result is not classification.");
                    None // Invalid if MetricResult is not Classification
                }
            }
            MonitorMetric::R2 => {
                if let Metrics::Regression(metrics) = metric_result {
                    Some(metrics.r2)
                } else {
                    warn!("Early stopping is set to monitor R2, but the metric result is not regression.");
                    None // Invalid if MetricResult is not Regression
                }
            }
        }
    }
}

#[typetag::serde]
impl EarlyStopper for FlexibleEarlyStopper {
    fn update(&mut self, epoch: usize, loss: f32, metric_result: &Metrics) {
        let raw_value = match self.get_current_value(loss, metric_result) {
            Some(value) => value,
            None => {
                return; // Exit if the metric result is invalid
            }
        };

        // Apply smoothing if a smoothing factor is set
        let effective_value = if let Some(factor) = self.smoothing_factor {
            let smoothed = match self.smoothed_value {
                Some(prev) => factor * raw_value + (1.0 - factor) * prev,
                None => raw_value,
            };
            self.smoothed_value = Some(smoothed);
            smoothed
        } else {
            raw_value
        };

        // Check if the target value is reached
        if let Some(target) = self.target {
            let target_reached = match self.monitor_metric {
                MonitorMetric::Loss => effective_value <= target, // Minimize loss
                MonitorMetric::Accuracy | MonitorMetric::R2 => effective_value >= target, // Maximize accuracy or R2
            };

            if target_reached {
                self.stop_training = true;
                self.stopped_epoch = epoch as isize;
                info!(
                    "Early stopping triggered: {:?} reached target value of {} at epoch {}.",
                    self.monitor_metric, target, epoch
                );
                return;
            }
        }

        // Check for improvement
        if self
            .monitor_metric
            .is_improvement(effective_value, self.best, self.min_delta)
        {
            self.best = effective_value; // Update the best value
            self.wait = 0; // Reset the wait counter
        } else {
            self.wait += 1; // Increment the wait counter if no improvement
            if self.wait >= self.patience {
                self.stopped_epoch = epoch as isize;
                self.stop_training = true; // Stop training if patience is exceeded
                info!(
                    "Early stopping triggered: no improvement in {:?} for {} epochs.",
                    self.monitor_metric, self.patience
                );
            }
        }
    }

    fn is_training_stopped(&self) -> bool {
        self.stop_training
    }

    fn reset(&mut self) {
        self.best = self.monitor_metric.initial_best();
        self.wait = 0;
        self.stopped_epoch = -1;
        self.stop_training = false;
        self.target = None;
        self.smoothing_factor = None;
    }
}

/// Flexible Early Stopping
/// This early stopping strategy allows you to monitor different metrics (like loss, accuracy, etc.)
/// and set a target value for the monitored metric.
/// It stops training if the monitored metric does not improve for a specified number of epochs (patience).
/// It also allows for a minimum delta to consider an improvement.
/// The target value can be used to stop training if the monitored metric reaches a certain threshold.
/// The smoothing factor can be used to smooth the monitored metric over epochs.
/// This is useful for scenarios where the monitored metric may fluctuate a lot.
pub struct Flexible {
    patience: usize,
    min_delta: f32,
    monitor_metric: MonitorMetric,
    target: Option<f32>,
    smoothing_factor: Option<f32>,
}

impl Flexible {
    /// Creates a new instance of the Flexible early stopping strategy.
    /// Default values are:
    /// - patience: 10
    /// - min_delta: 0.0
    /// - monitor_metric: Loss
    /// - target: None
    /// - smoothing_factor: None
    fn new() -> Self {
        Self {
            patience: 10,
            min_delta: 0.0,
            monitor_metric: MonitorMetric::Loss, // Default to monitoring loss
            target: None,
            smoothing_factor: None,
        }
    }

    /// Sets the patience for early stopping.
    /// The patience is the number of epochs with no improvement after which training will be stopped.
    /// #parameters
    /// - `patience`: The number of epochs with no improvement after which training will be stopped.
    pub fn patience(mut self, patience: usize) -> Self {
        self.patience = patience;
        self
    }

    /// Sets the minimum delta for early stopping.
    /// The minimum delta is the minimum change in the monitored metric to consider it an improvement.
    /// #parameters
    /// - `min_delta`: The minimum change in the monitored metric to consider it an improvement.
    pub fn min_delta(mut self, min_delta: f32) -> Self {
        self.min_delta = min_delta;
        self
    }

    /// Sets the metric to monitor for early stopping.
    /// The metric can be one of the following:
    /// - Loss
    /// - Accuracy if the model is a classifier
    /// - R2 if the model is a regressor
    ///
    /// #parameters
    /// - `monitor_metric`: The metric to monitor for early stopping.
    pub fn monitor_metric(mut self, monitor_metric: MonitorMetric) -> Self {
        self.monitor_metric = monitor_metric;
        self
    }

    /// Sets the target value for early stopping.
    /// The target value is the value of the monitored metric at which training will be stopped.
    /// This is useful for scenarios where the monitored metric may fluctuate a lot.
    /// #parameters
    /// - `target`: The target value for early stopping.
    pub fn target(mut self, target: f32) -> Self {
        self.target = Some(target);
        self
    }

    /// Sets the smoothing factor for early stopping.
    /// The smoothing factor is used to smooth the monitored metric over epochs.
    /// It uses exponential moving average (EMA) smoothing on the loss before comparing to best
    /// This is useful for scenarios where the monitored metric may fluctuate a lot.
    /// The smoothing factor should be in the range [0, 1].
    /// A value of 0 means no smoothing, and a value of 1 means maximum smoothing.
    /// #parameters
    /// - `smoothing_factor`: The smoothing factor for early stopping.
    pub fn smoothing_factor(mut self, smoothing_factor: f32) -> Self {
        self.smoothing_factor = Some(smoothing_factor);
        self
    }

    fn validate(&self) -> Result<(), NetworkError> {
        if self.patience == 0 {
            return Err(NetworkError::ConfigError(format!(
                "Patience for flexible early stopper must be greater than 0, but was {}",
                self.patience
            )));
        }
        if self.min_delta < 0.0 {
            return Err(NetworkError::ConfigError(format!(
                "Min delta for flexible early stopper must be non-negative, but was {}",
                self.min_delta
            )));
        }
        if let Some(target) = self.target {
            if target < 0.0 {
                return Err(NetworkError::ConfigError(format!(
                    "Target for flexible early stopper must be non-negative, but was {}",
                    target
                )));
            }
        }
        if let Some(smoothing_factor) = self.smoothing_factor {
            if !(0.0..=1.0).contains(&smoothing_factor) {
                return Err(NetworkError::ConfigError(format!(
                    "Smoothing factor for flexible early stopper must be in the range [0, 1], but was {}",
                    smoothing_factor
                )));
            }
        }

        Ok(())
    }

    pub fn build(self) -> Result<Box<dyn EarlyStopper>, NetworkError> {
        self.validate()?;
        Ok(Box::new(FlexibleEarlyStopper {
            patience: self.patience,
            min_delta: self.min_delta,
            best: self.monitor_metric.initial_best(),
            wait: 0,
            stopped_epoch: -1,
            stop_training: false,
            monitor_metric: self.monitor_metric,
            target: self.target,
            smoothing_factor: self.smoothing_factor,
            smoothed_value: None,
        }))
    }
}

impl Default for Flexible {
    /// Creates a new builder for Flexible with default values
    /// Default values are:
    /// - patience: 10
    /// - min_delta: 0.0
    /// - monitor_metric: Loss
    /// - target: None
    /// - smoothing_factor: None
    fn default() -> Self {
        Self::new()
    }
}

/// Enum representing the different metrics that can be monitored for early stopping.
/// The metrics can be one of the following:
/// - Loss
/// - Accuracy
/// - R2
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum MonitorMetric {
    Loss,
    Accuracy,
    R2,
}

impl MonitorMetric {
    /// Determines if the current value is an improvement over the best value
    pub(crate) fn is_improvement(&self, current: f32, best: f32, min_delta: f32) -> bool {
        match self {
            MonitorMetric::Loss => (best - current) > min_delta, // Minimize loss
            MonitorMetric::Accuracy => (current - best) > min_delta, // Maximize accuracy
            MonitorMetric::R2 => (current - best) > min_delta,   // Maximize R2
        }
    }

    /// Provides the initial best value for the metric
    pub(crate) fn initial_best(&self) -> f32 {
        match self {
            MonitorMetric::Loss => f32::MAX, // Start with a very high loss
            MonitorMetric::Accuracy => 0.0,  // Start with a very low accuracy
            MonitorMetric::R2 => f32::MIN,   // Start with a very low R2
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        classification::ClassificationMetrics,
        flexible::{Flexible, MonitorMetric},
        metric,
        regression::RegressionMetrics,
        Metrics,
    };

    #[test]
    fn test_flexible_builder_default_values() {
        let stopper = Flexible::new().build();
        assert!(stopper.is_ok());
    }

    #[test]
    fn test_flexible_builder_custom_values() {
        let stopper = Flexible::new()
            .patience(5)
            .min_delta(0.01)
            .monitor_metric(MonitorMetric::Accuracy)
            .target(0.95)
            .smoothing_factor(0.5)
            .build();
        assert!(stopper.is_ok());
    }

    #[test]
    fn test_flexible_builder_validation_errors() {
        // Invalid patience
        let result = Flexible::new().patience(0).build();
        assert!(result.is_err());
        if let Err(e) = result {
            assert_eq!(
                e.to_string(),
                "Configuration error: Patience for flexible early stopper must be greater than 0, but was 0"
            );
        }

        // Invalid min_delta
        let result = Flexible::new().min_delta(-0.01).build();
        assert!(result.is_err());
        if let Err(e) = result {
            assert_eq!(
                e.to_string(),
                "Configuration error: Min delta for flexible early stopper must be non-negative, but was -0.01"
            );
        }

        // Invalid target
        let result = Flexible::new().target(-1.0).build();
        assert!(result.is_err());
        if let Err(e) = result {
            assert_eq!(
                e.to_string(),
                "Configuration error: Target for flexible early stopper must be non-negative, but was -1"
            );
        }

        // Invalid smoothing factor
        let result = Flexible::new().smoothing_factor(1.5).build();
        assert!(result.is_err());
        if let Err(e) = result {
            assert_eq!(
                e.to_string(),
                "Configuration error: Smoothing factor for flexible early stopper must be in the range [0, 1], but was 1.5"
            );
        }
    }

    #[test]
    fn test_flexible_early_stopper_loss() {
        let mut stopper = Flexible::new()
            .patience(3)
            .min_delta(0.01)
            .monitor_metric(MonitorMetric::Loss)
            .build()
            .unwrap();

        let losses = [0.5, 0.4, 0.39, 0.39, 0.39, 0.38];
        for (epoch, &loss) in losses.iter().enumerate() {
            stopper.update(epoch, loss, &Metrics::Regression(RegressionMetrics { rmse: loss, r2: 0.0 }));
            if stopper.is_training_stopped() {
                assert_eq!(epoch, 5); // Training should stop after 4 epochs with no improvement
                break;
            }
        }
    }

    #[test]
    fn test_flexible_early_stopper_accuracy() {
        let mut stopper = Flexible::new()
            .patience(2)
            .min_delta(0.01)
            .monitor_metric(MonitorMetric::Accuracy)
            .target(0.95)
            .build()
            .unwrap();

        let accuracies = [0.7, 0.75, 0.76, 0.77, 0.95, 0.96];
        for (epoch, &accuracy) in accuracies.iter().enumerate() {
            stopper.update(
                epoch,
                0.0,
                &Metrics::Classification(ClassificationMetrics {
                    accuracy,
                    micro_precision: 0.0,
                    micro_recall: 0.0,
                    macro_f1_score: 0.0,
                    micro_f1_score: 0.0,
                    metrics_by_class: vec![],
                }),
            );
            if stopper.is_training_stopped() {
                assert_eq!(epoch, 4); // Training should stop when accuracy reaches 0.95
                break;
            }
        }
    }

    #[test]
    fn test_flexible_early_stopper_with_smoothing() {
        let mut stopper = Flexible::new()
            .patience(3)
            .min_delta(0.01)
            .monitor_metric(MonitorMetric::Loss)
            .smoothing_factor(0.5)
            .build()
            .unwrap();

        let losses = [0.5, 0.4, 0.39, 0.39, 0.39, 0.38];
        for (epoch, &loss) in losses.iter().enumerate() {
            stopper.update(epoch, loss, &Metrics::Regression(RegressionMetrics { rmse: loss, r2: 0.0 }));
            if stopper.is_training_stopped() {
                assert_eq!(epoch, 4); // Training should stop after 3 epochs with no improvement
                break;
            }
        }
    }

    #[test]
    fn test_flexible_early_stopper_invalid_metric_result() {
        let mut stopper = Flexible::new()
            .patience(3)
            .min_delta(0.01)
            .monitor_metric(MonitorMetric::Accuracy)
            .build()
            .unwrap();

        stopper.update(0, 0.0, &Metrics::Regression(RegressionMetrics { rmse: 0.5, r2: 0.0 }));
        assert!(!stopper.is_training_stopped()); // Should not stop as MetricResult is invalid for Accuracy
    }

    #[test]
    fn test_early_stopping_by_loss() {
        let mut early_stopper = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .patience(3)
            .min_delta(0.01)
            .build()
            .unwrap();

        let losses = [0.5, 0.4, 0.35, 0.36, 0.37, 0.38];
        for (epoch, &val_loss) in losses.iter().enumerate() {
            early_stopper.update(epoch, val_loss, &get_dummy_metric_result());
            if early_stopper.is_training_stopped() {
                assert_eq!(epoch, 5);
                break;
            }
        }
    }

    fn get_dummy_metric_result() -> Metrics {
        let dummy_metric = metric::classification::ClassificationMetrics {
            accuracy: 0.0,
            micro_precision: 0.0,
            micro_recall: 0.0,
            macro_f1_score: 0.0,
            micro_f1_score: 0.0,
            metrics_by_class: vec![],
        };

        Metrics::Classification(dummy_metric)
    }

    #[test]
    fn test_no_early_stopping() {
        let mut early_stopper = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .patience(3)
            .min_delta(0.01)
            .build()
            .unwrap();

        let val_losses = [0.5, 0.4, 0.35, 0.34, 0.33, 0.32];
        for (epoch, &val_loss) in val_losses.iter().enumerate() {
            early_stopper.update(epoch, val_loss, &get_dummy_metric_result());
        }
        assert!(!early_stopper.is_training_stopped());
    }

    #[test]
    fn test_reset() {
        let mut early_stopper = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .patience(3)
            .min_delta(0.01)
            .build()
            .unwrap();

        let val_losses = [0.5, 0.4, 0.35, 0.36, 0.37, 0.38];

        for (epoch, &val_loss) in val_losses.iter().enumerate() {
            early_stopper.update(epoch, val_loss, &get_dummy_metric_result());

            if early_stopper.is_training_stopped() {
                assert_eq!(epoch, 5);
                break;
            }
        }
        early_stopper.reset();
        assert!(!early_stopper.is_training_stopped());
    }

    #[test]
    fn test_target_loss_triggers_stop() {
        let mut early_stopper = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .target(0.33)
            .patience(10)
            .build()
            .unwrap();

        let val_losses = [0.5, 0.4, 0.35, 0.34, 0.33, 0.32];
        for (epoch, &val_loss) in val_losses.iter().enumerate() {
            early_stopper.update(epoch, val_loss, &get_dummy_metric_result());
            if early_stopper.is_training_stopped() {
                assert_eq!(epoch, 4); // Should stop at 0.33
                return;
            }
        }
        panic!("Expected early stopping by target loss");
    }

    #[test]
    fn test_smoothing_affects_stopping() {
        let mut early_stopper = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .patience(2)
            .min_delta(0.01)
            .smoothing_factor(0.5)
            .build()
            .unwrap();

        let losses = [0.5, 0.4, 0.45, 0.46, 0.47]; // Raw loss is going up but slowly
        let mut stopped = false;
        for (epoch, &val_loss) in losses.iter().enumerate() {
            early_stopper.update(epoch, val_loss, &get_dummy_metric_result());
            if early_stopper.is_training_stopped() {
                assert_eq!(epoch, 3);
                stopped = true;
                break;
            }
        }
        assert!(stopped, "Expected early stopping with smoothing applied");
    }

    #[test]
    fn test_no_smoothing_vs_smoothing() {
        // A slowly rising loss curve
        let val_losses = [0.30, 0.31, 0.32, 0.33, 0.34, 0.35];

        // Raw stopper: high patience so it won't stop in 6 epochs
        let mut no_smoothing = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .patience(10)
            .min_delta(0.01)
            .build()
            .unwrap();

        // EMA stopper: low patience, smoothing_factor => will detect rising trend
        let mut with_smoothing = Flexible::new()
            .monitor_metric(MonitorMetric::Loss)
            .patience(2)
            .min_delta(0.01)
            .smoothing_factor(0.6)
            .build()
            .unwrap();

        for (epoch, &loss) in val_losses.iter().enumerate() {
            no_smoothing.update(epoch, loss, &get_dummy_metric_result());
            with_smoothing.update(epoch, loss, &get_dummy_metric_result());
        }

        // EMA-based stopper should have halted
        assert!(with_smoothing.is_training_stopped(), "With smoothing should stop on the rising trend");
        // Raw stopper still within patience window
        assert!(!no_smoothing.is_training_stopped(), "Without smoothing (high patience) should continue");
    }
}