node package manager

weblearn


WebLearn
WebLearn

Modular neural networks for node.js and the browser.

Travis Build NPM Downloads NPM Version


WebLearn makes it super easy to write and train deep neural networks in JavaScript!

Since WebLearn is written completely in JavaScript, you can run the exact same code in the browser with browserify or in node.js.

WebLearn is extremely modular. The core WebLearn package is tiny, but it's been carefully designed to spawn a rich ecosystem of modules and tools. You can drop in new types of layers, visualizations, criteria, etc-- all with a simple require()

We borrow most of our abstractions and API conventions from Torch and Keras. Translating programs written with Torch to run on the web with WebLearn is especially straightforward.

It's basically the WebTorrent or Webcoin of neural networks.

If you're a JavaScript hacker, you may be aware that Deep Learning™ is a big deal, but maybe you've never actually used a neural network. WebLearn is for you. It's just JavaScript-- read the source and watch the mysticism around machine learning disappear. Or just snap together some layers from npm and don't give it a second thought!

AI will belong to the hackers / tinkerers of the world, and there's no larger community of tinkerers than npm / JavaScript users. WebLearn is a hackable-to-the-core pure JavaScript machine learning framework-- go forth and use it in the name of science!

v2.x

In version 2, WebLearn eschews using its own tensor class and instead uses ndarrays and functions that operate on them.

Usage

npm install weblearn
const ndarray = require('ndarray')
const { ReLU, Linear, MSE, SGD, Sequential } = require('weblearn')
 
let model = Sequential({
  optimizer: SGD(.01),
  loss: MSE()
})
 
model.add(Linear(2, 20))
  .add(ReLU())
  .add(Linear(20, 1))
 
// [ input, target ] (both ndarrays) 
 
const data = [
  [ndarray([0, 0]), ndarray([0])],
  [ndarray([0, 1]), ndarray([1])],
  [ndarray([1, 0]), ndarray([1])],
  [ndarray([1, 1]), ndarray([0])]
]
for(let i = 0; i < 1000; i++) {
  model.fit(data, { verbose: false })
}
 
data.forEach(d => {
  console.log(model.forward(d[0]))
})

Why JavaScript?

JS has some unique advantages for training neural nets:

  • Runtime availability: bundle your neural net into a single .js file and deploy it to your users! No installations, permissions, etc. Let your users bring their own compute to the deep learning-enabled features in your app.

  • Speed: when it comes to doing math on a CPU, JavaScript is actually very fast. It can also talk to the host's GPU to do math there!

  • Portability: you can run the exact same model in your user's browser and on your own servers with node. You can train your net on the client, pass the parameters to the server, and sample from it there. Or vice versa!

  • Community: lots of people know JavaScript and use npm. This makes it easier to develop a healthy ecosystem of WebLearn modules for you to use.

  • Novel architectures: p2p neural networks over WebRTC? Distributed stochastic gradient descent without data ever needing to leave a user's device for increased privacy? Some new kinds of mad science become possible when your neural networks are in JavaScript.

Modules

The main WebLearn package is simply a curated map of useful modules. Most of WebLearn's functionality lives in other modules in repositories listed here.

To add a module to this list, simply publish it on npm as weblearn-{layer,container,criterion,tensor}-yourmodulename and it will appear here when the next version of WebLearn is published.

name version tests issues description
weblearn-layer-linear Simple fully-connected layer
weblearn-layer-relu Rectified linear unit non-linearity
weblearn-layer-sigmoid Sigmoid activation layer
weblearn-container-sequential Compute child layers in sequence
weblearn-criterion-mse Mean squared error criterion
weblearn-tensor Single-process CPU tensor class
weblearn-module Base class for layers
weblearn-container Base class for containers
weblearn-criterion Base class for criteria

Writing a module

Check out the Torch docs here and look at WebLearn's Linear layer as an example. More docs coming soon.