scirs2-neural 0.4.3

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Standalone early stopping for neural network training
//!
//! Provides a reusable `EarlyStopping` component that monitors a metric and
//! signals when training should stop because the metric has stopped improving.
//!
//! # Features
//!
//! - Configurable patience (number of epochs with no improvement before stopping)
//! - Configurable minimum delta (smallest change that counts as improvement)
//! - Mode selection: minimize or maximize the monitored metric
//! - Best metric value and epoch tracking
//! - Best model state snapshot (via generic parameter state storage)
//! - Warm-up period: ignore early epochs before enforcing stopping criteria
//!
//! # Example
//!
//! ```rust
//! use scirs2_neural::training::early_stopping::{EarlyStopping, StoppingMode};
//!
//! let mut stopper = EarlyStopping::new(
//!     5,          // patience
//!     0.001,      // min_delta
//!     StoppingMode::Min,
//! );
//!
//! // Simulate training epochs with decreasing then stagnating loss
//! let losses = [1.0, 0.8, 0.6, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5];
//! for (epoch, &val_loss) in losses.iter().enumerate() {
//!     if stopper.check(val_loss) {
//!         println!("Early stopping at epoch {epoch}");
//!         break;
//!     }
//! }
//! println!("Best loss: {} at epoch {}", stopper.best_value(), stopper.best_epoch());
//! ```

use scirs2_core::ndarray::{ArrayD, ScalarOperand};
use scirs2_core::numeric::{Float, FromPrimitive, ToPrimitive};
use std::fmt::Debug;

// ============================================================================
// Types
// ============================================================================

/// Mode for determining "improvement" direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StoppingMode {
    /// Lower values are better (e.g., loss)
    Min,
    /// Higher values are better (e.g., accuracy)
    Max,
}

/// Reason why training was stopped
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopReason {
    /// Patience exhausted (no improvement for `patience` consecutive checks)
    PatienceExhausted {
        /// Number of checks since last improvement
        checks_without_improvement: usize,
    },
    /// The metric diverged (became NaN or infinite)
    MetricDiverged,
    /// The metric exceeded a user-defined absolute threshold
    ThresholdExceeded {
        /// The threshold that was exceeded
        threshold: String,
    },
    /// Training has not been stopped
    NotStopped,
}

/// Status returned by `EarlyStopping::step`
#[derive(Debug, Clone)]
pub struct StepResult {
    /// Whether training should stop now
    pub should_stop: bool,
    /// Whether this step was an improvement
    pub improved: bool,
    /// Current patience counter (checks since last improvement)
    pub patience_counter: usize,
    /// The reason for stopping (if should_stop is true)
    pub reason: StopReason,
}

// ============================================================================
// EarlyStopping
// ============================================================================

/// Standalone early stopping monitor.
///
/// Tracks a metric over training and decides when to stop based on configurable
/// patience, improvement thresholds, and mode (minimize vs maximize).
#[derive(Debug, Clone)]
pub struct EarlyStopping {
    /// Number of checks with no improvement before stopping
    patience: usize,
    /// Minimum change to qualify as an improvement
    min_delta: f64,
    /// Whether we are minimizing or maximizing
    mode: StoppingMode,
    /// Number of initial checks to skip before enforcing stopping
    warmup_checks: usize,
    /// Optional absolute divergence threshold (stop if metric exceeds this)
    divergence_threshold: Option<f64>,

    // --- internal state ---
    /// Best metric value seen so far
    best_value: f64,
    /// Epoch/check index at which the best value was observed
    best_epoch: usize,
    /// Current number of checks since last improvement
    counter: usize,
    /// Total number of checks performed
    total_checks: usize,
    /// Whether stopping has been triggered
    stopped: bool,
    /// The reason for stopping
    stop_reason: StopReason,
    /// History of all metric values received
    metric_history: Vec<f64>,
}

impl EarlyStopping {
    /// Create a new `EarlyStopping` instance.
    ///
    /// # Arguments
    ///
    /// * `patience` - Number of checks with no improvement before triggering stop
    /// * `min_delta` - Minimum absolute change in the metric to count as improvement
    /// * `mode` - Whether to minimize or maximize the metric
    pub fn new(patience: usize, min_delta: f64, mode: StoppingMode) -> Self {
        let initial_best = match mode {
            StoppingMode::Min => f64::INFINITY,
            StoppingMode::Max => f64::NEG_INFINITY,
        };

        Self {
            patience,
            min_delta: min_delta.abs(), // always positive
            mode,
            warmup_checks: 0,
            divergence_threshold: None,
            best_value: initial_best,
            best_epoch: 0,
            counter: 0,
            total_checks: 0,
            stopped: false,
            stop_reason: StopReason::NotStopped,
            metric_history: Vec::new(),
        }
    }

    /// Set a warmup period during which early stopping is not enforced.
    ///
    /// Useful for models that need a few epochs to stabilize before meaningful
    /// metric comparisons can be made.
    pub fn with_warmup(mut self, warmup_checks: usize) -> Self {
        self.warmup_checks = warmup_checks;
        self
    }

    /// Set a divergence threshold.
    ///
    /// In `Min` mode, if the metric exceeds this threshold, training stops immediately.
    /// In `Max` mode, if the metric falls below the negative of this threshold,
    /// training stops immediately.
    pub fn with_divergence_threshold(mut self, threshold: f64) -> Self {
        self.divergence_threshold = Some(threshold);
        self
    }

    /// Check whether training should stop, given the latest metric value.
    ///
    /// This is the simple interface that returns a boolean.
    ///
    /// # Arguments
    ///
    /// * `metric` - The current metric value (e.g., validation loss)
    ///
    /// # Returns
    ///
    /// `true` if training should stop, `false` if training should continue.
    pub fn check(&mut self, metric: f64) -> bool {
        self.step(metric).should_stop
    }

    /// Perform a step with the latest metric value, returning detailed results.
    ///
    /// # Arguments
    ///
    /// * `metric` - The current metric value
    ///
    /// # Returns
    ///
    /// A `StepResult` with detailed information about the decision.
    pub fn step(&mut self, metric: f64) -> StepResult {
        self.total_checks += 1;
        self.metric_history.push(metric);

        // Already stopped -- keep returning stopped
        if self.stopped {
            return StepResult {
                should_stop: true,
                improved: false,
                patience_counter: self.counter,
                reason: self.stop_reason.clone(),
            };
        }

        // Check for NaN / infinity
        if !metric.is_finite() {
            self.stopped = true;
            self.stop_reason = StopReason::MetricDiverged;
            return StepResult {
                should_stop: true,
                improved: false,
                patience_counter: self.counter,
                reason: StopReason::MetricDiverged,
            };
        }

        // Check divergence threshold
        if let Some(threshold) = self.divergence_threshold {
            let diverged = match self.mode {
                StoppingMode::Min => metric > threshold,
                StoppingMode::Max => metric < -threshold,
            };
            if diverged {
                self.stopped = true;
                self.stop_reason = StopReason::ThresholdExceeded {
                    threshold: format!("{threshold}"),
                };
                return StepResult {
                    should_stop: true,
                    improved: false,
                    patience_counter: self.counter,
                    reason: self.stop_reason.clone(),
                };
            }
        }

        // Check if this is an improvement
        let improved = self.is_improvement(metric);

        if improved {
            self.best_value = metric;
            self.best_epoch = self.total_checks - 1; // 0-indexed
            self.counter = 0;
        } else {
            self.counter += 1;
        }

        // During warmup, never trigger stopping
        if self.total_checks <= self.warmup_checks {
            return StepResult {
                should_stop: false,
                improved,
                patience_counter: self.counter,
                reason: StopReason::NotStopped,
            };
        }

        // Check patience
        if self.counter >= self.patience {
            self.stopped = true;
            self.stop_reason = StopReason::PatienceExhausted {
                checks_without_improvement: self.counter,
            };
            return StepResult {
                should_stop: true,
                improved: false,
                patience_counter: self.counter,
                reason: self.stop_reason.clone(),
            };
        }

        StepResult {
            should_stop: false,
            improved,
            patience_counter: self.counter,
            reason: StopReason::NotStopped,
        }
    }

    /// Returns whether the given metric value represents an improvement.
    fn is_improvement(&self, metric: f64) -> bool {
        match self.mode {
            StoppingMode::Min => metric < self.best_value - self.min_delta,
            StoppingMode::Max => metric > self.best_value + self.min_delta,
        }
    }

    /// Get the best metric value observed so far.
    pub fn best_value(&self) -> f64 {
        self.best_value
    }

    /// Get the epoch/check index at which the best value was observed (0-indexed).
    pub fn best_epoch(&self) -> usize {
        self.best_epoch
    }

    /// Get the current patience counter (checks since last improvement).
    pub fn patience_counter(&self) -> usize {
        self.counter
    }

    /// Get the total number of checks performed.
    pub fn total_checks(&self) -> usize {
        self.total_checks
    }

    /// Get the complete history of metric values.
    pub fn metric_history(&self) -> &[f64] {
        &self.metric_history
    }

    /// Check whether stopping has been triggered.
    pub fn is_stopped(&self) -> bool {
        self.stopped
    }

    /// Get the stop reason.
    pub fn stop_reason(&self) -> &StopReason {
        &self.stop_reason
    }

    /// Reset the early stopping state, allowing training to continue.
    pub fn reset(&mut self) {
        let initial_best = match self.mode {
            StoppingMode::Min => f64::INFINITY,
            StoppingMode::Max => f64::NEG_INFINITY,
        };
        self.best_value = initial_best;
        self.best_epoch = 0;
        self.counter = 0;
        self.total_checks = 0;
        self.stopped = false;
        self.stop_reason = StopReason::NotStopped;
        self.metric_history.clear();
    }
}

// ============================================================================
// EarlyStoppingWithState -- stores best model parameters
// ============================================================================

/// Early stopping that also stores a snapshot of the best model state.
///
/// This is useful when you want to restore the model to the best-performing
/// state after training completes.
#[derive(Debug)]
pub struct EarlyStoppingWithState<F>
where
    F: Float + Debug + ScalarOperand + FromPrimitive + ToPrimitive + Clone,
{
    /// The core early stopping logic
    inner: EarlyStopping,
    /// Snapshot of the best parameter state
    best_params: Option<Vec<ArrayD<F>>>,
}

impl<F> EarlyStoppingWithState<F>
where
    F: Float + Debug + ScalarOperand + FromPrimitive + ToPrimitive + Clone,
{
    /// Create a new instance with the given early stopping configuration.
    pub fn new(patience: usize, min_delta: f64, mode: StoppingMode) -> Self {
        Self {
            inner: EarlyStopping::new(patience, min_delta, mode),
            best_params: None,
        }
    }

    /// Set a warmup period.
    pub fn with_warmup(mut self, warmup_checks: usize) -> Self {
        self.inner = self.inner.with_warmup(warmup_checks);
        self
    }

    /// Set a divergence threshold.
    pub fn with_divergence_threshold(mut self, threshold: f64) -> Self {
        self.inner = self.inner.with_divergence_threshold(threshold);
        self
    }

    /// Check the metric and, if improved, snapshot the current parameters.
    ///
    /// # Arguments
    ///
    /// * `metric` - The current metric value
    /// * `params` - The current model parameters to snapshot if improved
    ///
    /// # Returns
    ///
    /// A `StepResult` indicating whether training should stop.
    pub fn step(&mut self, metric: f64, params: &[ArrayD<F>]) -> StepResult {
        let result = self.inner.step(metric);
        if result.improved {
            self.best_params = Some(params.to_vec());
        }
        result
    }

    /// Simple check interface (does NOT snapshot parameters -- use `step` for that).
    pub fn check(&mut self, metric: f64) -> bool {
        self.inner.check(metric)
    }

    /// Get the stored best parameters, if any.
    pub fn best_params(&self) -> Option<&[ArrayD<F>]> {
        self.best_params.as_deref()
    }

    /// Get the best metric value.
    pub fn best_value(&self) -> f64 {
        self.inner.best_value()
    }

    /// Get the epoch of the best metric.
    pub fn best_epoch(&self) -> usize {
        self.inner.best_epoch()
    }

    /// Check whether stopping has been triggered.
    pub fn is_stopped(&self) -> bool {
        self.inner.is_stopped()
    }

    /// Get the stop reason.
    pub fn stop_reason(&self) -> &StopReason {
        self.inner.stop_reason()
    }

    /// Get the complete metric history.
    pub fn metric_history(&self) -> &[f64] {
        self.inner.metric_history()
    }

    /// Reset the state (clears best params too).
    pub fn reset(&mut self) {
        self.inner.reset();
        self.best_params = None;
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array;

    // ------- EarlyStopping (no state) -------

    #[test]
    fn test_min_mode_basic_improvement() {
        let mut es = EarlyStopping::new(3, 0.0, StoppingMode::Min);
        // Monotonically decreasing loss -- should never trigger stop
        for &loss in &[1.0, 0.9, 0.8, 0.7, 0.6, 0.5] {
            assert!(!es.check(loss));
        }
        assert_eq!(es.best_epoch(), 5);
        assert!((es.best_value() - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_min_mode_patience_exhausted() {
        let mut es = EarlyStopping::new(3, 0.0, StoppingMode::Min);
        // Best = 0.5, then no improvement for 3 checks
        assert!(!es.check(0.5)); // improvement (from +inf)
        assert!(!es.check(0.6)); // counter=1
        assert!(!es.check(0.7)); // counter=2
        assert!(es.check(0.8)); // counter=3 => stop
        assert_eq!(es.patience_counter(), 3);
    }

    #[test]
    fn test_max_mode_basic() {
        let mut es = EarlyStopping::new(2, 0.0, StoppingMode::Max);
        assert!(!es.check(0.5)); // improve from -inf
        assert!(!es.check(0.7)); // improve
        assert!(!es.check(0.6)); // no improve, counter=1
        assert!(es.check(0.6)); // no improve, counter=2 => stop

        assert_eq!(es.best_epoch(), 1);
        assert!((es.best_value() - 0.7).abs() < 1e-10);
    }

    #[test]
    fn test_min_delta_effect() {
        let mut es = EarlyStopping::new(3, 0.01, StoppingMode::Min);
        assert!(!es.check(1.0));
        // 0.995 is an improvement by 0.005, less than min_delta=0.01, so NOT counted
        assert!(!es.check(0.995)); // counter=1
        assert!(!es.check(0.996)); // counter=2
        assert!(es.check(0.997)); // counter=3 => stop

        // But best value is still 1.0 because 0.995 didn't meet the delta threshold
        assert!((es.best_value() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_nan_causes_divergence_stop() {
        let mut es = EarlyStopping::new(10, 0.0, StoppingMode::Min);
        assert!(!es.check(0.5));
        let result = es.step(f64::NAN);
        assert!(result.should_stop);
        assert_eq!(es.stop_reason(), &StopReason::MetricDiverged);
    }

    #[test]
    fn test_infinity_causes_divergence_stop() {
        let mut es = EarlyStopping::new(10, 0.0, StoppingMode::Min);
        assert!(!es.check(0.5));
        assert!(es.check(f64::INFINITY));
        assert_eq!(es.stop_reason(), &StopReason::MetricDiverged);
    }

    #[test]
    fn test_divergence_threshold() {
        let mut es = EarlyStopping::new(10, 0.0, StoppingMode::Min).with_divergence_threshold(5.0);

        assert!(!es.check(1.0));
        assert!(!es.check(4.0));
        let result = es.step(6.0); // exceeds threshold
        assert!(result.should_stop);
        match es.stop_reason() {
            StopReason::ThresholdExceeded { .. } => {} // expected
            other => panic!("Expected ThresholdExceeded, got {:?}", other),
        }
    }

    #[test]
    fn test_warmup_period() {
        let mut es = EarlyStopping::new(2, 0.0, StoppingMode::Min).with_warmup(3);

        // During warmup (checks 1..3), no stopping even with no improvement
        assert!(!es.check(0.5)); // check 1 (warmup)
        assert!(!es.check(0.6)); // check 2 (warmup), counter=1
        assert!(!es.check(0.7)); // check 3 (warmup), counter=2 -- but still in warmup

        // After warmup, patience still active from before
        // counter is already at 2, and patience is 2, so this should trigger
        // Actually, counter was 2, now check 4 => 0.8, counter=3 => patience=2 exceeded
        assert!(es.check(0.8)); // check 4 (post-warmup), counter=3 >= patience=2
    }

    #[test]
    fn test_reset() {
        let mut es = EarlyStopping::new(2, 0.0, StoppingMode::Min);
        assert!(!es.check(0.5));
        assert!(!es.check(0.6));
        assert!(es.check(0.7));
        assert!(es.is_stopped());

        es.reset();
        assert!(!es.is_stopped());
        assert_eq!(es.total_checks(), 0);
        assert!(es.metric_history().is_empty());
    }

    #[test]
    fn test_step_result_details() {
        let mut es = EarlyStopping::new(3, 0.0, StoppingMode::Min);
        let r1 = es.step(1.0);
        assert!(r1.improved);
        assert!(!r1.should_stop);
        assert_eq!(r1.patience_counter, 0);

        let r2 = es.step(1.5);
        assert!(!r2.improved);
        assert_eq!(r2.patience_counter, 1);
    }

    #[test]
    fn test_metric_history_tracking() {
        let mut es = EarlyStopping::new(5, 0.0, StoppingMode::Min);
        for &v in &[0.9, 0.8, 0.85, 0.7, 0.75] {
            es.check(v);
        }
        assert_eq!(es.metric_history(), &[0.9, 0.8, 0.85, 0.7, 0.75]);
        assert_eq!(es.total_checks(), 5);
    }

    // ------- EarlyStoppingWithState -------

    #[test]
    fn test_with_state_stores_best_params() {
        let mut es = EarlyStoppingWithState::<f64>::new(3, 0.0, StoppingMode::Min);

        let params1 = vec![Array::from_vec(vec![1.0, 2.0]).into_dyn()];
        let params2 = vec![Array::from_vec(vec![3.0, 4.0]).into_dyn()];
        let params3 = vec![Array::from_vec(vec![5.0, 6.0]).into_dyn()];

        let r1 = es.step(1.0, &params1); // improvement
        assert!(r1.improved);
        assert!(es.best_params().is_some());

        let r2 = es.step(0.5, &params2); // improvement -- updates stored params
        assert!(r2.improved);
        let best = es.best_params().expect("should have best params");
        assert_eq!(best[0].as_slice().expect("contiguous"), &[3.0, 4.0]);

        let r3 = es.step(0.8, &params3); // no improvement -- keeps params2
        assert!(!r3.improved);
        let best = es.best_params().expect("should still have best params");
        assert_eq!(best[0].as_slice().expect("contiguous"), &[3.0, 4.0]);
    }

    #[test]
    fn test_with_state_reset_clears_params() {
        let mut es = EarlyStoppingWithState::<f64>::new(3, 0.0, StoppingMode::Min);
        let params = vec![Array::from_vec(vec![1.0]).into_dyn()];
        es.step(0.5, &params);
        assert!(es.best_params().is_some());

        es.reset();
        assert!(es.best_params().is_none());
        assert!(!es.is_stopped());
    }

    #[test]
    fn test_with_state_stopping_behavior() {
        let mut es = EarlyStoppingWithState::<f64>::new(2, 0.0, StoppingMode::Max);
        let params = vec![Array::from_vec(vec![0.0]).into_dyn()];

        let r1 = es.step(0.8, &params); // improve
        assert!(!r1.should_stop);
        let r2 = es.step(0.7, &params); // no improve, counter=1
        assert!(!r2.should_stop);
        let r3 = es.step(0.6, &params); // no improve, counter=2 => stop
        assert!(r3.should_stop);
        assert!(es.is_stopped());
    }
}