Machine learning basics
Source-owned programs from the current OA SDK. Rust and Python examples follow their respective public APIs.
Rust · Python
The former Fashion-MNIST classifier tutorial has not been admitted to the Rust SDK. This page now points to the current Python ML basics program; a paired Rust regression example is available in the SDK.
Example
This example is currently documented in Python only.
Tu_ml_basics.py
1import oa2x = oa.Matrix.new_from_f32([5, 1], [-2.0, -1.0, 0.0, 1.0, 2.0])3target = oa.Matrix.new_from_f32([5, 1], [-3.0, -1.0, 1.0, 3.0, 5.0])45model = oa.ml.Linear(1, 1, seed=0)6optimizer = oa.ml.Sgd(model.parameters(), learning_rate=0.05)78initial_loss = 0.09for step in range(80):10 optimizer.zero_grad()11 with oa.ml.GradientTape() as tape:12 prediction = model.forward(x)13 loss = oa.ml.loss.mse(prediction, target)14 tape.backward(loss)15 optimizer.step()16 if step == 0:17 initial_loss = oa.ml.metric.scalar_loss(loss)1819final_loss = oa.ml.metric.scalar_loss(loss)20prediction = model.forward(x)21values = prediction.read_f32()2223assert final_loss < initial_loss * 0.0124assert max(abs(a - b) for a, b in zip(values, [-3, -1, 1, 3, 5])) < 0.12526print(f"loss: {initial_loss:.6f} -> {final_loss:.6f}")27print(values)28
Run Rust tutorials from the OA checkout with cargo run --release --example <name>. Run Python with the environment containing the OA native extension. Consult each program's arguments and capability checks before running it.