You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](#backers)[](#sponsors)
[](#backers)[](#sponsors)
11
+
## About
13
12
14
13
`brain.js` is a library of [Neural Networks](http://en.wikipedia.org/wiki/Artificial_neural_network) written in JavaScript.
15
14
16
15
:bulb:**Note**: This is a continuation of the [**harthur/brain**](https://github.com/harthur/brain) repository (which is not maintained anymore). For more details, check out [this issue](https://github.com/harthur/brain/issues/72).
17
16
17
+
## Table of Contents
18
+
18
19
-[Examples](#examples)
19
20
+[More Examples](#more-examples)
20
21
-[Usage](#usage)
@@ -45,57 +46,60 @@
45
46
46
47
# Examples
47
48
Here's an example showcasing how to approximate the XOR function using `brain.js`:
48
-
More info on config [here](https://github.com/BrainJS/brain.js/blob/develop/src/neural-network.js#L31).
49
+
more info on config [here](https://github.com/BrainJS/brain.js/blob/develop/src/neural-network.js#L31).
// create a simple feed forward neural network with backpropagation
60
+
constnet=newbrain.NeuralNetwork(config);
59
61
60
62
net.train([{input: [0, 0], output: [0]},
61
63
{input: [0, 1], output: [1]},
62
64
{input: [1, 0], output: [1]},
63
65
{input: [1, 1], output: [0]}]);
64
66
65
-
var output =net.run([1, 0]); // [0.987]
67
+
constoutput=net.run([1, 0]); // [0.987]
66
68
```
67
69
or
68
-
More info on config [here](https://github.com/BrainJS/brain.js/blob/develop/src/recurrent/rnn.js#L726).
70
+
more info on config [here](https://github.com/BrainJS/brain.js/blob/develop/src/recurrent/rnn.js#L726).
71
+
69
72
```javascript
70
-
//provide optional config object, defaults shown.
71
-
var config = {
73
+
//provide optional config object, defaults shown.
74
+
constconfig= {
72
75
inputSize:20,
73
76
inputRange:20,
74
-
hiddenSizes:[20,20],
77
+
hiddenSizes:[20,20],
75
78
outputSize:20,
76
79
learningRate:0.01,
77
80
decayRate:0.999,
78
-
}
79
-
//create a simple recurrent neural network
80
-
var net =newbrain.recurrent.RNN(config);
81
+
};
82
+
83
+
// create a simple recurrent neural network
84
+
constnet=newbrain.recurrent.RNN(config);
81
85
82
86
net.train([{input: [0, 0], output: [0]},
83
87
{input: [0, 1], output: [1]},
84
88
{input: [1, 0], output: [1]},
85
89
{input: [1, 1], output: [0]}]);
86
90
87
-
var output =net.run([0, 0]); // [0]
88
-
output =net.run([0, 1]); // [1]
89
-
output =net.run([1, 0]); // [1]
90
-
output =net.run([1, 1]); // [0]
91
+
let output =net.run([0, 0]); // [0]
92
+
output =net.run([0, 1]); // [1]
93
+
output =net.run([1, 0]); // [1]
94
+
output =net.run([1, 1]); // [0]
91
95
```
92
96
93
97
However, there is no reason to use a neural network to figure out XOR. (-: So, here is a more involved, realistic example:
94
98
[Demo: training a neural network to recognize color contrast](https://brain.js.org/).
95
99
96
100
## More Examples
97
-
You check out this fantastic screencast, which explains how to train a simple neural network using a real world dataset: [How to create a neural network in the browser using Brain.js](https://scrimba.com/c/c36zkcb).
98
-
*[writing a children's book using a recurrent neural neural network](./examples/childrens-book.js)
101
+
You can check out this fantastic screencast, which explains how to train a simple neural network using a real world dataset: [How to create a neural network in the browser using Brain.js](https://scrimba.com/c/c36zkcb).
102
+
*[writing a children's book using a recurrent neural network](./examples/childrens-book.js)
99
103
*[simple letter detection](./examples/which-letter-simple.js)
100
104
101
105
# Usage
@@ -112,12 +116,7 @@ Or if you prefer yarn:
112
116
yarn add brain.js
113
117
```
114
118
115
-
Alternatively, you can install `brain.js` with [bower](https://bower.io/):
116
-
```
117
-
bower install brain.js
118
-
```
119
-
120
-
At present, the npm version of brain.js is approximately 1.0.0, featuring only Feed forward NN. All other models are beta and are being jazzed up and battle hardened.
119
+
At present, the published version of brain.js is approximately 1.0.0, featuring only Feed-forward NN. All other models are beta and are being jazzed up and battle hardened.
121
120
You can still download the latest, though. They are cool!
122
121
123
122
### Browser
@@ -233,7 +232,7 @@ net.train(data, {
233
232
234
233
The network will stop training whenever one of the two criteria is met: the training error has gone below the threshold (default `0.005`), or the max number of iterations (default `20000`) has been reached.
235
234
236
-
By default training will not 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.
235
+
By default training will not let you know how it's 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.
237
236
238
237
The 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 training results may be constrained to a local minimum and perform badly on new data.(_Overfitting_) The default learning rate is `0.3`.
239
238
@@ -258,17 +257,17 @@ A boolean property called `invalidTrainOptsShouldThrow` is set to `true` by defa
258
257
With multiple networks you can train in parallel like this:
259
258
260
259
```javascript
261
-
var net =newbrain.NeuralNetwork();
262
-
var net2 =newbrain.NeuralNetwork();
260
+
constnet=newbrain.NeuralNetwork();
261
+
constnet2=newbrain.NeuralNetwork();
263
262
264
-
var p1 =net.trainAsync(data, options);
265
-
var p2 =net2.trainAsync(data, options);
263
+
constp1=net.trainAsync(data, options);
264
+
constp2=net2.trainAsync(data, options);
266
265
267
266
Promise
268
267
.all([p1, p2])
269
268
.then(values=> {
270
-
var res = values[0];
271
-
var res2 = values[1];
269
+
constres= values[0];
270
+
constres2= values[1];
272
271
console.log(`net trained in ${res.iterations} and net2 trained in ${res2.iterations}`);
273
272
// do something super cool with my 2 trained networks
274
273
})
@@ -295,23 +294,23 @@ If the training error is still something huge like `0.4` after 20000 iterations,
295
294
Serialize or load in the state of a trained network with JSON:
296
295
297
296
```javascript
298
-
var json =net.toJSON();
297
+
constjson=net.toJSON();
299
298
net.fromJSON(json);
300
299
```
301
300
302
301
You can also get a custom standalone function from a trained network that acts just like `run()`:
303
302
304
303
```javascript
305
-
var run =net.toFunction();
306
-
var output =run({ r:1, g:0.4, b:0 });
304
+
construn=net.toFunction();
305
+
constoutput=run({ r:1, g:0.4, b:0 });
307
306
console.log(run.toString()); // copy and paste! no need to import brain.js
308
307
```
309
308
310
309
# Options
311
310
`NeuralNetwork()` takes a hash of options:
312
311
313
312
```javascript
314
-
var net =newbrain.NeuralNetwork({
313
+
constnet=newbrain.NeuralNetwork({
315
314
activation:'sigmoid', // activation function
316
315
hiddenLayers: [4],
317
316
learningRate:0.6// global learning rate, useful when training using streams
@@ -326,7 +325,7 @@ This parameter lets you specify which activation function your neural network sh
Here's a table (Thanks, Wikipedia!) summarizing a plethora of activation functions — [Activation Function](https://www.wikiwand.com/en/Activation_function)
328
+
Here's a table (thanks, Wikipedia!) summarizing a plethora of activation functions — [Activation Function](https://www.wikiwand.com/en/Activation_function)
330
329
331
330
### hiddenLayers
332
331
You can use this to specify 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:
@@ -362,11 +361,14 @@ To train the network using a stream you must first create the stream by calling
362
361
Use 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.
363
362
364
363
# Utilities
364
+
365
365
### `likely`
366
+
366
367
```js
367
-
var likely =require('brain/likely');
368
-
var key =likely(input, net);
368
+
constlikely=require('brain/likely');
369
+
constkey=likely(input, net);
369
370
```
371
+
370
372
Likely example see: [simple letter detection](./examples/which-letter-simple.js)
371
373
372
374
# Neural Network Types
@@ -385,7 +387,9 @@ Different neural nets do different things well. For example:
385
387
* A Recurrent Neural Network _remembers_, and has a finite set of results.
386
388
387
389
# Get Involved!
390
+
388
391
### Issues
392
+
389
393
If you have an issue, either a bug or a feature you think would benefit your project let us know and we will do our best.
390
394
391
395
Create issues [here](https://github.com/BrainJS/brain.js/issues) and follow the template.
@@ -417,5 +421,3 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
0 commit comments