FL03 / acme

Acme aims to be a complete auto differentiation system written in Rust.
https://crates.io/crates/acme
Apache License 2.0
3 stars 0 forks source link

Create a set of integrated procedural macros for building differentiable functions, methods, etc. #77

Open FL03 opened 4 months ago

FL03 commented 4 months ago

Example

extern crate acme;

use acme::{autodiff, operator};

fn main() {
    let x = 5f64;
    let (z, dz) = (sigmoid(x), sigmoid_prime(x));
    assert_eq!(autodiff!(lex x: sigmoid_lexical()), dz);
}

#[operator(partial)]
pub fn sigmoid<T>(x: T) -> T where T: num::Float {
    x.exp() / (T::one() + x.exp())
}

pub fn sigmoid_prime<T>(x: T) -> T where T: num::Float {
    sigmoid(x) * (1 - sigmoid(x)
}

Try to integrate with the #[operator] macro by collecting the String created by invoking _lexical()

FL03 commented 4 months ago

It may be possible to generate a macro that represents the gradient;

extern crate acme;

use acme::operator;

fn main() {
    let dx = mul_gradient!(x: 10f64);

    let (x, y) = (3f64, 4f64);
    let dx = mul_gradient!(x);
    let dy = mul_gradient!(y);

    assert_eq!(dx, y);
    assert_eq!(dy, x);
}

#[operator]
pub fn mul<A, B, C>(x: A, y: B) -> C where A: core::ops::Mul<B, Output = C> {
    x * y
}