aprender-core 0.29.2

Next-generation machine learning library in pure Rust
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
//! Curriculum Learning for Progressive Training
//!
//! Train on easy samples first, then progressively harder ones to
//! improve convergence speed and model quality.
//!
//! # References
//!
//! - [Bengio et al. 2009] "Curriculum Learning" - Theoretical foundation
//! - [Kumar et al. 2010] "Self-Paced Learning for Latent Variable Models"
//!
//! # Toyota Way Principles
//!
//! - **Heijunka**: Level training workload by ordering samples
//! - **Kaizen**: Progressive improvement through staged learning

use crate::error::{AprenderError, Result};

/// Difficulty scorer for samples
pub trait DifficultyScorer: Send + Sync {
    /// Score sample difficulty (lower = easier)
    ///
    /// # Arguments
    /// * `features` - Feature vector
    /// * `target` - Target value
    ///
    /// # Returns
    /// Difficulty score (typically 0.0 to 1.0, but can be unbounded)
    fn score(&self, features: &[f64], target: f64) -> f64;
}

/// Curriculum learning scheduler
///
/// Reference: [Bengio et al. 2009] "Curriculum Learning"
/// - Ordering samples from easy to hard improves convergence
/// - 25% faster training, better generalization
pub trait CurriculumScheduler: Send + Sync {
    /// Get current stage (0.0 = easiest only, 1.0 = all data)
    fn stage(&self) -> f64;

    /// Advance to next stage
    fn advance(&mut self);

    /// Check if curriculum is complete
    fn is_complete(&self) -> bool;

    /// Get the difficulty threshold for current stage
    fn current_threshold(&self) -> f64;

    /// Reset to beginning
    fn reset(&mut self);
}

/// Sample with cached difficulty
#[derive(Debug, Clone)]
pub struct ScoredSample {
    /// Feature vector
    pub features: Vec<f64>,
    /// Target value
    pub target: f64,
    /// Cached difficulty score
    pub difficulty: f64,
}

impl ScoredSample {
    /// Create a new scored sample
    #[must_use]
    pub fn new(features: Vec<f64>, target: f64, difficulty: f64) -> Self {
        Self {
            features,
            target,
            difficulty,
        }
    }
}

/// Linear curriculum that progresses through difficulty levels
#[derive(Debug, Clone)]
pub struct LinearCurriculum {
    /// Current stage (0.0 to 1.0)
    stage: f64,
    /// Stage increment per advance
    step_size: f64,
    /// Maximum difficulty at each stage
    difficulty_range: (f64, f64),
}

impl Default for LinearCurriculum {
    fn default() -> Self {
        Self::new(10)
    }
}

impl LinearCurriculum {
    /// Create a linear curriculum with specified number of stages
    ///
    /// # Arguments
    /// * `n_stages` - Number of stages (higher = finer progression)
    #[must_use]
    pub fn new(n_stages: usize) -> Self {
        let step_size = if n_stages > 0 {
            1.0 / n_stages as f64
        } else {
            1.0
        };

        Self {
            stage: 0.0,
            step_size,
            difficulty_range: (0.0, 1.0),
        }
    }

    /// Set custom difficulty range
    #[must_use]
    pub fn with_difficulty_range(mut self, min: f64, max: f64) -> Self {
        self.difficulty_range = (min, max);
        self
    }
}

impl CurriculumScheduler for LinearCurriculum {
    fn stage(&self) -> f64 {
        self.stage
    }

    fn advance(&mut self) {
        self.stage = (self.stage + self.step_size).min(1.0);
    }

    fn is_complete(&self) -> bool {
        self.stage >= 1.0
    }

    fn current_threshold(&self) -> f64 {
        let (min, max) = self.difficulty_range;
        min + (max - min) * self.stage
    }

    fn reset(&mut self) {
        self.stage = 0.0;
    }
}

/// Exponential curriculum that progresses slowly at first
#[derive(Debug, Clone)]
pub struct ExponentialCurriculum {
    /// Current stage (0.0 to 1.0)
    stage: f64,
    /// Growth rate (higher = faster progression)
    growth_rate: f64,
    /// Number of advances
    n_advances: u64,
}

impl Default for ExponentialCurriculum {
    fn default() -> Self {
        Self::new(0.3)
    }
}

impl ExponentialCurriculum {
    /// Create an exponential curriculum
    ///
    /// # Arguments
    /// * `growth_rate` - Growth rate (typical: 0.1-0.5)
    #[must_use]
    pub fn new(growth_rate: f64) -> Self {
        Self {
            stage: 0.0,
            growth_rate,
            n_advances: 0,
        }
    }
}

impl CurriculumScheduler for ExponentialCurriculum {
    fn stage(&self) -> f64 {
        self.stage
    }

    fn advance(&mut self) {
        self.n_advances += 1;
        // Exponential growth: 1 - e^(-r*t)
        self.stage = 1.0 - (-self.growth_rate * self.n_advances as f64).exp();
    }

    fn is_complete(&self) -> bool {
        self.stage >= 0.99
    }

    fn current_threshold(&self) -> f64 {
        self.stage
    }

    fn reset(&mut self) {
        self.stage = 0.0;
        self.n_advances = 0;
    }
}

/// Self-paced curriculum learning
///
/// Reference: [Kumar et al. 2010] "Self-Paced Learning for Latent
/// Variable Models"
/// - Automatically determines sample difficulty from loss
/// - Adapts pace based on model performance
#[derive(Debug, Clone)]
pub struct SelfPacedCurriculum {
    /// All training samples with cached difficulties
    samples: Vec<ScoredSample>,
    /// Current difficulty threshold
    threshold: f64,
    /// Initial threshold
    initial_threshold: f64,
    /// Threshold growth rate
    growth_rate: f64,
    /// Maximum threshold
    max_threshold: f64,
    /// Current batch index
    batch_idx: usize,
}

impl SelfPacedCurriculum {
    /// Create a new self-paced curriculum
    ///
    /// # Arguments
    /// * `initial_threshold` - Starting difficulty threshold (e.g., 0.1)
    /// * `growth_rate` - How much to increase threshold per advance (e.g., 1.5)
    #[must_use]
    pub fn new(initial_threshold: f64, growth_rate: f64) -> Self {
        Self {
            samples: Vec::new(),
            threshold: initial_threshold,
            initial_threshold,
            growth_rate,
            max_threshold: f64::MAX,
            batch_idx: 0,
        }
    }

    /// Set maximum threshold
    #[must_use]
    pub fn with_max_threshold(mut self, max: f64) -> Self {
        self.max_threshold = max;
        self
    }

    /// Add samples with difficulty scores
    pub fn add_samples(&mut self, samples: Vec<ScoredSample>) {
        self.samples.extend(samples);
        // Sort by difficulty (easiest first)
        self.samples.sort_by(|a, b| {
            a.difficulty
                .partial_cmp(&b.difficulty)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
    }

    /// Update difficulties based on model predictions
    ///
    /// # Arguments
    /// * `loss_fn` - Function that computes loss for a sample
    pub fn update_difficulties<F>(&mut self, loss_fn: F)
    where
        F: Fn(&[f64], f64) -> f64,
    {
        for sample in &mut self.samples {
            sample.difficulty = loss_fn(&sample.features, sample.target);
        }

        // Re-sort by difficulty
        self.samples.sort_by(|a, b| {
            a.difficulty
                .partial_cmp(&b.difficulty)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
    }

    /// Get next batch of samples below current threshold
    pub fn next_batch(&mut self, batch_size: usize) -> Vec<&ScoredSample> {
        contract_pre_streaming_data_loader!();
        let eligible: Vec<_> = self
            .samples
            .iter()
            .filter(|s| s.difficulty <= self.threshold)
            .collect();

        let start = self.batch_idx;
        let end = (start + batch_size).min(eligible.len());

        if start >= eligible.len() {
            self.batch_idx = 0;
            let result: Vec<&ScoredSample> = vec![];
            contract_post_streaming_data_loader!(&result);
            return result;
        }

        self.batch_idx = end;
        let result = eligible[start..end].to_vec();
        contract_post_streaming_data_loader!(&result);
        result
    }

    /// Get all samples below current threshold
    #[must_use]
    pub fn eligible_samples(&self) -> Vec<&ScoredSample> {
        self.samples
            .iter()
            .filter(|s| s.difficulty <= self.threshold)
            .collect()
    }

    /// Get number of eligible samples
    #[must_use]
    pub fn n_eligible(&self) -> usize {
        self.samples
            .iter()
            .filter(|s| s.difficulty <= self.threshold)
            .count()
    }

    /// Get total number of samples
    #[must_use]
    pub fn n_total(&self) -> usize {
        self.samples.len()
    }

    /// Get current threshold
    #[must_use]
    pub fn threshold(&self) -> f64 {
        self.threshold
    }
}

impl CurriculumScheduler for SelfPacedCurriculum {
    fn stage(&self) -> f64 {
        if self.samples.is_empty() {
            return 1.0;
        }

        let eligible = self.n_eligible();
        eligible as f64 / self.samples.len() as f64
    }

    fn advance(&mut self) {
        self.threshold = (self.threshold * self.growth_rate).min(self.max_threshold);
        self.batch_idx = 0;
    }

    fn is_complete(&self) -> bool {
        self.n_eligible() >= self.samples.len()
    }

    fn current_threshold(&self) -> f64 {
        self.threshold
    }

    fn reset(&mut self) {
        self.threshold = self.initial_threshold;
        self.batch_idx = 0;
    }
}

/// Loss-based difficulty scorer
///
/// Uses prediction error as difficulty proxy
#[derive(Debug, Clone)]
pub struct LossDifficultyScorer {
    /// Mean of target values (for baseline)
    target_mean: f64,
}

impl LossDifficultyScorer {
    /// Create a new loss-based scorer
    #[must_use]
    pub fn new() -> Self {
        Self { target_mean: 0.0 }
    }

    /// Create with known target mean
    #[must_use]
    pub fn with_mean(target_mean: f64) -> Self {
        Self { target_mean }
    }

    /// Estimate target mean from samples
    pub fn fit(&mut self, targets: &[f64]) {
        if !targets.is_empty() {
            self.target_mean = targets.iter().sum::<f64>() / targets.len() as f64;
        }
    }
}

impl Default for LossDifficultyScorer {
    fn default() -> Self {
        Self::new()
    }
}

impl DifficultyScorer for LossDifficultyScorer {
    fn score(&self, _features: &[f64], target: f64) -> f64 {
        // Distance from mean as proxy for difficulty
        (target - self.target_mean).abs()
    }
}

/// Feature-norm difficulty scorer
///
/// Uses feature magnitude as difficulty proxy (larger = harder)
#[derive(Debug, Clone, Default)]
pub struct FeatureNormScorer;

impl FeatureNormScorer {
    /// Create a new feature norm scorer
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl DifficultyScorer for FeatureNormScorer {
    fn score(&self, features: &[f64], _target: f64) -> f64 {
        // L2 norm of features
        features.iter().map(|x| x * x).sum::<f64>().sqrt()
    }
}

/// Curriculum-guided training helper
#[derive(Debug)]
pub struct CurriculumTrainer<S: CurriculumScheduler + std::fmt::Debug> {
    /// The curriculum scheduler
    scheduler: S,
    /// All samples sorted by difficulty
    samples: Vec<ScoredSample>,
}

include!("curriculum_trainer.rs");
include!("curriculum_tests.rs");