TensorflowJS - how to add time series data into tensors?

689 views
Skip to first unread message

Alexandru Cobuz

unread,
May 14, 2018, 1:02:27 PM5/14/18
to TensorFlow.js Discussion
Hello,
How can I train an AI using time series instead of normal data?
I wasn't able to find any example like this, I'm considering a simple time series: yy/mm/dd, value1, value2 and to make a simple prediction for a future time of yy/mm/dd?

Any sort of example will be really useful!

David Soergel

unread,
May 14, 2018, 1:15:31 PM5/14/18
to alexand...@gmail.com, TensorFlow.js Discussion
You'll need an RNN for this task.  If you search on Google for "Keras RNN" you'll get a bunch of examples, tutorials, and videos that will largely translate to TensorFlow.js (because we provide the same RNN layers that Keras does).  Then, see the TF.js "Addition RNN" example (https://github.com/tensorflow/tfjs-examples/tree/master/addition-rnn) for mechanics of how to hook things up in TF.js specifically.

Hope this helps,
-ds

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/3cd8fa6e-e01a-445b-bed3-133e15cc006e%40tensorflow.org.

Alexandru Cobuz

unread,
May 14, 2018, 1:24:50 PM5/14/18
to TensorFlow.js Discussion, alexand...@gmail.com
Thank you, this is usefull!
I was looking for a direct TensorFlow.js implementation instead of using other tools. But I have a starting point now, thank you!

David Soergel

unread,
May 15, 2018, 10:00:53 AM5/15/18
to alexand...@gmail.com, TensorFlow.js Discussion
Yep, the Addition RNN example is a pure TF.js implementation; no other tools needed.  I suggested searching for Keras tutorials etc. just because there are a lot more of them, and the concepts and the API are the same.

-ds

Barry Lachapelle

unread,
May 30, 2018, 7:51:37 PM5/30/18
to TensorFlow.js Discussion
Hi Alex. I’m looking to do the exact same thing. Were you able to find any Keras examples that might help?

Iconoclasta Vesgo

unread,
May 31, 2018, 4:59:47 AM5/31/18
to Barry Lachapelle, TensorFlow.js Discussion
I'll try to come up with a simple example for this when I'm on the computer. hope it can point you in the right direction.

On Thu, May 31, 2018, 12:51 AM Barry Lachapelle <barry.la...@gmail.com> wrote:
Hi Alex. I’m looking to do the exact same thing. Were you able to find any Keras examples that might help?

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.

blach...@kujakuja.com

unread,
May 31, 2018, 9:23:47 AM5/31/18
to TensorFlow.js Discussion
Thanks Tiago! That would be amazing.

In the meantime Ill try translating this Keras tutorial > https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/

Atanas Stoyanov

unread,
May 31, 2018, 9:40:06 AM5/31/18
to TensorFlow.js Discussion, alexand...@gmail.com
Hi,

I have a running example of tfjs with time series here: https://crypto-grommet.com/models/playground
Source code can be found here: https://github.com/atanasster/crypto-grommet (there is more code than you probably wanted, but the basic implementation is in https://github.com/atanasster/crypto-grommet/tree/master/tensorflow)

Its an early version, still working on it but can be a good start for experimenting.

blach...@kujakuja.com

unread,
May 31, 2018, 10:38:40 AM5/31/18
to TensorFlow.js Discussion
Very nice Atanas. Would you happen to have a super simple/stripped down version of this?

Atanas Stoyanov

unread,
May 31, 2018, 10:41:26 AM5/31/18
to TensorFlow.js Discussion
sorry - I don't have a stripped down version as of now. Currently busy on a hyper parameters es6 implementation for tfjs, and hopefully once I get a bit of free time will make a sample.
In the meantime, if you have some questions, feel free to reach out.

blach...@kujakuja.com

unread,
May 31, 2018, 2:58:11 PM5/31/18
to TensorFlow.js Discussion
Thanks Atanas. I am currently running into data shape issues with LSTM. Any ideas on what I am doing wrong exactly.

var data = tf.tensor2d([1, 2, 3, 4, 5, 6], [6, 1]);
var y = tf.tensor2d([1, 2, 3, 4, 5, 6], [6, 1]);
y.print()


const learningRate = 0.0001;
const optimizer = tf.train.sgd(learningRate);

const model = tf.sequential();
model.add(
tf.layers.lstm({
units: 1,
inputShape: [6, 1],
recurrentInitializer: 'glorotNormal'
})
);

model.compile({ loss: "meanSquaredError", optimizer: optimizer });

console.log('training');

await model.fit(data, y, { epochs: 200 }).then(() => {
// // Use the model to do inference on a data point the model hasnt
// // seen before:
// //console.log('Prediction:');
//
// //model.predict(tf.tensor2d([6], [1, 1])).print();
});

Atanas Stoyanov

unread,
May 31, 2018, 10:12:42 PM5/31/18
to TensorFlow.js Discussion
Not sure if tfjs has run documentation, but you can pretty much consult the keras docs when using the layers API.

Input shape: 3D tensor with shape (batch_size, timesteps, input_dim

A simple version of your example would be 
var data = tf.tensor2d([1, 2, 3, 4, 5, 6], [6, 1, 1]);
....
 tf.layers.lstm({
            units: 1,
            inputShape: [1, 1],
            recurrentInitializer: 'glorotNormal'
        })
Message has been deleted

blach...@kujakuja.com

unread,
Jun 6, 2018, 4:00:26 PM6/6/18
to TensorFlow.js Discussion
Thank you Atanas.. this got me closer. My code so far....



async
function predictfuture(){


   
////////////////////////
   
// create fake data
   
///////////////////////


   
var xs = tf.tensor3d([
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]]
   
]);
    xs
.print();


   
var ys = tf.tensor3d([
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]],
       
[[1],[1],[0]]
   
]);
    ys
.print();




   
////////////////////////
   
// create model w/ layers api
   
///////////////////////


    console
.log('Creating Model...');


   
/*


    model design:


                    i(xs)   h       o(ys)
    batch_size  ->  *       *       * -> batch_size
    timesteps   ->  *       *       * -> timesteps
    input_dim   ->  *       *       * -> input_dim




    */



   
const model = tf.sequential();


   
//hidden layer
   
const hidden = tf.layers.lstm({
        units
: 3,
        activation
: 'sigmoid',
        inputShape
: [3 , 1]
   
});
    model
.add(hidden);


   
//output layer
   
const output = tf.layers.lstm({
        units
: 3,
        activation
: 'sigmoid',
        inputShape
: [3] //optional
   
});
    model
.add(output);


   
//compile
   
const sgdoptimizer = tf.train.sgd(0.1)
    model
.compile({
        optimizer
: sgdoptimizer,
        loss
: tf.losses.meanSquaredError
   
});


   
////////////////////////
   
// train & predict
   
///////////////////////


    console
.log('Training Model...');


    await model
.fit(xs, ys, { epochs: 200 }).then(() => {


        console
.log('Training Complete!');
        console
.log('Creating Prediction...');


       
const inputs = tf.tensor2d( [[1],[1],[0]] );
        let outputs
= model.predict(inputs);
        outputs
.print();


   
});


}


predictfuture
();

Reply all
Reply to author
Forward
0 new messages