This project was originally my bachelor thesis, the code used is preserved in the thesis branch.
This is a thesis project to rewrite the micrograd framework by Andrej Karpathy to Rust. The purpose of this thesis is to discuss memory allocation of micrograd when rewritten in the Rust programming language. I looked over the limitations of writing the project similarly to Rust and how it can be improved to focus on stack allocation (which I will implement in the micrograd_v2). The report will be added to this project once it is published.
let a = Value::from(-4.0);
let b = Value::from(2.0);
let mut c = &a + &b;
let mut d = &a * &b + b.pow(&Value::from(3.0));
c = &Value::from(2.0) * &c + Value::from(1.0);
c = Value::from(1.0) + &Value::from(2.0) * &c + (-&a);
d = &d + &(&d * &Value::from(2.0)) + (&b + &a).relu();
d = &d + &(&Value::from(3.0) * &d) + (&b - &a).relu();
let e = &c - &d;
let f = e.pow(&Value::from(2.0));
let mut g = &f / &Value::from(2.0);
g = g + &Value::from(10.0) / &f;
println!("{:.4}", g.borrow().data); // prints 24.7041
g.backward();
println!("{:.4}", a.borrow().grad); // print 138.8338
println!("{:.4}", b.borrow().grad); // print 645.5773
We also implemented a draw_dot function that will act similarly to graphviz and digraph. This will allow us to visualize each node, showing both their data and gradient. An example below shows how to run it. Also trace_graph.ipynb
shows how to achieve this using evxcr jupyter kernel
use micrograd::engine::Value;
use micrograd::nn::Neuron;
let x = Value::from(1.0);
let y = (x * Value::from(2) + Value::from(1)).relu();
y.backward();
draw_dot(y);
All tests are in the tests
folder. You can run them with the following command.
cargo test
MIT