Javascript Neural Networks

There are tons of neural network implementations out there, but not many in JavaScript. This is pretty surprising given that JavaScript is awesome and neural networks could really benefit from being in the browser. One partial implementation was used to do some sweet Captcha OCR, and my last post was about using them to determine whether to display black or white text over a given background color.

I ended up creating brain, the missing JavaScript neural network library. I tried to make it easy to use. To use it you don’t have to know what a hidden layer is (but you can specify hidden layers if you want), you can also specify input (and expected output, for training) as hashes instead of arrays – good for sparse or labelled input, and you can pass trained networks around in JSON, which is useful with Worker threads.

If you want to find out more using neural networks from a programmatic perspective, this is a good introduction that just popped up.

16 thoughts on “Javascript Neural Networks

  1. Hello!

    My name is Brian McMahan and I am a cognitive science major with a focus on computer science. I have been recently thinking about neural networks using JSON as a communciation protocol between nodes. I have many more ideas but don’t like putting them out there for public access.

    I am looking for someone to partner up on this. Let me know if this would interest you.

      1. I’m not sure how useful it will be for what you guys are discussing, but you may want to check out the project, since it implements a actor-based message passing architecture for node.js processes using dnode’s IPC protocol.

  2. Thank you! We’re using this currently on a project and it works really well. It’s really easy to use and well-written – saved me loads of time – just a shame more people don’t know about neural nets.

  3. Would a recurrent neural network also be possible with this code?
    I mean like interconnecting several brain.NeuralNetworks.

  4. Hi,
    My name is Rodrigo, I’m a student of the Universidade Federal do Rio Grande do Norte, Brazil. ( – My blog, I’ve started to talk about NNs on it). I think that your idea is wonderfull. Using neural network on browsers can make the experience of using internet much more atractive and rich.
    Congratulations.I’m dying to write something about “brain”, as soon as I test it. And, who know, give some contribution to it.

  5. Heather, I’ll be honest I still don’t understand how to take a NN and configure it so I’ll get a good output, and I’ve been trying. I got it to work on a training set of naming 64 colors, but when I upped it to 200 it failed dismally.

    I’m also working on cleaning up the shit-code that was the Mentifex project. I’m hoping a clean symbolic consciousness module would have more than a novelty applicability. (see website link)

    1. Is this still a problem? A problem like naming colors has the potential to be a tough problem for a neural network to solve if the data isn’t conclusive enough. You can try messing with the options a bit (like learningRate or the number of nodes).

    2. Mentifex (ATM) here. Daniel is referring to the obsolete JavaScript AI Mind code at the SourceForge “mind” project, which was hijacked away from me about two years ago. (People e-mailed me to ask if I objected to a “mind” project at Google Code, which I did not, but somehow they also convinced SourceForge to give them my “mind” project.) I was and am the sole developer (since anno 2000) of that SourceForge JavaScript “Mind” program, and it has remained alive and well at the website. I this spring and summer of 2011 I have worked extremely productively on further developing the AiMind in both JavaScript and Win32Forth. So I thank Daniel for putting time and energy into the JavaScript AI programming, and I assure Heather Arthur and visitors to this excellent weblog that Mentifex here is still making great strides forward on JavaScript quasi-neural-net AI. -Arthur

  6. I’m thinking of using brain.js with node.js for a website I’m doing. A high number of individual Bayes classifiers and neural networks will be needed, and because I need to query large large amounts of data efficiently, I’m looking to provide persistence with a traditional database.

    I see that there is support for JSON in the neural network, but the Bayes classifier seems to have no way of getting at the data. I assume that the amount of data for the neural network is limited to information about the individual neurons, and is thus relatively small, and fast to serialize.

    Conversely, I assume that, since the Bayes classifier potentially needs to keep a database of thousands of words, it would be poor design to store this information in JavaScript structures, and serializing it to JSON would just be a bad, bad idea.

    I’m no expert on node.js, or on reading other people’s code, but I assume that what your library does is to leave the heavy data queries to the persistence back-end?

    My question is: How can a new back-end, or a callback API, be added to brain.js, in order to make it usable with any object store? I imagine that there is a narrow, fixed set of queries one would need to implement: “Store/fetch frequency/count for word X from data set Y.” should about cover it, right?

    1. So I’ve been examining the source code. I’m unfamiliar with a lot of this syntax, because of the heavy use of underscore.js, but it seems that all I would have to do is write a new back-end, and patch the constructor to recognize it. What I don’t understand, though, is why you don’t just accept back-end instances as an argument to the constructor, instead of that hard-coded switch statement?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s