tsai_train 0.1.0

Training loop, callbacks, metrics, and checkpointing for tsai-rs
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Training metrics.

use burn::prelude::*;

/// Trait for training metrics.
pub trait Metric<B: Backend>: Send + Sync {
    /// Compute the metric from predictions and targets.
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32;

    /// Get the metric name.
    fn name(&self) -> &str;

    /// Whether higher is better.
    fn higher_is_better(&self) -> bool {
        true
    }
}

/// Classification accuracy metric.
#[derive(Debug, Clone, Default)]
pub struct Accuracy;

impl<B: Backend> Metric<B> for Accuracy {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let pred_classes = preds.clone().argmax(1);
        let target_classes = if targets.dims()[1] > 1 {
            // One-hot encoded
            targets.clone().argmax(1)
        } else {
            // Class indices - simplified for now
            targets.clone().argmax(1)
        };

        let correct = pred_classes.equal(target_classes);
        let correct_sum: f32 = correct.int().sum().into_scalar().elem();
        let total = preds.dims()[0] as f32;

        correct_sum / total
    }

    fn name(&self) -> &str {
        "accuracy"
    }
}

/// Mean Squared Error metric.
#[derive(Debug, Clone, Default)]
pub struct MSE;

impl<B: Backend> Metric<B> for MSE {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let diff = preds.clone() - targets.clone();
        let squared = diff.clone() * diff;
        squared.mean().into_scalar().elem()
    }

    fn name(&self) -> &str {
        "mse"
    }

    fn higher_is_better(&self) -> bool {
        false
    }
}

/// Mean Absolute Error metric.
#[derive(Debug, Clone, Default)]
pub struct MAE;

impl<B: Backend> Metric<B> for MAE {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let diff = preds.clone() - targets.clone();
        diff.abs().mean().into_scalar().elem()
    }

    fn name(&self) -> &str {
        "mae"
    }

    fn higher_is_better(&self) -> bool {
        false
    }
}

/// F1 Score metric (macro average).
///
/// Computes the macro-averaged F1 score across all classes.
/// F1 = 2 * (precision * recall) / (precision + recall)
#[derive(Debug, Clone)]
pub struct F1Score {
    n_classes: usize,
}

impl F1Score {
    /// Create a new F1 score metric.
    pub fn new(n_classes: usize) -> Self {
        Self { n_classes }
    }
}

impl<B: Backend> Metric<B> for F1Score {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let pred_classes = preds.clone().argmax(1);
        let target_classes = targets.clone().argmax(1);

        // Get predictions as data for CPU computation
        let pred_data = pred_classes.into_data();
        let target_data = target_classes.into_data();

        let pred_vec: Vec<i64> = pred_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();
        let target_vec: Vec<i64> = target_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();

        let n_samples = pred_vec.len();
        if n_samples == 0 {
            return 0.0;
        }

        // Compute per-class metrics
        let mut total_f1 = 0.0;
        let mut valid_classes = 0;

        for class in 0..self.n_classes {
            let class_i64 = class as i64;

            // True positives: predicted class and actual class both equal to current class
            let mut tp = 0;
            // False positives: predicted class equals current but actual doesn't
            let mut fp = 0;
            // False negatives: actual class equals current but predicted doesn't
            let mut fn_ = 0;

            for i in 0..n_samples {
                let pred = pred_vec[i];
                let target = target_vec[i];

                if pred == class_i64 && target == class_i64 {
                    tp += 1;
                } else if pred == class_i64 && target != class_i64 {
                    fp += 1;
                } else if pred != class_i64 && target == class_i64 {
                    fn_ += 1;
                }
            }

            // Compute precision and recall for this class
            let precision = if tp + fp > 0 {
                tp as f32 / (tp + fp) as f32
            } else {
                0.0
            };

            let recall = if tp + fn_ > 0 {
                tp as f32 / (tp + fn_) as f32
            } else {
                0.0
            };

            // Compute F1 for this class
            let f1 = if precision + recall > 0.0 {
                2.0 * precision * recall / (precision + recall)
            } else {
                0.0
            };

            // Only count classes that appear in the targets
            if tp + fn_ > 0 {
                total_f1 += f1;
                valid_classes += 1;
            }
        }

        // Return macro-averaged F1
        if valid_classes > 0 {
            total_f1 / valid_classes as f32
        } else {
            0.0
        }
    }

    fn name(&self) -> &str {
        "f1_score"
    }
}

/// Precision metric (macro average).
#[derive(Debug, Clone)]
pub struct Precision {
    n_classes: usize,
}

impl Precision {
    /// Create a new precision metric.
    pub fn new(n_classes: usize) -> Self {
        Self { n_classes }
    }
}

impl<B: Backend> Metric<B> for Precision {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let pred_classes = preds.clone().argmax(1);
        let target_classes = targets.clone().argmax(1);

        let pred_data = pred_classes.into_data();
        let target_data = target_classes.into_data();

        let pred_vec: Vec<i64> = pred_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();
        let target_vec: Vec<i64> = target_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();

        let n_samples = pred_vec.len();
        if n_samples == 0 {
            return 0.0;
        }

        let mut total_precision = 0.0;
        let mut valid_classes = 0;

        for class in 0..self.n_classes {
            let class_i64 = class as i64;
            let mut tp = 0;
            let mut fp = 0;

            for i in 0..n_samples {
                if pred_vec[i] == class_i64 {
                    if target_vec[i] == class_i64 {
                        tp += 1;
                    } else {
                        fp += 1;
                    }
                }
            }

            if tp + fp > 0 {
                total_precision += tp as f32 / (tp + fp) as f32;
                valid_classes += 1;
            }
        }

        if valid_classes > 0 {
            total_precision / valid_classes as f32
        } else {
            0.0
        }
    }

    fn name(&self) -> &str {
        "precision"
    }
}

/// Recall metric (macro average).
#[derive(Debug, Clone)]
pub struct Recall {
    n_classes: usize,
}

impl Recall {
    /// Create a new recall metric.
    pub fn new(n_classes: usize) -> Self {
        Self { n_classes }
    }
}

impl<B: Backend> Metric<B> for Recall {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let pred_classes = preds.clone().argmax(1);
        let target_classes = targets.clone().argmax(1);

        let pred_data = pred_classes.into_data();
        let target_data = target_classes.into_data();

        let pred_vec: Vec<i64> = pred_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();
        let target_vec: Vec<i64> = target_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();

        let n_samples = pred_vec.len();
        if n_samples == 0 {
            return 0.0;
        }

        let mut total_recall = 0.0;
        let mut valid_classes = 0;

        for class in 0..self.n_classes {
            let class_i64 = class as i64;
            let mut tp = 0;
            let mut fn_ = 0;

            for i in 0..n_samples {
                if target_vec[i] == class_i64 {
                    if pred_vec[i] == class_i64 {
                        tp += 1;
                    } else {
                        fn_ += 1;
                    }
                }
            }

            if tp + fn_ > 0 {
                total_recall += tp as f32 / (tp + fn_) as f32;
                valid_classes += 1;
            }
        }

        if valid_classes > 0 {
            total_recall / valid_classes as f32
        } else {
            0.0
        }
    }

    fn name(&self) -> &str {
        "recall"
    }
}

/// Root Mean Squared Error metric.
#[derive(Debug, Clone, Default)]
pub struct RMSE;

impl<B: Backend> Metric<B> for RMSE {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let diff = preds.clone() - targets.clone();
        let squared = diff.clone() * diff;
        let mse: f32 = squared.mean().into_scalar().elem();
        mse.sqrt()
    }

    fn name(&self) -> &str {
        "rmse"
    }

    fn higher_is_better(&self) -> bool {
        false
    }
}

/// Area Under ROC Curve (AUC) metric.
///
/// For binary classification, computes the standard AUC.
/// For multi-class classification, computes macro-averaged one-vs-rest AUC.
///
/// Predictions should be probabilities (after softmax).
#[derive(Debug, Clone)]
pub struct AUC {
    n_classes: usize,
}

impl AUC {
    /// Create a new AUC metric.
    ///
    /// # Arguments
    /// * `n_classes` - Number of classes (2 for binary classification)
    pub fn new(n_classes: usize) -> Self {
        Self { n_classes }
    }

    /// Compute binary AUC using the trapezoidal rule.
    ///
    /// This implements the standard AUC computation by sorting predictions
    /// and computing the area under the ROC curve.
    fn compute_binary_auc(probs: &[f32], labels: &[bool]) -> f32 {
        if probs.is_empty() || labels.is_empty() {
            return 0.5;
        }

        let n = probs.len();
        let n_pos: usize = labels.iter().filter(|&&x| x).count();
        let n_neg = n - n_pos;

        if n_pos == 0 || n_neg == 0 {
            // All same class - AUC is undefined, return 0.5
            return 0.5;
        }

        // Sort by prediction score (ascending) - higher rank = higher score
        // This is required for the Wilcoxon-Mann-Whitney formula
        let mut indices: Vec<usize> = (0..n).collect();
        indices.sort_by(|&a, &b| {
            probs[a]
                .partial_cmp(&probs[b])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Compute AUC using the Wilcoxon-Mann-Whitney statistic
        // AUC = (sum of ranks of positives - n_pos*(n_pos+1)/2) / (n_pos * n_neg)
        // With ascending sort, rank 1 = lowest score, rank n = highest score
        // Perfect AUC: all positives have highest ranks (n_neg+1 to n)
        let mut rank_sum = 0.0;
        let mut current_rank = 0.0;
        let mut i = 0;

        while i < n {
            // Find tied group
            let current_prob = probs[indices[i]];
            let mut j = i;
            while j < n && (probs[indices[j]] - current_prob).abs() < 1e-10 {
                j += 1;
            }

            // Average rank for tied group (1-based ranks)
            // Ranks in this group: (current_rank + 1) to (current_rank + group_size)
            let group_size = (j - i) as f32;
            let avg_rank = current_rank + (group_size + 1.0) / 2.0;

            // Sum ranks for positive samples in this group
            for k in i..j {
                if labels[indices[k]] {
                    rank_sum += avg_rank;
                }
            }

            current_rank += group_size;
            i = j;
        }

        // Compute AUC
        let auc = (rank_sum - (n_pos as f32 * (n_pos as f32 + 1.0) / 2.0))
            / (n_pos as f32 * n_neg as f32);

        auc.clamp(0.0, 1.0)
    }
}

impl Default for AUC {
    fn default() -> Self {
        Self { n_classes: 2 }
    }
}

impl<B: Backend> Metric<B> for AUC {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let [n_samples, n_classes] = preds.dims();

        if n_samples == 0 {
            return 0.5;
        }

        // Get predictions as data for CPU computation
        let pred_data = preds.clone().into_data();
        let target_data = targets.clone().into_data();

        let pred_flat: Vec<f32> = pred_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();
        let target_flat: Vec<f32> = target_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();

        if self.n_classes == 2 {
            // Binary classification: use probability of positive class
            let probs: Vec<f32> = (0..n_samples)
                .map(|i| pred_flat[i * n_classes + 1])
                .collect();
            let labels: Vec<bool> = (0..n_samples)
                .map(|i| target_flat[i * n_classes + 1] > 0.5)
                .collect();

            Self::compute_binary_auc(&probs, &labels)
        } else {
            // Multi-class: compute macro-averaged one-vs-rest AUC
            let mut total_auc = 0.0;
            let mut valid_classes = 0;

            for class in 0..self.n_classes {
                // Probability for this class
                let probs: Vec<f32> = (0..n_samples)
                    .map(|i| pred_flat[i * n_classes + class])
                    .collect();

                // Binary label: is this the true class?
                let labels: Vec<bool> = (0..n_samples)
                    .map(|i| target_flat[i * n_classes + class] > 0.5)
                    .collect();

                // Check if this class has both positive and negative samples
                let n_pos = labels.iter().filter(|&&x| x).count();
                if n_pos > 0 && n_pos < n_samples {
                    let class_auc = Self::compute_binary_auc(&probs, &labels);
                    total_auc += class_auc;
                    valid_classes += 1;
                }
            }

            if valid_classes > 0 {
                total_auc / valid_classes as f32
            } else {
                0.5
            }
        }
    }

    fn name(&self) -> &str {
        "auc"
    }
}

/// Matthews Correlation Coefficient (MCC) metric.
///
/// A balanced metric that takes into account all four cells of the confusion matrix.
/// Values range from -1 (total disagreement) to +1 (perfect prediction).
#[derive(Debug, Clone)]
pub struct MCC {
    n_classes: usize,
}

impl MCC {
    /// Create a new MCC metric.
    pub fn new(n_classes: usize) -> Self {
        Self { n_classes }
    }
}

impl<B: Backend> Metric<B> for MCC {
    fn compute(&self, preds: &Tensor<B, 2>, targets: &Tensor<B, 2>) -> f32 {
        let pred_classes = preds.clone().argmax(1);
        let target_classes = targets.clone().argmax(1);

        let pred_data = pred_classes.into_data();
        let target_data = target_classes.into_data();

        let pred_vec: Vec<i64> = pred_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();
        let target_vec: Vec<i64> = target_data
            .as_slice()
            .map(|s| s.to_vec())
            .unwrap_or_default();

        let n_samples = pred_vec.len();
        if n_samples == 0 {
            return 0.0;
        }

        if self.n_classes == 2 {
            // Binary MCC
            let mut tp = 0;
            let mut tn = 0;
            let mut fp = 0;
            let mut fn_ = 0;

            for i in 0..n_samples {
                let p = pred_vec[i] == 1;
                let t = target_vec[i] == 1;

                if p && t {
                    tp += 1;
                } else if !p && !t {
                    tn += 1;
                } else if p && !t {
                    fp += 1;
                } else {
                    fn_ += 1;
                }
            }

            let numerator = (tp * tn) as f32 - (fp * fn_) as f32;
            let denominator = ((tp + fp) * (tp + fn_) * (tn + fp) * (tn + fn_)) as f32;

            if denominator > 0.0 {
                numerator / denominator.sqrt()
            } else {
                0.0
            }
        } else {
            // Multi-class MCC using the general formula
            // Build confusion matrix
            let mut conf_matrix = vec![vec![0i64; self.n_classes]; self.n_classes];
            for i in 0..n_samples {
                let p = pred_vec[i] as usize;
                let t = target_vec[i] as usize;
                if p < self.n_classes && t < self.n_classes {
                    conf_matrix[t][p] += 1;
                }
            }

            // Compute components
            let n = n_samples as f64;
            let mut c = 0.0; // sum of diagonal (correct predictions)
            let s = n * n; // total squared

            // Row and column sums
            let mut row_sums = vec![0i64; self.n_classes];
            let mut col_sums = vec![0i64; self.n_classes];

            for k in 0..self.n_classes {
                c += conf_matrix[k][k] as f64;
                for l in 0..self.n_classes {
                    row_sums[k] += conf_matrix[k][l];
                    col_sums[k] += conf_matrix[l][k];
                }
            }

            // Compute sums of products
            let mut pk_tk = 0.0;
            let mut pk_sq = 0.0;
            let mut tk_sq = 0.0;

            for k in 0..self.n_classes {
                pk_tk += (col_sums[k] * row_sums[k]) as f64;
                pk_sq += (col_sums[k] * col_sums[k]) as f64;
                tk_sq += (row_sums[k] * row_sums[k]) as f64;
            }

            let numerator = c * n - pk_tk;
            let denominator = ((s - pk_sq) * (s - tk_sq)).sqrt();

            if denominator > 0.0 {
                (numerator / denominator) as f32
            } else {
                0.0
            }
        }
    }

    fn name(&self) -> &str {
        "mcc"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tsai_core::backend::NdArray;

    type TestBackend = NdArray;

    #[test]
    fn test_metric_names() {
        assert_eq!(<Accuracy as Metric<TestBackend>>::name(&Accuracy), "accuracy");
        assert_eq!(<MSE as Metric<TestBackend>>::name(&MSE), "mse");
        assert_eq!(<MAE as Metric<TestBackend>>::name(&MAE), "mae");
    }

    #[test]
    fn test_higher_is_better() {
        assert!(<Accuracy as Metric<TestBackend>>::higher_is_better(&Accuracy));
        assert!(!<MSE as Metric<TestBackend>>::higher_is_better(&MSE));
        assert!(!<MAE as Metric<TestBackend>>::higher_is_better(&MAE));
    }

    #[test]
    fn test_auc_metric_names() {
        let auc = AUC::new(2);
        assert_eq!(<AUC as Metric<TestBackend>>::name(&auc), "auc");
        assert!(<AUC as Metric<TestBackend>>::higher_is_better(&auc));
    }

    #[test]
    fn test_mcc_metric_names() {
        let mcc = MCC::new(2);
        assert_eq!(<MCC as Metric<TestBackend>>::name(&mcc), "mcc");
        assert!(<MCC as Metric<TestBackend>>::higher_is_better(&mcc));
    }

    #[test]
    fn test_auc_binary_perfect() {
        // Perfect predictions should give AUC = 1.0
        let probs = vec![0.9, 0.8, 0.7, 0.2, 0.1, 0.0];
        let labels = vec![true, true, true, false, false, false];
        let auc = AUC::compute_binary_auc(&probs, &labels);
        assert!((auc - 1.0).abs() < 1e-5, "Expected AUC=1.0, got {}", auc);
    }

    #[test]
    fn test_auc_binary_random() {
        // All same probabilities should give AUC = 0.5
        let probs = vec![0.5, 0.5, 0.5, 0.5];
        let labels = vec![true, true, false, false];
        let auc = AUC::compute_binary_auc(&probs, &labels);
        assert!((auc - 0.5).abs() < 1e-5, "Expected AUC=0.5, got {}", auc);
    }

    #[test]
    fn test_auc_all_same_class() {
        // All same class should return 0.5
        let probs = vec![0.9, 0.8, 0.7];
        let labels = vec![true, true, true];
        let auc = AUC::compute_binary_auc(&probs, &labels);
        assert!((auc - 0.5).abs() < 1e-5, "Expected AUC=0.5 for single class, got {}", auc);
    }

    #[test]
    fn test_auc_empty() {
        let probs: Vec<f32> = vec![];
        let labels: Vec<bool> = vec![];
        let auc = AUC::compute_binary_auc(&probs, &labels);
        assert!((auc - 0.5).abs() < 1e-5);
    }
}