DEV Community

loading...

brain.js - Neural networks in JavaScript

Ben Halpern
A Canadian software developer who thinks he’s funny. He/Him.
・1 min read

This is a fairly remarkable project which offers a library of Neural Networks written in JavaScript.

GitHub logo BrainJS / brain.js

πŸ€– GPU accelerated Neural networks in JavaScript for Browsers and Node.js

Logo

brain.js

GPU accelerated Neural networks in JavaScript for Browsers and Node.js

GitHub npm js-standard-style Backers on Open Collective Sponsors on Open Collective Gitter Slack CI codecov Twitter

NPM

About

brain.js is a GPU accelerated library for Neural Networks written in JavaScript.

πŸ’‘ This is a continuation of the harthur/brain, which is not maintained anymore. More info

Table of Contents

Installation and Usage

NPM

If you can install brain.js with npm:

npm install brain.js
Enter fullscreen mode Exit fullscreen mode

CDN

<script src="//unpkg.com/brain.js"></script>
Enter fullscreen mode Exit fullscreen mode

Download

Download the latest brain.js for browser

Installation note

Brain.js…

Here's an example showcasing how to approximate the XOR function from the README:

// provide optional config object (or undefined). Defaults shown.
const config = {
    binaryThresh: 0.5,
    hiddenLayers: [3],     // array of ints for the sizes of the hidden layers in the network
    activation: 'sigmoid'  // supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
    leakyReluAlpha: 0.01   // supported for activation type 'leaky-relu'
};

// create a simple feed forward neural network with backpropagation
const net = new brain.NeuralNetwork(config);

net.train([{input: [0, 0], output: [0]},
           {input: [0, 1], output: [1]},
           {input: [1, 0], output: [1]},
           {input: [1, 1], output: [0]}]);

const output = net.run([1, 0]);  // [0.987]
Enter fullscreen mode Exit fullscreen mode

It's actively maintained. Definitely worth checking out.

This post is part of the new GitHunt DEV tag. Follow it out for more like this.

#githunt

Interesting open source repos you've discovered and want to share with the community.

Discussion (9)

Collapse
mvasigh profile image
Mehdi Vasigh

I love the GitHunt tag and the projects you've been posting, Ben. This has been on my list of libraries I need to play with for a long time. I like Martin's spam filter project idea; anyone else have any other projects or ideas for what to do with this that I can draw inspiration from?

Collapse
embiem profile image
Martin Beierling-Mutz • Edited

I love this one. Used it a while back in a side project to filter spammy chat messages and it worked like a charm.

It's also one of the few JS ML libraries that are actively maintained.

A big "competitor" to brain.js is TensorFlow.js, which is backed by Google and Tensorflow in general is very widely used. It was exciting to see Tensorflow coming to JavaScript this year.

Collapse
ben profile image
Ben Halpern Author

Great to hear you had success on spam filtering. That's a notoriously difficult problem without tech like this, but even with neural networks, I don't hear a lot of practical "this worked and how" examples.

If you wanted to write a DEV post on that experience, I think it would go over really well.

Collapse
johnkazer profile image
John Kazer

I built a sentence matching system using this library. It matched the words and characters in an input sentence against a database to make a best guess match. The standardised sentence version in the database was linked to properties we used for processing a response.
The response was a set of calculations rather than a chat app.
The library was useful in 3 ways:

  1. We could claim to use AI in the app πŸ˜€.
  2. Once trained (it runs in node but could have done it in browser) you save the settings for fast and repeated use. I think, but never tested this, that it is faster than a traditional brute force best match search.
  3. You don't really need to know much about neural networks to set it up. Getting your training and testing data organised is by far the hardest part.
Collapse
masihfathi profile image
masih fathi • Edited

does the speed of computation comparable with c tensorflow library?

Collapse
avalander profile image
Avalander

Wow, looks great, thanks for sharing! I'll see if I can try it out soon!

Collapse
daijapan profile image
Daisuke Ishii

Love this. Congrats!

Collapse
_bigblind profile image
Frederik πŸ‘¨β€πŸ’»βž‘οΈπŸŒ Creemers

If you're running in NodeJS anyway, I'd expect you're better of with some kind of wrapper around native TensorFlow bindings than running a neural network in pure JS.

Forem Open with the Forem app