|
| 1 | +# BrainJS |
| 2 | +BrainJS is a neural network library for browser JS & NodeJS, for generic code examples see here, for actual explanations see the official documentation. |
| 3 | + |
| 4 | +## Setup |
| 5 | +<details> |
| 6 | +<summary>Setup</summary> |
| 7 | + |
| 8 | +NodeJS: npm install brainjs |
| 9 | +```javascript |
| 10 | + const brain = require("brain"); |
| 11 | +``` |
| 12 | +browser: |
| 13 | +```html |
| 14 | + <script src="//unpkg.com/brain.js"></script> |
| 15 | +``` |
| 16 | +</details> |
| 17 | + |
| 18 | +## Minimal Example |
| 19 | +<details> |
| 20 | +<summary>code</summary> |
| 21 | + |
| 22 | +```javascript |
| 23 | + const net = new brain.NeuralNetwork(); |
| 24 | + const training = net.train([ |
| 25 | + { input: [0, 1], output: [1] }, |
| 26 | + { input: [0, 0], output: [0] }, |
| 27 | + { input: [1, 1], output: [0] }, |
| 28 | + { input: [1, 0], output: [1] }]); |
| 29 | + net.run([1, 1]); // expect 0 |
| 30 | +``` |
| 31 | +</details> |
| 32 | + |
| 33 | +## [Neural Network Types](https://github.com/BrainJS/brain.js#neural-network-types) |
| 34 | +<details> |
| 35 | +<summary>code</summary> |
| 36 | + |
| 37 | +```javascript |
| 38 | + const net = new brain.NeuralNetwork(); |
| 39 | + // const net = new brain.recurrent.LSTMTimeStep(); |
| 40 | + // const net = new brain.recurrent.LSTMTimeStep({ inputSize: 2, hiddenLayers: [10], outputSize: 2,}); |
| 41 | + // const net = new brain.recurrent.LSTM(); |
| 42 | + // const net = new brain.recurrent.LSTM(); |
| 43 | + // const net = new brain.recurrent.RNN(); |
| 44 | + // const net = new brain.NeuralNetworkGPU(); |
| 45 | + // const net = new brain.NeuralNetworkGRU(); |
| 46 | +``` |
| 47 | +</details> |
| 48 | + |
| 49 | +## [Training](https://github.com/BrainJS/brain.js#training) |
| 50 | +<details> |
| 51 | +<summary>code</summary> |
| 52 | + |
| 53 | +```javascript |
| 54 | + net.train([ |
| 55 | + { input: [0, 1], output: [1] }, |
| 56 | + { input: [0, 0], output: [0] }, |
| 57 | + { input: [1, 1], output: [0] }, |
| 58 | + { input: [1, 0], output: [1] }, |
| 59 | + { input: [0, 1], output: [1] }, |
| 60 | + { input: [0, 0], output: [0] }, |
| 61 | + { input: [1, 1], output: [0] }, |
| 62 | + { input: [1, 0], output: [1] }]); |
| 63 | + |
| 64 | + net.train([ |
| 65 | + { input: 'I feel great about the world!', output: 'happy' }, |
| 66 | + { input: 'The world is a terrible place!', output: 'sad' }, |
| 67 | + ]); |
| 68 | +``` |
| 69 | +</details> |
| 70 | + |
| 71 | + |
| 72 | +### [Training Options](https://github.com/BrainJS/brain.js#training-options) |
| 73 | +<details> |
| 74 | +<summary>code</summary> |
| 75 | + |
| 76 | +```javascript |
| 77 | + net.train(data, { |
| 78 | + // Defaults values --> expected validation |
| 79 | + iterations: 20000, // the maximum times to iterate the training data --> number greater than 0 |
| 80 | + errorThresh: 0.005, // the acceptable error percentage from training data --> number between 0 and 1 |
| 81 | + log: false, // true to use console.log, when a function is supplied it is used --> Either true or a function |
| 82 | + logPeriod: 10, // iterations between logging out --> number greater than 0 |
| 83 | + learningRate: 0.3, // scales with delta to effect training rate --> number between 0 and 1 |
| 84 | + momentum: 0.1, // scales with next layer's change value --> number between 0 and 1 |
| 85 | + callback: null, // a periodic call back that can be triggered while training --> null or function |
| 86 | + callbackPeriod: 10, // the number of iterations through the training data between callback calls --> number greater than 0 |
| 87 | + timeout: Infinity, // the max number of milliseconds to train for --> number greater than 0 |
| 88 | + }); |
| 89 | +``` |
| 90 | +</details> |
| 91 | + |
| 92 | +### [Asynchronus Training](https://github.com/BrainJS/brain.js#async-training) |
| 93 | +<details> |
| 94 | +<summary>code</summary> |
| 95 | + |
| 96 | +```javascript |
| 97 | + const net = new brain.NeuralNetwork(); |
| 98 | + const net2 = new brain.NeuralNetwork(); |
| 99 | + |
| 100 | + const p1 = net.trainAsync(data, options); |
| 101 | + const p2 = net2.trainAsync(data, options); |
| 102 | + |
| 103 | + Promise.all([p1, p2]) |
| 104 | + .then((values) => { |
| 105 | + const res = values[0]; |
| 106 | + const res2 = values[1]; |
| 107 | + console.log( |
| 108 | + `net trained in ${res.iterations} and net2 trained in ${res2.iterations}` |
| 109 | + ); |
| 110 | + // do something super cool with my 2 trained networks |
| 111 | + }) |
| 112 | + .catch(handleError); |
| 113 | + |
| 114 | + const crossValidate = new brain.CrossValidate( |
| 115 | + brain.NeuralNetwork, |
| 116 | + networkOptions |
| 117 | + ); |
| 118 | +``` |
| 119 | +</details> |
| 120 | + |
| 121 | +### [Cross Validation](https://github.com/BrainJS/brain.js#cross-validation) |
| 122 | +<details> |
| 123 | +<summary>code</summary> |
| 124 | + |
| 125 | +```javascript |
| 126 | + crossValidate.train(data, trainingOptions, k); //note k (or KFolds) is optional |
| 127 | + const json = crossValidate.toJSON(); // all stats in json as well as neural networks |
| 128 | + const net = crossValidate.toNeuralNetwork(); // get top performing net out of `crossValidate` |
| 129 | + |
| 130 | + // optionally later |
| 131 | + const json = crossValidate.toJSON(); |
| 132 | + const net = crossValidate.fromJSON(json); |
| 133 | +``` |
| 134 | +</details> |
| 135 | + |
| 136 | +### [NodeJS Streams](https://github.com/BrainJS/brain.js#train-stream) |
| 137 | +<details> |
| 138 | +<summary>code</summary> |
| 139 | + |
| 140 | +```javascript |
| 141 | + const net = new brain.NeuralNetwork(); |
| 142 | + const trainStream = new brain.TrainStream({ |
| 143 | + neuralNetwork: net, |
| 144 | + floodCallback: function () { |
| 145 | + flood(trainStream, data); |
| 146 | + }, |
| 147 | + doneTrainingCallback: function (stats) { |
| 148 | + // network is done training! What next? |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + // kick it off |
| 153 | + readInputs(trainStream, data); |
| 154 | + |
| 155 | + function readInputs(stream, data) { |
| 156 | + for (let i = 0; i < data.length; i++) { |
| 157 | + stream.write(data[i]); |
| 158 | + } |
| 159 | + // let it know we've reached the end of the inputs |
| 160 | + stream.endInputs(); |
| 161 | + } |
| 162 | +``` |
| 163 | +</details> |
| 164 | + |
| 165 | +### [Forecast](https://github.com/BrainJS/brain.js#forecastinput-count---predictions) |
| 166 | +<details> |
| 167 | +<summary>code</summary> |
| 168 | + |
| 169 | +```javascript |
| 170 | + const net = new brain.LSTMTimeStep(); |
| 171 | + net.fromJSON(json); |
| 172 | + net.forecast(input, 3); |
| 173 | + |
| 174 | + const run = net.toFunction(); |
| 175 | + const output = run({ r: 1, g: 0.4, b: 0 }); |
| 176 | + console.log(run.toString()); // copy and paste! no need to import brain.js |
| 177 | +``` |
| 178 | +</details> |
| 179 | + |
| 180 | +### [Standalone Function](https://github.com/BrainJS/brain.js#standalone-function) |
| 181 | +<details> |
| 182 | +<summary>code</summary> |
| 183 | + |
| 184 | +```javascript |
| 185 | + const run = net.toFunction(); |
| 186 | + const output = run({ r: 1, g: 0.4, b: 0 }); |
| 187 | + console.log(run.toString()); // copy and paste! no need to import brain.js |
| 188 | +``` |
| 189 | +</details> |
| 190 | + |
| 191 | +### [Activation Function](https://github.com/BrainJS/brain.js#activation) |
| 192 | +<details> |
| 193 | +<summary>code</summary> |
| 194 | + |
| 195 | +```javascript |
| 196 | + const net = new brain.NeuralNetwork({ |
| 197 | + activation: 'sigmoid', /// relu, leaky-relu, tanh |
| 198 | + hiddenLayers: [4], |
| 199 | + learningRate: 0.6, // global learning rate, useful when training using streams |
| 200 | + }); |
| 201 | +``` |
| 202 | +</details> |
| 203 | + |
| 204 | +### [Likely](https://github.com/BrainJS/brain.js#activation) |
| 205 | +<details> |
| 206 | +<summary>code</summary> |
| 207 | + |
| 208 | +```javascript |
| 209 | + const likely = require('brain/likely'); |
| 210 | + const key = likely(input, net); |
| 211 | +``` |
| 212 | +</details> |
| 213 | + |
| 214 | +## [toSVG](https://github.com/BrainJS/brain.js#tosvg) |
| 215 | + |
0 commit comments