npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simplegrad

v1.0.0

Published

Simple reverse mode automatic differentiation of scalar values in javascript

Downloads

12

Readme

simplegrad

This library implements efficient automatic differentiation of scalar valued functions. It is lightweight and fast.

Similar to existing educational libraries like micrograd, this library operate on computational graphs. Unlike existing educational libraries, this library compiles computational graphs into efficient javascript code at runtime, which results in extremely fast forward/backward passes, making this library quite useful for quick prototyping and even real world applications.

How to use it?

First, you need to install it from npm.

npm install --save simplegrad

Now let's create a simple function f(a, b) = (a + b) * a, and compute its value at point a = 2, b = 3:

import {Variable, ValueStorage} from 'simplegrad';

// First, let's construct computational graph of our f(a, b) function:
let valueStorage = new ValueStorage();

let a = new Variable(vs);
let b = new Variable(vs);
let f = a.add(b).mul(a);

// The graph is ready, let's produce efficient code for
// function computation:
f.compile();

// At this point we are ready to compute f(2, 3), but how do we
// assign concrete values to variables? Use `setValue()` method:
a.setValue(2);
b.setValue(3);

// The values are set, let's propagate them forward in the computational graph:
f.forward();

// If we want to know the value of f(2, 3), we can call `getValue()`:
// Note: performance of `getValue()` is O(1), as all values are already computed
// in `f.forward()` call.
assert(f.getValue() === (2 + 3) * 2);

// To learn value of the `df_da` and `df_db` at point (2, 3)
// let's propagate gradient backward.
// First, we need to tell what the value df_df is, as it could be
// coming from external source. Most of the time however it should be just `1`:
f.setGradient(1);

// Propagate gradient backward:
f.backward();

// Remember our f(a, b) = (a + b) * a, so df_da = 2 * a + b, df_db = a:
assert(a.getGradient() === 2 * a.getValue() + b.getValue());
assert(b.getGradient() === a.getValue());

Under the hood

If you open the hood of this library you'll notice a beautiful engine. Each Variable instance upon creation reserves a slot in the ValueStorage. ValueStorage is a simple object that stores values of variables in typed array:

// somewhere inside ValueStorage
// `v` variable stores values of all variables for the forward pass:
v = new Float64Array(variablesCount);
// `gv` variable stores gradient values for the backward pass:
gv = new Float64Array(variablesCount);

When you call compile(), the computational graph is traversed in topological order only once. The topological traversal implementation is non-recursive, which allows to traverse huge graphs without Maximum call stack size exceeded error.

Once traversal is done, each variable has an opportunity to produce code for forward/backward passes, and ValueStorage creates and returns dynamic functions that can be reused for different values without re-compilation, graph traversal, or memory allocations.

For example, function f(a, b) = (a + b) * a will be compiled by f.compile() call, which in turn will ask ValueStorage to produce f.forward() and f.backward() functions:

// This code is dynamically compiled by `ValueStorage`:
var v = new Float64Array(4);
var gv = new Float64Array(4);

function forward() {
  v[2] = v[0] + v[1];
  v[3] = v[2] * v[0];
}

function backward() {
  gv[2] += v[0] * gv[3]; gv[0] += v[2] * gv[3]
  gv[0] += gv[2]; gv[1] += gv[2]
}

return {
  forward: forward,
  backward: backward,
  v: v,
  gv: gv
}

As you can see the produced code is extremely efficient, each variable is computed only once, no new objects are allocated, and the code can be optimized by javascript engines.

Each operation that can be performed on Variable instance is implemented in Variable.js file. You can easily extend it with your own custom functions.

Feedback

Please let me know how it goes. You can also sponsor my projects here - your funds will be dedicated to more libraries and data visualizations.

License

MIT License