最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tensorflow.js LSTM time series prediction - Stack Overflow

programmeradmin4浏览0评论

I am trying to build a simple time-series prediction script in Tensorflow.js with an LSTM RNN. I am new to ML obviously. I have been trying to adapt my JS code from the Keras RNN/LSTM layer api which apparently is the same thing. From what I gather my layer, shapes etc are all correct. Any thoughts on what I am doing wrong here?

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);

    //pile
    const sgdoptimizer = tf.train.sgd(0.1)
    modelpile({
        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();

And my error:

I am trying to build a simple time-series prediction script in Tensorflow.js with an LSTM RNN. I am new to ML obviously. I have been trying to adapt my JS code from the Keras RNN/LSTM layer api which apparently is the same thing. From what I gather my layer, shapes etc are all correct. Any thoughts on what I am doing wrong here?

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);

    //pile
    const sgdoptimizer = tf.train.sgd(0.1)
    model.pile({
        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();

And my error:

Share Improve this question asked Jun 6, 2018 at 20:04 barrylachapellebarrylachapelle 9874 gold badges15 silver badges37 bronze badges 2
  • 2 As I understood LSTMs in tensorflow they always need an extra dimension to reflect the steps. So const inputs = tf.tensor3d( [[[1],[1],[0]]] ); would be my guess – Sebastian Speitel Commented Jun 6, 2018 at 20:17
  • 1 Nice catch but unfortunately still getting the same error. – barrylachapelle Commented Jun 6, 2018 at 20:21
Add a ment  | 

1 Answer 1

Reset to default 7

The code runs by adding returnSequences: true and changing the output layer units to 1:

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

//output layer
const output = tf.layers.lstm({
    units: 1, 
    activation: 'sigmoid',
    returnSequences: true
})
model.add(output);

And as @Sebastian Speitel mentions, change the input to:

const inputs = tf.tensor3d( [[[1],[1],[0]]] );
发布评论

评论列表(0)

  1. 暂无评论