UNPKG

6.11 kBMarkdownView Raw
1# brain
2
3### NOTE: This is a fork of the brain package.
4
5`brain` is a JavaScript [neural network](http://en.wikipedia.org/wiki/Artificial_neural_network) library. Here's an example of using it to approximate the XOR function:
6
7```javascript
8var net = new brain.NeuralNetwork();
9
10net.train([{input: [0, 0], output: [0]},
11 {input: [0, 1], output: [1]},
12 {input: [1, 0], output: [1]},
13 {input: [1, 1], output: [0]}]);
14
15var output = net.run([1, 0]); // [0.987]
16```
17
18There's no reason to use a neural network to figure out XOR however (-: so here's a more involved, realistic example:
19[Demo: training a neural network to recognize color contrast](http://harthur.github.com/brain/)
20
21## Using in node
22If you have [node](http://nodejs.org/) you can install with [npm](http://npmjs.org):
23
24```
25npm install brain
26```
27
28## Using in the browser
29Download the latest [brain.js](https://github.com/harthur/brain/tree/gh-pages). Training is computationally expensive, so you should try to train the network offline (or on a Worker) and use the `toFunction()` or `toJSON()` options to plug the pre-trained network in to your website.
30
31## Training
32Use `train()` 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 to `train()`. The more training patterns, the longer it will probably take to train, but the better the network will be at classifiying new patterns.
33
34#### Data format
35Each training pattern should have an `input` and an `output`, both of which can be either an array of numbers from `0` to `1` or a hash of numbers from `0` to `1`. For the [color constrast demo](http://harthur.github.com/brain/) it looks something like this:
36
37```javascript
38var net = new brain.NeuralNetwork();
39
40net.train([{input: { r: 0.03, g: 0.7, b: 0.5 }, output: { black: 1 }},
41 {input: { r: 0.16, g: 0.09, b: 0.2 }, output: { white: 1 }},
42 {input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 }}]);
43
44var output = net.run({ r: 1, g: 0.4, b: 0 }); // { white: 0.99, black: 0.002 }
45```
46
47#### Options
48`train()` takes a hash of options as its second argument:
49
50```javascript
51net.train(data, {
52 errorThresh: 0.005, // error threshold to reach
53 iterations: 20000, // maximum training iterations
54 log: true, // console.log() progress periodically
55 logPeriod: 10, // number of iterations between logging
56 learningRate: 0.3 // learning rate
57})
58```
59
60The network will train until the training error has gone below the threshold (default `0.005`) or the max number of iterations (default `20000`) has been reached, whichever comes first.
61
62By default training won't let you know how its doing until the end, but set `log` to `true` to get periodic updates on the current training error of the network. The training error should decrease every time. The updates will be printed to console. If you set `log` to a function, this function will be called with the updates instead of printing to the console.
63
64The learning rate is a parameter that influences how quickly the network trains. It's a number from `0` to `1`. If the learning rate is close to `0` it will take longer to train. If the learning rate is closer to `1` it will train faster but it's in danger of training to a local minimum and performing badly on new data. The default learning rate is `0.3`.
65
66#### Output
67The output of `train()` is a hash of information about how the training went:
68
69```javascript
70{
71 error: 0.0039139985510105032, // training error
72 iterations: 406 // training iterations
73}
74```
75
76#### Failing
77If the network failed to train, the error will be above the error threshold. This could happen because the training data is too noisy (most likely), the network doesn't have enough hidden layers or nodes to handle the complexity of the data, or it hasn't trained for enough iterations.
78
79If the training error is still something huge like `0.4` after 20000 iterations, it's a good sign that the network can't make sense of the data you're giving it.
80
81## JSON
82Serialize or load in the state of a trained network with JSON:
83
84```javascript
85var json = net.toJSON();
86
87net.fromJSON(json);
88```
89
90You can also get a custom standalone function from a trained network that acts just like `run()`:
91
92```javascript
93var run = net.toFunction();
94
95var output = run({ r: 1, g: 0.4, b: 0 });
96
97console.log(run.toString()); // copy and paste! no need to import brain.js
98```
99
100## Options
101`NeuralNetwork()` takes a hash of options:
102
103```javascript
104var net = new brain.NeuralNetwork({
105 hiddenLayers: [4],
106 learningRate: 0.6 // global learning rate, useful when training using streams
107});
108```
109
110#### hiddenLayers
111Specify the number of hidden layers in the network and the size of each layer. For example, if you want two hidden layers - the first with 3 nodes and the second with 4 nodes, you'd give:
112
113```
114hiddenLayers: [3, 4]
115```
116
117By default `brain` uses one hidden layer with size proportionate to the size of the input array.
118
119## Streams
120The network now has a [WriteStream](http://nodejs.org/api/stream.html#stream_class_stream_writable). You can train the network by using `pipe()` to send the training data to the network.
121
122#### Example
123Refer to `stream-example.js` for an example on how to train the network with a stream.
124
125#### Initialization
126To train the network using a stream you must first create the stream by calling `net.createTrainStream()` which takes the following options:
127
128* `floodCallback()` - the callback function to re-populate the stream. This gets called on every training iteration.
129* `doneTrainingCallback(info)` - the callback function to execute when the network is done training. The `info` param will contain a hash of information about how the training went:
130
131```javascript
132{
133 error: 0.0039139985510105032, // training error
134 iterations: 406 // training iterations
135}
136```
137
138#### Transform
139Use a [Transform](http://nodejs.org/api/stream.html#stream_class_stream_transform) to coerce the data into the correct format. You might also use a Transform stream to normalize your data on the fly.