Realm

ex_ml_regression.rs

Train a linear regression model. Autograd, SGD, and explicit loss readback.

ExSDK source-ownedRustMl

Source

Example

1use oa::{Matrix, ml};
2
3fn main() -> oa::Result<()> {
4 let x = Matrix::new_from_f32([5, 1], &[-2.0, -1.0, 0.0, 1.0, 2.0])?;
5 let target = Matrix::new_from_f32([5, 1], &[-3.0, -1.0, 1.0, 3.0, 5.0])?;
6
7 let model = ml::nn::Linear::new_with_seed(1, 1, 0)?;
8 let mut optimizer = ml::Sgd::new(model.parameters(), 0.05, 0.0, 0.0)?;
9
10 let mut initial_loss = 0.0_f32;
11 let mut loss_matrix = Matrix::new_from_f32([1], &[0.0])?;
12
13 for step in 0..80 {
14 optimizer.zero_grad();
15 let tape = ml::GradientTape::new();
16 let prediction = model.forward(&x)?;
17 loss_matrix = ml::loss::mse(&prediction, &target)?;
18 tape.backward(&loss_matrix)?;
19 drop(tape);
20 optimizer.step()?;
21
22 if step == 0 {
23 initial_loss = loss_matrix.read_f32()?[0];
24 }
25 }
26
27 let final_loss = loss_matrix.read_f32()?[0];
28 let values = model.forward(&x)?.read_f32()?;
29
30 assert!(final_loss < initial_loss * 0.01);
31 assert!(
32 values
33 .iter()
34 .zip([-3.0_f32, -1.0, 1.0, 3.0, 5.0])
35 .all(|(&a, b)| (a - b).abs() < 0.1)
36 );
37
38 println!("loss: {initial_loss:.6} -> {final_loss:.6}");
39 println!("{values:?}");
40 Ok(())
41}
42