Skip to main content

batch_vs_incremental_comparison/
batch_vs_incremental_comparison.rs

1use incremental_rs::{
2    BatchStats, IncrementalLinearRegression, IncrementalSupervisedEstimator,
3    LearningRateSchedule, MonitoredEstimator,
4};
5use ndarray::{array, Array2};
6
7fn main() {
8    println!("===Batch vs. Incremental Convergence Comparison ===");
9
10    let x = Array2::from_shape_vec(
11        (8, 2),
12        vec![
13            1.0, 2.0, 2.0, 1.0, 3.0, 4.0, 5.0, 2.0, 4.0, 1.0, 6.0, 3.0, 7.0, 2.0, 8.0, 5.0,
14        ],
15    )
16    .unwrap();
17    let y = array![-3.5, 1.5, -5.5, 4.5, 5.5, 3.5, 8.5, 1.5];
18
19    let schedule = LearningRateSchedule::InverseScaling {
20        initial_rate: 0.05,
21        decay: 0.01,
22        power: 0.5,
23    };
24
25    let mut model = IncrementalLinearRegression::new(schedule, 0.01);
26
27    println!("Step | Mini-Batch MSE Loss");
28    println!("--------------------------");
29
30    let mut monitored = MonitoredEstimator::new(&mut model, |stats: BatchStats| {
31        println!("{:4} | {:.6}", stats.step, stats.loss);
32    });
33
34    // Run multiple incremental epoch passes over data[cite: 1]
35    for _ in 0..10 {
36        monitored.partial_fit(&x, &y).unwrap();
37    }
38}