Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dead simple example of synaptic js lstm rnn algorithm

It's pretty crazy that there isn't a dead simple example of the LSTM RNN predicting time series data.

https://github.com/cazala/synaptic

https://github.com/cazala/synaptic/wiki/Architect#lstm

I'd like to use the historical data in the following array:

const array = [
    0,
    0,
    0,
    1,
    0,
    0,
    0,
    1
];

Some pretty mind blowing data right there right?

I'd like to A) train the algorithm with the array then B) test the algorithm with the following array:

const array = [
    0,
    0,
    0,
    1,
    0,
    0,
    0,
    1,
    0
];

Should result in it predicting a 0.

Unfortunately the documentation is pretty bad, no clear code examples exist. Anyone have any examples?

like image 676
basickarl Avatar asked Apr 23 '17 18:04

basickarl


People also ask

Is LSTM and RNN same?

Long short-term memory (LSTM) networks are an extension of RNN that extend the memory. LSTM are used as the building blocks for the layers of a RNN. LSTMs assign data “weights” which helps RNNs to either let new information in, forget information or give it importance enough to impact the output.

Is LSTM better than RNN?

It difficult to train RNN that requires long-term memorization meanwhile LSTM performs better in these kinds of datasets it has more additional special units that can hold information longer. LSTM includes a 'memory cell' that can maintain information in memory for long periods of time.

What is the disadvantage of LSTM?

As it is said, everything in this world comes with its own advantages and disadvantages, LSTMs too, have a few drawbacks which are discussed as below: LSTMs became popular because they could solve the problem of vanishing gradients. But it turns out, they fail to remove it completely.


1 Answers

This answer is not written with Synaptic, but with Neataptic. I decided to make a quick answer that I will include in the documentation soon. This is the code, it works 9/10 times:

var network = new neataptic.architect.LSTM(1,6,1);

// when the timeseries is [0,0,0,1,0,0,0,1...]
var trainingData = [
  { input: [0], output: [0] },
  { input: [0], output: [0] },
  { input: [0], output: [1] },
  { input: [1], output: [0] },
  { input: [0], output: [0] },
  { input: [0], output: [0] },
  { input: [0], output: [1] },
];

network.train(trainingData, {
  log: 500,
  iterations: 6000,
  error: 0.03,
  clear: true,
  rate: 0.05,
});

Run it on JSFIDDLE to see the prediction! For more predictions, open this one.

Explanation to some choices I made:

  • I set option clear to true, as you want do a chronological timeseries prediction. This makes sure that the network starts from the 'beginning' every training iteration, instead of continuing on from the 'end' of the last iteration.
  • Rate is fairly low, higher rates will get stuck at an MSE error of ~0.2
  • The LSTM has 1 block of 6 memory nodes, lower amounts don't seem to work as well.
like image 84
Thomas Wagenaar Avatar answered Nov 14 '22 22:11

Thomas Wagenaar