Socket
Book a DemoInstallSign in
Socket

js-grad

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-grad

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

1.0.5
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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();

FAQs

Package last updated on 24 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.