error with LSTM, dimensions stuff

143 views
Skip to first unread message

Javier I

unread,
Aug 9, 2018, 1:34:59 PM8/9/18
to TensorFlow.js Discussion
good day friends
anybody can tell me why I get this error here?

"Error: Error when checking input: expected lstm_LSTM1_input to have 3 dimension(s). but got array with shape 6,1"

thank u very much  ;)

async function predictfuture(){

const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);
    xs.print();

    ys.print();


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


    const model = tf.sequential();

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

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

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

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

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

    });

}

predictfuture();


Javier I

unread,
Aug 9, 2018, 2:06:00 PM8/9/18
to TensorFlow.js Discussion
this works when I switch to tensor3D, is it a requirement of lstm to have a 3d vector?


async function predictfuture(){

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

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


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

    const model = tf.sequential();

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

//output layer
const output = tf.layers.lstm({
    units: 1, 
    activation: 'sigmoid',
    returnSequences: true
})
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]] );
const inputs = tf.tensor3d( [[[1]]] );

Daniel Smilkov

unread,
Aug 9, 2018, 8:41:00 PM8/9/18
to ide...@gmail.com, TensorFlow.js Discussion
Hi Javier,

The issue might be that you are specifying an explicit inputShape: [1, 1] when creating the first lstm. This says your input is 2d ignoring the batch dimension, which means that along with the batch dimension, your input is expected to be rank-3. Trying removing the inputShape: [1,1] and see what happens.

Daniel


--
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/842bd331-e845-4e2d-b4c3-0e1761cc7960%40tensorflow.org.

Javier I

unread,
Aug 11, 2018, 4:36:26 PM8/11/18
to TensorFlow.js Discussion, ide...@gmail.com
Thank you Daniel,
I think I also read that lstm expects 3d,
will try what you say, thank u
Reply all
Reply to author
Forward
0 new messages