Giter VIP home page Giter VIP logo

micrograd's Introduction

micrograd

a rust port of andrej karpathy's https://github.com/karpathy/micrograd

why

an exercise while following along with https://www.youtube.com/watch?v=VMj-3S1tku0

example

use micrograd::nn::{flatten, from_tensor, rmse, tensor, Tensor, MLP};

fn train_mlp(xs: &[Tensor], ys: &Tensor, step: f64, iterations: usize) -> MLP {
    let mut nn = MLP::new(3, 4, vec![4, 1]);
    for i in 0..iterations {
        // forward pass
        let ypred = flatten(nn.forward2(xs));
        let mut loss = rmse(&ypred, ys);

        // backward pass
        for mut p in nn.parameters() {
            p.zero_grad();
        }
        loss.backward();

        // update parameters
        for mut p in nn.parameters() {
            p.adjust_val(step * -p.grad());
        }

        if i % 10 == 0 {
            println!("{:5} loss {}", i, loss.val());
        }
    }
    nn
}

fn main() {
    let xs = [
        tensor([2.0, 3.0, -1.0]),
        tensor([3.0, -1.0, 0.5]),
        tensor([0.5, 1.0, 1.0]),
        tensor([1.0, 1.0, -1.0]),
    ];
    let ys = tensor([1.0, -1.0, -1.0, 1.0]);
    let nn = train_mlp(&xs, &ys, 0.01, 200);
    let ypred = flatten(nn.forward2(&xs));
    println!("predictions: {:?}", from_tensor(&ypred));
    println!("rmse: {}", rmse(&ypred, &ys).val());
}

who

daniel connelly [email protected] (https://dhconnelly.com)

license

MIT

micrograd's People

Contributors

dhconnelly avatar

Stargazers

Clément avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.