ex_ml_regression.rs
Train a linear regression model. Autograd, SGD, and explicit loss readback.
ExSDK source-ownedRustMl
Source
Ml · Vulkan compute
Example
1use oa::{Matrix, ml};23fn 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])?;67 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)?;910 let mut initial_loss = 0.0_f32;11 let mut loss_matrix = Matrix::new_from_f32([1], &[0.0])?;1213 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()?;2122 if step == 0 {23 initial_loss = loss_matrix.read_f32()?[0];24 }25 }2627 let final_loss = loss_matrix.read_f32()?[0];28 let values = model.forward(&x)?.read_f32()?;2930 assert!(final_loss < initial_loss * 0.01);31 assert!(32 values33 .iter()34 .zip([-3.0_f32, -1.0, 1.0, 3.0, 5.0])35 .all(|(&a, b)| (a - b).abs() < 0.1)36 );3738 println!("loss: {initial_loss:.6} -> {final_loss:.6}");39 println!("{values:?}");40 Ok(())41}42