Skip to content

Commit 865c7c9

Browse files
Merge pull request #193 from BrainJS/192-time-series-baseline
Add Time Step Recurrent Neural Network
2 parents 1d643a3 + eae7e22 commit 865c7c9

25 files changed

+900
-31
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [Training](#training)
2222
+ [Data format](#data-format)
2323
+ [For training with NeuralNetwork](#for-training-with-neuralnetwork)
24+
+ [For training with `RNNTimeStep`, `LSTMTimeStep` and `GRUTimeStep`](#for-training-with-rnntimestep-lstmtimestep-and-gputimestep)
2425
+ [For training with `RNN`, `LSTM` and `GRU`](#for-training-with-rnn-lstm-and-gpu)
2526
+ [Training Options](#training-options)
2627
+ [Async Training](#async-training)
@@ -128,6 +129,38 @@ net.train([{input: { r: 0.03, g: 0.7 }, output: { black: 1 }},
128129

129130
var output = net.run({ r: 1, g: 0.4, b: 0 }); // { white: 0.81, black: 0.18 }
130131
```
132+
133+
#### For training with `RNNTimeStep`, `LSTMTimeStep` and `GRUTimeStep`
134+
Eeach training pattern can either:
135+
* Be an array of numbers
136+
* Be an array of arrays of numbers
137+
138+
Example using an array of numbers:
139+
```javascript
140+
var net = new brain.recurrent.LSTMTimeStep();
141+
142+
net.train([
143+
1,
144+
2,
145+
3,
146+
]);
147+
148+
var output = net.run([1, 2]); // 3
149+
```
150+
151+
Example using an array of arrays of numbers:
152+
```javascript
153+
var net = new brain.recurrent.LSTMTimeStep();
154+
155+
net.train([
156+
[1, 3],
157+
[2, 2],
158+
[3, 1],
159+
]);
160+
161+
var output = net.run([[1, 3], [2, 2]]); // [3, 1]
162+
```
163+
131164
#### For training with `RNN`, `LSTM` and `GRU`
132165
Each training pattern can either:
133166
* Be an array of values
@@ -321,13 +354,17 @@ Likely example see: [simple letter detection](./examples/which-letter-simple.js)
321354

322355
# Neural Network Types
323356
* [`brain.NeuralNetwork`](src/neural-network.js) - [Feedforward Neural Network](https://en.wikipedia.org/wiki/Feedforward_neural_network) with backpropagation
357+
* [`brain.recurrent.RNNTimeStep`](src/recurrent/rnn-time-step.js) - [Time Step Recurrent Neural Network or "RNN"](https://en.wikipedia.org/wiki/Recurrent_neural_network)
358+
* [`brain.recurrent.LSTMTimeStep`](src/recurrent/lstm-time-step.js) - [Time Step Long Short Term Memory Neural Network or "LSTM"](https://en.wikipedia.org/wiki/Long_short-term_memory)
359+
* [`brain.recurrent.GRUTimeStep`](src/recurrent/gru-time-step.js) - [Time Step Gated Recurrent Unit or "GRU"](https://en.wikipedia.org/wiki/Gated_recurrent_unit)
324360
* [`brain.recurrent.RNN`](src/recurrent/rnn.js) - [Recurrent Neural Network or "RNN"](https://en.wikipedia.org/wiki/Recurrent_neural_network)
325361
* [`brain.recurrent.LSTM`](src/recurrent/lstm.js) - [Long Short Term Memory Neural Network or "LSTM"](https://en.wikipedia.org/wiki/Long_short-term_memory)
326362
* [`brain.recurrent.GRU`](src/recurrent/gru.js) - [Gated Recurrent Unit or "GRU"](https://en.wikipedia.org/wiki/Gated_recurrent_unit)
327363

328364
### Why different Neural Network Types?
329365
Different neural nets do different things well. For example:
330366
* A Feedforward Neural Network can classify simple things very well, but it has no memory of previous actions and has infinite variation of results.
367+
* A Time Step Recurrent Neural Network _remembers_, and can predict future values.
331368
* A Recurrent Neural Network _remembers_, and has a finite set of results.
332369

333370
# Get Involved!

browser.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* license: MIT (http://opensource.org/licenses/MIT)
77
* author: Heather Arthur <[email protected]>
88
* homepage: https://github.com/brainjs/brain.js#readme
9-
* version: 1.1.3
9+
* version: 1.2.0
1010
*
1111
* acorn:
1212
* license: MIT (http://opensource.org/licenses/MIT)
@@ -2463,6 +2463,7 @@ var Equation = function () {
24632463
_classCallCheck(this, Equation);
24642464

24652465
this.inputRow = 0;
2466+
this.inputValue = null;
24662467
this.states = [];
24672468
}
24682469

@@ -2611,6 +2612,25 @@ var Equation = function () {
26112612
return product;
26122613
}
26132614

2615+
/**
2616+
* copy a matrix
2617+
* @param {Matrix} input
2618+
* @returns {Matrix}
2619+
*/
2620+
2621+
}, {
2622+
key: 'input',
2623+
value: function input(_input) {
2624+
var self = this;
2625+
this.states.push({
2626+
product: _input,
2627+
forwardFn: function forwardFn() {
2628+
_input.weights = self.inputValue;
2629+
}
2630+
});
2631+
return _input;
2632+
}
2633+
26142634
/**
26152635
* connects a matrix via a row
26162636
* @param {Matrix} m
@@ -2722,6 +2742,27 @@ var Equation = function () {
27222742
* @output {Matrix}
27232743
*/
27242744

2745+
}, {
2746+
key: 'runInput',
2747+
value: function runInput(inputValue) {
2748+
this.inputValue = inputValue;
2749+
var state = void 0;
2750+
for (var i = 0, max = this.states.length; i < max; i++) {
2751+
state = this.states[i];
2752+
if (!state.hasOwnProperty('forwardFn')) {
2753+
continue;
2754+
}
2755+
state.forwardFn(state.product, state.left, state.right);
2756+
}
2757+
2758+
return state.product;
2759+
}
2760+
2761+
/**
2762+
* @patam {Number} [rowIndex]
2763+
* @output {Matrix}
2764+
*/
2765+
27252766
}, {
27262767
key: 'runBackpropagate',
27272768
value: function runBackpropagate() {
@@ -3476,7 +3517,7 @@ var RNN = function () {
34763517

34773518
_classCallCheck(this, RNN);
34783519

3479-
var defaults = RNN.defaults;
3520+
var defaults = this.constructor.defaults;
34803521

34813522
for (var p in defaults) {
34823523
if (!defaults.hasOwnProperty(p)) continue;
@@ -3868,7 +3909,7 @@ var RNN = function () {
38683909
value: function train(data) {
38693910
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
38703911

3871-
options = Object.assign({}, RNN.trainDefaults, options);
3912+
options = Object.assign({}, this.constructor.trainDefaults, options);
38723913
var iterations = options.iterations;
38733914
var errorThresh = options.errorThresh;
38743915
var log = options.log === true ? console.log : options.log;
@@ -3935,7 +3976,7 @@ var RNN = function () {
39353976
}, {
39363977
key: 'toJSON',
39373978
value: function toJSON() {
3938-
var defaults = RNN.defaults;
3979+
var defaults = this.constructor.defaults;
39393980
var model = this.model;
39403981
var options = {};
39413982
for (var p in defaults) {
@@ -3966,7 +4007,7 @@ var RNN = function () {
39664007
key: 'fromJSON',
39674008
value: function fromJSON(json) {
39684009
this.json = json;
3969-
var defaults = RNN.defaults;
4010+
var defaults = this.constructor.defaults;
39704011
var model = this.model;
39714012
var options = json.options;
39724013
var allMatrices = model.allMatrices;

browser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/recurrent/gru-time-step.js

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/recurrent/gru-time-step.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/recurrent/lstm-time-step.js

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/recurrent/lstm-time-step.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)