Coder Social home page Coder Social logo

autograd's Introduction

autograd

A tiny Autograd engine written in Java. Implements backpropagation (reverse-mode autodiff) over a dynamically built DAG.

Inspired by a python project created by @karpathy at https://github.com/karpathy/micrograd

Example usage

Below is a slightly contrived example showing a number of possible supported operations ( from AutogradDemo)

                // a = Value(-4.0)
                var a = createGradValue(-4.0f, true);

                // b = Value(2.0)
                var b = createGradValue(2.0f, true);

                // c = a + b
                var c = a.add(b);

                // d = a * b + b**3
                var d = a.mul(b).add(b.mul(b).mul(b));

                // c += c + 1
                c = c.add(c.add(1));

                // c += 1 + c + (-a)
                c = c.add(createGradValue(1.0f, false).add(c).add(a.neg()));

                // d += d * 2 + (b + a).relu()
                d = d.add(d.mul(2).add(b.add(a).relu()));

                // d += 3 * d + (b - a).relu()
                d = d.add(createGradValue(3.0f, false).mul(d).add(b.sub(a).relu()));

                // e = c - d
                var e = c.sub(d);

                // f = e**2
                var f = e.mul(e);

                // g = f / 2.0
                var g = f.div(2.0f);

                // g += 10.0 / f
                g = g.add(createGradValue(10.0f, false).div(f));

                System.out.println(g.data().get()); // Prints 24.704082, the outcome of this forward pass
                    
                // Perform a backward pass        
                g.backward();
                
                System.out.println(a.grad().data().get()); // Prints 138.83382, i.e. the numerical value of dg/da
                        
                System.out.println(b.grad().data().get()); // Prints 645.5772, i.e. the numerical value of dg/db

autograd's People

Contributors

michaellavelle avatar

Forkers

michaellavelle

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.