23 6 / 2010
Brain.js: Neural Networks In JavaScript
A neural network is “a mathematical model or computational model that tries to simulate the structure and/or functional aspects of biological neural networks” according to Wikipedia. Basically, a neural network carries out pattern recognition, very much like the human brain would. The user of the network trains it, and then it can be used with varying levels of accuracy depending on the amount of training that it has received. In this way, neural networks are learn patterns and can then recognize them on different input data. If you want to learn more about neural networks, here is a good introduction.
Heather Arthur has written an implementation of a neural network in JavaScript. Below is an example of the library in use:
var net = new NeuralNetwork();
net.train([{input: {r:1, g:0.65, b:0}, output: {orange: 1}},
{input: {r:0, g:0.54, b:0}, output: {green: 1}},
{input: {r:0.6, g:1, b:0.5}, output: {green: 1}},
{input: {r:0.67, g:0, b:1}, output: {purple: 1}}]);
var output = net.run({r:1, g:1, b:0});
This example trains the neural network with some simple patterns of input and output, then runs the neural network with an input. The output would be something like:
{"orange":0.96,"green":0.18,"purple":0.00}
because yellow is most similar to orange.
Heather has also provided a working demo that you can try out. First you train the network to recognize whether to use black or white text over a given background color. Then, it compares the output of the neural network to a luminosity algorithm that calculates the same thing. In many cases, the neural network produces results that are better than the luminosity algorithm. Calculation all happens in a WebWorker, so you will need a modern web browser (no IE, sorry!).
You can check out the code on Github, the documentation here, and the color demo here. Awesome work!