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

js-grad

v1.0.5

Published

This is a javascript implementation of autodiff translated from micrograd by Adrej Karpathy. Then built upon this to build a torch like neural network composition

Downloads

6

Readme

JS GRAD

This translates Andrej Karpathy's micrograd into javascript and modifies the neural network composition to look more like pytorch. Thank you to Andrej!

Getting Started

First install from npm

npm install js-grad

Then import from package in javascript

import { autodiff, nn } from "js-grad";

or

const { autodiff, nn } = require("js-grad");

in node.

Example Use Case in javascript : Linear Regression

This is a simple example of linear regression. You can see this live in the console at code sandbox

import { autodiff, nn } from "js-grad";

class LinearRegression extends nn.layers.FeedForward {
  constructor() {
    super();
    this.model = new nn.layers.Linear(1, 1);
  }
  forward(X) {
    return this.model.forward(X);
  }
  parameters() {
    return this.model.parameters();
  }
}
function createLinearVector(length) {
  return {
    X: new Array(length).fill(0).map((_, i) => [new autodiff.Value(i)]),
    y: new Array(length).fill(0).map((_, i) => [new autodiff.Value(i)])
  };
}
function main() {
  const numDataPoints = 10;
  const { X, y } = createLinearVector(numDataPoints);

  const learningRate = 0.01,
    epochs = 15; // hyper params

  // create model, how to evaluate(loss) and optimize model
  const model = new LinearRegression();
  const loss = new nn.losses.MSE();
  const optimizer = new nn.optimizers.SGD(model.parameters(), learningRate);

  for (let epoch = 1; epoch <= epochs; epoch++) {
    // forward pass
    const outputs = model.forward(X);
    const totalLoss = loss.forward(outputs, y);

    // backward pass and optimize
    optimizer.zeroGrad();
    totalLoss.backward();
    optimizer.step();

    // log metrics
    console.log(`EPOCH=${epoch} \t LOSS=${totalLoss.data}`);
  }
}

main(); // run the main program for linear regression

Example Use Case : Sin Curve Fitting

import { autodiff, nn } from "js-grad";

// destructure necessary components
const { Value } = autodiff;
const {
  layers: { Sequential, Linear, ReLU, FeedForward },
  losses: { MSE },
  optimizers: { SGD }
} = nn;

class NeuralNetwork extends FeedForward {
  constructor() {
    super();
    this.model = new Sequential(
      new Linear(1, 8),
      new ReLU(),
      new Linear(8, 8),
      new ReLU(),
      new Linear(8, 1)
    );
  }
  forward(X) {
    return this.model.forward(X);
  }
  parameters() {
    return this.model.parameters();
  }
}

function generate2D(start, stop, interval, func) {
  let X = [],
    y = [];
  for (let i = start; i < stop; i += interval) {
    X.push([new Value(i)]);
    y.push([new Value(func(i))]);
  }
  return { X, y };
}
function main() {
  const { X, y } = generate2D(0, 2 * Math.PI, Math.PI / 16, Math.sin);
  console.log(X.length, y.length);

  const model = new NeuralNetwork();
  const epochs = 10000;
  const learningRate = 0.01;
  const loss = new MSE();
  const optimizer = new SGD(model.parameters(), learningRate);

  // training
  for (let epoch = 0; epoch < epochs; epoch++) {
    //forward
    const outputs = model.forward(X);
    const totalLoss = loss.forward(outputs, y);

    //backward
    optimizer.zeroGrad(); // clear gradient
    totalLoss.backward();

    // gradient descent
    optimizer.step();

    console.log(`Epoch=${epoch + 1} \t Loss=${totalLoss.data}`);
  }
}

main();