Reverse mode automatic differentiation in Rust.
To use this in your crate, add the following to Cargo.toml
:
rust
[dependencies]
reverse = "0.1"
```rust use reverse::*;
fn main() { let graph = Graph::new(); let a = graph.addvar(2.5); let b = graph.addvar(14.); let c = (a.sin() + b.ln() * 3.) - 5.; let gradients = c.backward();
asserteq!(gradients.wrt(&a), 2.5f64.cos()); assert_eq!(gradients.wrt(&b), 3. / 14.); } ```
There is an optional diff
feature which activates a macro which automatically transforms functions to the right type so that they are differentiable. That is, functions that act on f64
s can be used on differentiable variables without change.
To use this, add the following to Cargo.toml
:
rust
reverse = { version = "0.1", features = ["diff"] }
Here is an example of what the feature allows you to do:
```rust use reverse::*;
fn main() { let graph = Graph::new(); let a = graph.addvar(5.); let b = graph.addvar(2.); let res = addmul(&[a, b], &[&[1.]]); println!("{:?}", res.backward()); }
// function must have these argument types but can be arbitrarily complex
fn addmul(params: &[f64], data: &[&[f64]]) -> f64 { params[0] + data[0][0] * params[1] } ```