JavaScript神經網絡:Brain

jopen 9年前發布 | 33K 次閱讀 Brain 神經網絡

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]</pre>

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.baiduhome.net/lib/view/home/1436520926911

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!