Skip to content

Commit b2bec1e

Browse files
Merge pull request #275 from BrainJS/cleanup-api
Cleanup api for TrainStream, CrossValidate, and Typescript
2 parents d9b853e + eebe6c8 commit b2bec1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+17821
-16502
lines changed

README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
+ [For training with `RNN`, `LSTM` and `GRU`](#for-training-with-rnn-lstm-and-gru)
2929
+ [Training Options](#training-options)
3030
+ [Async Training](#async-training)
31+
+ [Cross Validation](#cross-validation)
32+
+ [Train Stream](#train-stream)
3133
- [Methods](#methods)
3234
+ [train](#train)
3335
- [Failing](#failing)
@@ -274,6 +276,49 @@ With multiple networks you can train in parallel like this:
274276
.catch(handleError);
275277
```
276278

279+
### Cross Validation
280+
[Cross Validation](https://en.wikipedia.org/wiki/Cross-validation_(statistics)) can provide a less fragile way of training on larger data sets. The brain.js api provides Cross Validation in this example:
281+
```js
282+
const crossValidate = new CrossValidate(brain.NeuralNetwork);
283+
const stats = crossValidate.train(data, networkOptions, trainingOptions, k); //note k (or KFolds) is optional
284+
const net = crossValidate.toNetwork();
285+
286+
287+
// optionally later
288+
const json = crossValidate.toJSON();
289+
const net = crossValidate.fromJSON(json);
290+
```
291+
292+
An example of using cross validate can be found in [examples/cross-validate.js](examples/cross-validate.js)
293+
294+
### Train Stream
295+
Streams are a very powerful tool in node for massive data spread across processes and are provided via the brain.js api in the following way:
296+
```js
297+
const net = new brain.NeuralNetwork();
298+
const trainStream = new brain.TrainStream({
299+
neuralNetwork: net,
300+
floodCallback: function() {
301+
flood(trainStream, data);
302+
},
303+
doneTrainingCallback: function(stats) {
304+
// network is done training! What next?
305+
}
306+
});
307+
308+
// kick it off
309+
readInputs(trainStream, data);
310+
311+
function readInputs(stream, data) {
312+
for (let i = 0; i < data.length; i++) {
313+
stream.write(data[i]);
314+
}
315+
// let it know we've reached the end of the inputs
316+
stream.endInputs();
317+
}
318+
```
319+
320+
An example of using train stream can be found in [examples/stream-example.js](examples/stream-example.js)
321+
277322
# Methods
278323
### train
279324
The output of `train()` is a hash of information about how the training went:
@@ -341,7 +386,7 @@ The network now has a [WriteStream](http://nodejs.org/api/stream.html#stream_cla
341386

342387

343388
### Example
344-
Refer to [`stream-example.js`](./examples/cli/stream-example.js) for an example on how to train the network with a stream.
389+
Refer to [`stream-example.js`](examples/stream-example.js) for an example on how to train the network with a stream.
345390

346391

347392
### Initialization

0 commit comments

Comments
 (0)