Skip to main content

regression/
regression.rs

1use rusting_brain::{Activation, Dataset, Loss, Network, Optimizer, TrainConfig};
2
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut inputs = Vec::new();
5    let mut targets = Vec::new();
6
7    for i in 0..20 {
8        for j in 0..20 {
9            let x0 = (i as f32) / 20.0;
10            let x1 = (j as f32) / 20.0;
11            inputs.push(vec![x0, x1]);
12            targets.push(vec![x0 + x1]);
13        }
14    }
15
16    let dataset = Dataset::new(inputs, targets);
17    let mut model = Network::builder()
18        .input_size(2)
19        .dense(8, Activation::Relu)
20        .dense(1, Activation::Linear)
21        .loss(Loss::Mse)
22        .optimizer(Optimizer::adam(0.01))
23        .build();
24
25    let initial = model.evaluate_loss(&dataset)?;
26    println!("initial loss: {initial:.6}");
27
28    // `fit_with` instead of `fit`: five hundred epochs are otherwise five
29    // hundred epochs of silence, and this problem is solved long before the
30    // last one. Returning `false` stops the loop, and the history holds only
31    // the epochs that ran.
32    let history = model.fit_with(
33        &dataset,
34        TrainConfig {
35            epochs: 500,
36            batch_size: 16,
37            shuffle: true,
38            seed: Some(3),
39        },
40        |epoch, loss| {
41            if epoch % 100 == 0 {
42                println!("  epoch {epoch:>3}  loss {loss:.6}");
43            }
44            loss > 1e-5
45        },
46    )?;
47    let final_loss = model.evaluate_loss(&dataset)?;
48
49    println!("epochs run:   {}", history.losses.len());
50    println!("final loss:   {final_loss:.6}");
51    println!("[0.25, 0.50] -> {:.4}", model.predict(&[0.25, 0.50])?[0]);
52
53    Ok(())
54}