JavaScript神经网络:Brain

jopen 9年前

Brain 是一个 JavaScript 神经网络库。下面是一个用它来实现近似 XOR 功能的例子:

var net = new brain.NeuralNetwork();     net.train([{input: [0, 0], output: [0]},             {input: [0, 1], output: [1]},             {input: [1, 0], output: [1]},             {input: [1, 1], output: [0]}]);                var output = net.run([1, 0]);  // [0.987]

There's no reason to use a neural network to figure out XOR however (-: so here's a more involved, realistic example: Demo: training a neural network to recognize color contrast

Using in node

If you have node you can install with npm:

npm install brain

Using in the browser

Download the latest brain.js. Training is computationally expensive, so you should try to train the network offline (or on a Worker) and use thetoFunction()ortoJSON()options to plug the pre-trained network in to your website.

Training

Usetrain()to train the network with an array of training data. The network has to be trained with all the data in bulk in one call totrain(). The more training patterns, the longer it will probably take to train, but the better the network will be at classifiying new patterns.

Data format

Each training pattern should have aninputand anoutput, both of which can be either an array of numbers from0to1or a hash of numbers from0to1. For the color constrast demo it looks something like this:

var net = new brain.NeuralNetwork();    net.train([{input: { r: 0.03, g: 0.7, b: 0.5 }, output: { black: 1 }},             {input: { r: 0.16, g: 0.09, b: 0.2 }, output: { white: 1 }},             {input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 }}]);    var output = net.run({ r: 1, g: 0.4, b: 0 });  // { white: 0.99, black: 0.002 }

项目主页:http://www.open-open.com/lib/view/home/1436520926911