tensorlogic-train 0.1.0

Training loops, loss composition, and optimization schedules for TensorLogic
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
//! Curriculum learning strategies for progressive training.
//!
//! This module provides various curriculum learning strategies that gradually increase
//! training difficulty:
//! - Sample-level curriculum (difficulty scoring)
//! - Competence-based pacing (adaptive difficulty)
//! - Self-paced learning
//! - Task-level curriculum (multi-task progressive training)

use crate::{TrainError, TrainResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1};
use std::collections::HashMap;

/// Trait for curriculum learning strategies.
pub trait CurriculumStrategy {
    /// Get the subset of samples to use for the current training step.
    ///
    /// # Arguments
    /// * `epoch` - Current training epoch
    /// * `total_epochs` - Total number of training epochs
    /// * `difficulties` - Difficulty scores for each sample `[N]`
    ///
    /// # Returns
    /// Indices of samples to include in training at this stage
    fn select_samples(
        &self,
        epoch: usize,
        total_epochs: usize,
        difficulties: &ArrayView1<f64>,
    ) -> TrainResult<Vec<usize>>;

    /// Compute difficulty scores for training samples.
    ///
    /// # Arguments
    /// * `data` - Training data `[N, features]`
    /// * `labels` - Training labels `[N, classes]`
    /// * `predictions` - Model predictions `[N, classes]` (optional, for adaptive strategies)
    ///
    /// # Returns
    /// Difficulty score for each sample `[N]` (higher = more difficult)
    fn compute_difficulty(
        &self,
        data: &Array2<f64>,
        labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<Array1<f64>>;
}

/// Linear curriculum: gradually increase the percentage of samples used.
///
/// Starts with a small percentage of easiest samples and linearly increases
/// to use all samples by the end of training.
#[derive(Debug, Clone)]
pub struct LinearCurriculum {
    /// Initial percentage of samples to use (0.0 to 1.0).
    pub start_percentage: f64,
    /// Whether to sort by difficulty (true) or use all samples (false).
    pub sort_by_difficulty: bool,
}

impl LinearCurriculum {
    /// Create a new linear curriculum.
    ///
    /// # Arguments
    /// * `start_percentage` - Initial percentage of samples (e.g., 0.1 for 10%)
    pub fn new(start_percentage: f64) -> TrainResult<Self> {
        if !(0.0..=1.0).contains(&start_percentage) {
            return Err(TrainError::InvalidParameter(
                "start_percentage must be in [0, 1]".to_string(),
            ));
        }
        Ok(Self {
            start_percentage,
            sort_by_difficulty: true,
        })
    }

    /// Disable sorting by difficulty (use random subset instead).
    pub fn without_sorting(mut self) -> Self {
        self.sort_by_difficulty = false;
        self
    }
}

impl Default for LinearCurriculum {
    fn default() -> Self {
        Self {
            start_percentage: 0.2,
            sort_by_difficulty: true,
        }
    }
}

impl CurriculumStrategy for LinearCurriculum {
    fn select_samples(
        &self,
        epoch: usize,
        total_epochs: usize,
        difficulties: &ArrayView1<f64>,
    ) -> TrainResult<Vec<usize>> {
        let n = difficulties.len();
        if n == 0 {
            return Ok(Vec::new());
        }

        // Compute current percentage (linear interpolation)
        let progress = if total_epochs > 1 {
            epoch as f64 / (total_epochs - 1) as f64
        } else {
            1.0
        };
        let current_percentage = self.start_percentage + (1.0 - self.start_percentage) * progress;
        let num_samples = ((n as f64 * current_percentage).ceil() as usize).min(n);

        if !self.sort_by_difficulty {
            // Return first num_samples indices
            return Ok((0..num_samples).collect());
        }

        // Sort by difficulty (ascending) and select easiest samples
        let mut indices: Vec<usize> = (0..n).collect();
        indices.sort_by(|&a, &b| {
            difficulties[a]
                .partial_cmp(&difficulties[b])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(indices.into_iter().take(num_samples).collect())
    }

    fn compute_difficulty(
        &self,
        _data: &Array2<f64>,
        _labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<Array1<f64>> {
        // Default: use prediction entropy as difficulty
        // If no predictions provided, use zeros (all equal difficulty)
        if let Some(preds) = predictions {
            let n = preds.nrows();
            let mut difficulties = Array1::zeros(n);

            for i in 0..n {
                let pred = preds.row(i);
                // Compute entropy: -Σ p_i log(p_i)
                let mut entropy = 0.0;
                for &p in pred.iter() {
                    if p > 1e-10 {
                        entropy -= p * p.ln();
                    }
                }
                difficulties[i] = entropy;
            }

            Ok(difficulties)
        } else {
            // No predictions provided, assume all equal difficulty
            Ok(Array1::zeros(_labels.nrows()))
        }
    }
}

/// Exponential curriculum: exponentially increase sample percentage.
///
/// Uses an exponential schedule to quickly ramp up the number of training samples.
#[derive(Debug, Clone)]
pub struct ExponentialCurriculum {
    /// Initial percentage of samples.
    pub start_percentage: f64,
    /// Exponential growth rate (higher = faster growth).
    pub growth_rate: f64,
}

impl ExponentialCurriculum {
    /// Create a new exponential curriculum.
    ///
    /// # Arguments
    /// * `start_percentage` - Initial percentage of samples
    /// * `growth_rate` - Growth rate (e.g., 2.0 for doubling)
    pub fn new(start_percentage: f64, growth_rate: f64) -> TrainResult<Self> {
        if !(0.0..=1.0).contains(&start_percentage) {
            return Err(TrainError::InvalidParameter(
                "start_percentage must be in [0, 1]".to_string(),
            ));
        }
        if growth_rate <= 0.0 {
            return Err(TrainError::InvalidParameter(
                "growth_rate must be positive".to_string(),
            ));
        }
        Ok(Self {
            start_percentage,
            growth_rate,
        })
    }
}

impl Default for ExponentialCurriculum {
    fn default() -> Self {
        Self {
            start_percentage: 0.1,
            growth_rate: 2.0,
        }
    }
}

impl CurriculumStrategy for ExponentialCurriculum {
    fn select_samples(
        &self,
        epoch: usize,
        total_epochs: usize,
        difficulties: &ArrayView1<f64>,
    ) -> TrainResult<Vec<usize>> {
        let n = difficulties.len();
        if n == 0 {
            return Ok(Vec::new());
        }

        // Exponential growth: p(t) = start * exp(growth * t)
        let progress = if total_epochs > 1 {
            epoch as f64 / (total_epochs - 1) as f64
        } else {
            1.0
        };
        let current_percentage =
            (self.start_percentage * (self.growth_rate * progress).exp()).min(1.0);
        let num_samples = ((n as f64 * current_percentage).ceil() as usize).min(n);

        // Sort by difficulty and select easiest samples
        let mut indices: Vec<usize> = (0..n).collect();
        indices.sort_by(|&a, &b| {
            difficulties[a]
                .partial_cmp(&difficulties[b])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(indices.into_iter().take(num_samples).collect())
    }

    fn compute_difficulty(
        &self,
        _data: &Array2<f64>,
        _labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<Array1<f64>> {
        // Same as LinearCurriculum
        if let Some(preds) = predictions {
            let n = preds.nrows();
            let mut difficulties = Array1::zeros(n);

            for i in 0..n {
                let pred = preds.row(i);
                let mut entropy = 0.0;
                for &p in pred.iter() {
                    if p > 1e-10 {
                        entropy -= p * p.ln();
                    }
                }
                difficulties[i] = entropy;
            }

            Ok(difficulties)
        } else {
            Ok(Array1::zeros(_labels.nrows()))
        }
    }
}

/// Self-paced learning: model determines its own learning pace.
///
/// Adaptively selects samples based on current model performance,
/// prioritizing samples the model is ready to learn from.
#[derive(Debug, Clone)]
pub struct SelfPacedCurriculum {
    /// Age parameter controlling pace (higher = more aggressive).
    pub lambda: f64,
    /// Threshold for sample selection.
    pub threshold: f64,
}

impl SelfPacedCurriculum {
    /// Create a new self-paced curriculum.
    ///
    /// # Arguments
    /// * `lambda` - Age parameter (controls learning pace)
    /// * `threshold` - Selection threshold
    pub fn new(lambda: f64, threshold: f64) -> TrainResult<Self> {
        if lambda <= 0.0 {
            return Err(TrainError::InvalidParameter(
                "lambda must be positive".to_string(),
            ));
        }
        Ok(Self { lambda, threshold })
    }
}

impl Default for SelfPacedCurriculum {
    fn default() -> Self {
        Self {
            lambda: 1.0,
            threshold: 0.5,
        }
    }
}

impl CurriculumStrategy for SelfPacedCurriculum {
    fn select_samples(
        &self,
        _epoch: usize,
        _total_epochs: usize,
        difficulties: &ArrayView1<f64>,
    ) -> TrainResult<Vec<usize>> {
        // Select samples with difficulty below threshold
        let indices: Vec<usize> = difficulties
            .iter()
            .enumerate()
            .filter(|(_, &d)| d < self.threshold)
            .map(|(i, _)| i)
            .collect();

        Ok(indices)
    }

    fn compute_difficulty(
        &self,
        _data: &Array2<f64>,
        labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<Array1<f64>> {
        if let Some(preds) = predictions {
            let n = preds.nrows();
            let mut difficulties = Array1::zeros(n);

            for i in 0..n {
                // Compute loss for each sample (cross-entropy)
                let pred = preds.row(i);
                let label = labels.row(i);

                let mut loss = 0.0;
                for j in 0..pred.len() {
                    let p = pred[j].clamp(1e-10, 1.0 - 1e-10);
                    loss -= label[j] * p.ln();
                }

                // Weight by self-pacing parameter
                difficulties[i] = loss * self.lambda;
            }

            Ok(difficulties)
        } else {
            Err(TrainError::InvalidParameter(
                "SelfPacedCurriculum requires predictions for difficulty computation".to_string(),
            ))
        }
    }
}

/// Competence-based curriculum: adapts to model's current competence level.
///
/// Gradually increases difficulty based on model's mastery of easier samples.
#[derive(Debug, Clone)]
pub struct CompetenceCurriculum {
    /// Initial competence level (0.0 to 1.0).
    pub initial_competence: f64,
    /// Competence growth rate per epoch.
    pub growth_rate: f64,
    /// Maximum competence level.
    pub max_competence: f64,
}

impl CompetenceCurriculum {
    /// Create a new competence-based curriculum.
    ///
    /// # Arguments
    /// * `initial_competence` - Starting competence level
    /// * `growth_rate` - How fast competence grows per epoch
    pub fn new(initial_competence: f64, growth_rate: f64) -> TrainResult<Self> {
        if !(0.0..=1.0).contains(&initial_competence) {
            return Err(TrainError::InvalidParameter(
                "initial_competence must be in [0, 1]".to_string(),
            ));
        }
        Ok(Self {
            initial_competence,
            growth_rate,
            max_competence: 1.0,
        })
    }
}

impl Default for CompetenceCurriculum {
    fn default() -> Self {
        Self {
            initial_competence: 0.3,
            growth_rate: 0.05,
            max_competence: 1.0,
        }
    }
}

impl CurriculumStrategy for CompetenceCurriculum {
    fn select_samples(
        &self,
        epoch: usize,
        _total_epochs: usize,
        difficulties: &ArrayView1<f64>,
    ) -> TrainResult<Vec<usize>> {
        // Current competence level
        let competence =
            (self.initial_competence + self.growth_rate * epoch as f64).min(self.max_competence);

        // Select samples with difficulty <= competence
        let indices: Vec<usize> = difficulties
            .iter()
            .enumerate()
            .filter(|(_, &d)| d <= competence)
            .map(|(i, _)| i)
            .collect();

        Ok(indices)
    }

    fn compute_difficulty(
        &self,
        _data: &Array2<f64>,
        _labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<Array1<f64>> {
        // Normalize difficulties to [0, 1]
        if let Some(preds) = predictions {
            let n = preds.nrows();
            let mut difficulties = Array1::zeros(n);

            for i in 0..n {
                let pred = preds.row(i);
                let mut entropy = 0.0;
                for &p in pred.iter() {
                    if p > 1e-10 {
                        entropy -= p * p.ln();
                    }
                }
                difficulties[i] = entropy;
            }

            // Normalize to [0, 1]
            let max_difficulty = difficulties.iter().cloned().fold(0.0f64, f64::max);
            if max_difficulty > 0.0 {
                difficulties.mapv_inplace(|d| d / max_difficulty);
            }

            Ok(difficulties)
        } else {
            Ok(Array1::zeros(_labels.nrows()))
        }
    }
}

/// Task-level curriculum for multi-task learning.
///
/// Progressively introduces different tasks during training.
#[derive(Debug, Clone)]
pub struct TaskCurriculum {
    /// Task schedule: (start_epoch, task_id) pairs.
    task_schedule: Vec<(usize, usize)>,
}

impl TaskCurriculum {
    /// Create a new task curriculum.
    ///
    /// # Arguments
    /// * `schedule` - Task introduction schedule [(epoch, task_id)]
    pub fn new(schedule: Vec<(usize, usize)>) -> Self {
        let mut sorted_schedule = schedule;
        sorted_schedule.sort_by_key(|(epoch, _)| *epoch);
        Self {
            task_schedule: sorted_schedule,
        }
    }

    /// Get active tasks for the current epoch.
    ///
    /// # Arguments
    /// * `epoch` - Current training epoch
    ///
    /// # Returns
    /// Set of active task IDs
    pub fn get_active_tasks(&self, epoch: usize) -> Vec<usize> {
        self.task_schedule
            .iter()
            .filter(|(start_epoch, _)| *start_epoch <= epoch)
            .map(|(_, task_id)| *task_id)
            .collect()
    }
}

impl Default for TaskCurriculum {
    fn default() -> Self {
        // Default: single task from epoch 0
        Self {
            task_schedule: vec![(0, 0)],
        }
    }
}

/// Manager for curriculum learning that tracks training progress.
pub struct CurriculumManager {
    strategy: Box<dyn CurriculumStrategyClone>,
    difficulty_cache: HashMap<String, Array1<f64>>,
    current_epoch: usize,
}

impl std::fmt::Debug for CurriculumManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CurriculumManager")
            .field("current_epoch", &self.current_epoch)
            .field("num_cached_difficulties", &self.difficulty_cache.len())
            .finish()
    }
}

/// Helper trait for cloning curriculum strategies.
trait CurriculumStrategyClone: CurriculumStrategy {
    fn clone_box(&self) -> Box<dyn CurriculumStrategyClone>;
}

impl<T: CurriculumStrategy + Clone + 'static> CurriculumStrategyClone for T {
    fn clone_box(&self) -> Box<dyn CurriculumStrategyClone> {
        Box::new(self.clone())
    }
}

impl Clone for Box<dyn CurriculumStrategyClone> {
    fn clone(&self) -> Self {
        self.clone_box()
    }
}

impl CurriculumStrategy for Box<dyn CurriculumStrategyClone> {
    fn select_samples(
        &self,
        epoch: usize,
        total_epochs: usize,
        difficulties: &ArrayView1<f64>,
    ) -> TrainResult<Vec<usize>> {
        (**self).select_samples(epoch, total_epochs, difficulties)
    }

    fn compute_difficulty(
        &self,
        data: &Array2<f64>,
        labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<Array1<f64>> {
        (**self).compute_difficulty(data, labels, predictions)
    }
}

impl CurriculumManager {
    /// Create a new curriculum manager.
    ///
    /// # Arguments
    /// * `strategy` - Curriculum learning strategy
    pub fn new<S: CurriculumStrategy + Clone + 'static>(strategy: S) -> Self {
        Self {
            strategy: Box::new(strategy),
            difficulty_cache: HashMap::new(),
            current_epoch: 0,
        }
    }

    /// Update the current epoch.
    pub fn set_epoch(&mut self, epoch: usize) {
        self.current_epoch = epoch;
    }

    /// Compute and cache difficulty scores.
    ///
    /// # Arguments
    /// * `key` - Cache key (e.g., "train", "val")
    /// * `data` - Training data
    /// * `labels` - Training labels
    /// * `predictions` - Optional model predictions
    pub fn compute_difficulty(
        &mut self,
        key: &str,
        data: &Array2<f64>,
        labels: &Array2<f64>,
        predictions: Option<&Array2<f64>>,
    ) -> TrainResult<()> {
        let difficulties = self
            .strategy
            .compute_difficulty(data, labels, predictions)?;
        self.difficulty_cache.insert(key.to_string(), difficulties);
        Ok(())
    }

    /// Get selected sample indices for training.
    ///
    /// # Arguments
    /// * `key` - Cache key for difficulty scores
    /// * `total_epochs` - Total number of training epochs
    ///
    /// # Returns
    /// Indices of samples to use for current epoch
    pub fn get_selected_samples(&self, key: &str, total_epochs: usize) -> TrainResult<Vec<usize>> {
        let difficulties = self.difficulty_cache.get(key).ok_or_else(|| {
            TrainError::InvalidParameter(format!("No difficulty scores cached for key: {}", key))
        })?;

        self.strategy
            .select_samples(self.current_epoch, total_epochs, &difficulties.view())
    }

    /// Clear the difficulty cache.
    pub fn clear_cache(&mut self) {
        self.difficulty_cache.clear();
    }
}

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

    #[test]
    fn test_linear_curriculum() {
        let curriculum = LinearCurriculum::new(0.2).expect("unwrap");
        let difficulties = array![0.1, 0.5, 0.3, 0.9, 0.2];

        // At epoch 0, should select 20% of samples (1 sample)
        let selected = curriculum
            .select_samples(0, 10, &difficulties.view())
            .expect("unwrap");
        assert_eq!(selected.len(), 1);

        // At epoch 9 (last epoch), should select all samples
        let selected = curriculum
            .select_samples(9, 10, &difficulties.view())
            .expect("unwrap");
        assert_eq!(selected.len(), 5);
    }

    #[test]
    fn test_linear_curriculum_invalid() {
        assert!(LinearCurriculum::new(-0.1).is_err());
        assert!(LinearCurriculum::new(1.5).is_err());
    }

    #[test]
    fn test_exponential_curriculum() {
        let curriculum = ExponentialCurriculum::new(0.1, 2.0).expect("unwrap");
        let difficulties = array![0.1, 0.5, 0.3, 0.9, 0.2];

        let selected = curriculum
            .select_samples(0, 10, &difficulties.view())
            .expect("unwrap");
        assert!(!selected.is_empty());

        let selected = curriculum
            .select_samples(9, 10, &difficulties.view())
            .expect("unwrap");
        // Should select most/all samples at the end (exponential growth may round differently)
        assert!(selected.len() >= 4);
    }

    #[test]
    fn test_self_paced_curriculum() {
        let curriculum = SelfPacedCurriculum::new(1.0, 0.5).expect("unwrap");
        let difficulties = array![0.1, 0.6, 0.3, 0.9, 0.2];

        // Should select samples with difficulty < 0.5
        let selected = curriculum
            .select_samples(0, 10, &difficulties.view())
            .expect("unwrap");
        assert_eq!(selected.len(), 3); // indices 0, 2, 4
    }

    #[test]
    fn test_competence_curriculum() {
        let curriculum = CompetenceCurriculum::new(0.3, 0.1).expect("unwrap");
        let difficulties = array![0.1, 0.5, 0.3, 0.9, 0.2];

        // At epoch 0, competence = 0.3, should select difficulties <= 0.3
        let selected = curriculum
            .select_samples(0, 10, &difficulties.view())
            .expect("unwrap");
        assert_eq!(selected.len(), 3); // indices 0, 2, 4

        // At epoch 5, competence = 0.8, should select more samples
        let selected = curriculum
            .select_samples(5, 10, &difficulties.view())
            .expect("unwrap");
        assert!(selected.len() >= 3);
    }

    #[test]
    fn test_task_curriculum() {
        let curriculum = TaskCurriculum::new(vec![(0, 0), (5, 1), (10, 2)]);

        let tasks = curriculum.get_active_tasks(0);
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0], 0);

        let tasks = curriculum.get_active_tasks(7);
        assert_eq!(tasks.len(), 2);
        assert!(tasks.contains(&0));
        assert!(tasks.contains(&1));

        let tasks = curriculum.get_active_tasks(15);
        assert_eq!(tasks.len(), 3);
    }

    #[test]
    fn test_difficulty_computation() {
        let curriculum = LinearCurriculum::default();

        // Test with predictions
        let data = array![[1.0, 2.0], [3.0, 4.0]];
        let labels = array![[1.0, 0.0], [0.0, 1.0]];
        let predictions = array![[0.8, 0.2], [0.3, 0.7]];

        let difficulties = curriculum
            .compute_difficulty(&data, &labels, Some(&predictions))
            .expect("unwrap");
        assert_eq!(difficulties.len(), 2);
        assert!(difficulties.iter().all(|&d| d >= 0.0));

        // Test without predictions
        let difficulties = curriculum
            .compute_difficulty(&data, &labels, None)
            .expect("unwrap");
        assert_eq!(difficulties.len(), 2);
        assert!(difficulties.iter().all(|&d| d == 0.0));
    }

    #[test]
    fn test_curriculum_manager() {
        let mut manager = CurriculumManager::new(LinearCurriculum::default());

        let data = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let labels = array![[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]];
        let predictions = array![[0.8, 0.2], [0.3, 0.7], [0.6, 0.4]];

        // Compute difficulties
        manager
            .compute_difficulty("train", &data, &labels, Some(&predictions))
            .expect("unwrap");

        // Get selected samples
        manager.set_epoch(0);
        let selected = manager.get_selected_samples("train", 10).expect("unwrap");
        assert!(!selected.is_empty());

        // Clear cache
        manager.clear_cache();
    }

    #[test]
    fn test_curriculum_manager_missing_key() {
        let manager = CurriculumManager::new(LinearCurriculum::default());
        let result = manager.get_selected_samples("nonexistent", 10);
        assert!(result.is_err());
    }

    #[test]
    fn test_linear_curriculum_without_sorting() {
        let curriculum = LinearCurriculum::new(0.5)
            .expect("unwrap")
            .without_sorting();
        let difficulties = array![0.9, 0.1, 0.5, 0.3, 0.7];

        // Should not sort by difficulty
        let selected = curriculum
            .select_samples(0, 10, &difficulties.view())
            .expect("unwrap");
        assert_eq!(selected.len(), 3); // 50% of 5 samples, rounded up
    }

    #[test]
    fn test_empty_difficulties() {
        let curriculum = LinearCurriculum::default();
        let difficulties = Array1::<f64>::zeros(0);

        let selected = curriculum
            .select_samples(0, 10, &difficulties.view())
            .expect("unwrap");
        assert_eq!(selected.len(), 0);
    }
}