I have a question on tensorflow/tfjs LSTM inputShape and also LSTM basic understaning

Viewed 388

my training data set is for one user

let training = [
    [[2019.1], [10]],
    [[2019.2], [2]],
    [[2019.4], [11]],
    [[2019.5], [31]]
]

for this simple input I want to get next month prediction. To understand LSTM time series first. using below code for generating training model

let train_x = training.map(i => {return i[0]})
let train_y = training.map(j=> {return j[1]})

const model = tf.sequential();
**model.add(tf.layers.lstm({units: 128, returnSequences: false,  inputShape:[train_x.length]}));**
model.add(tf.layers.dropout(0.2))
model.add(tf.layers.dense({units: training.length, activation: 'softmax'}));

model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.rmsprop(0.002)});

**const xs = tf.tensor3d([train_x]);**
const ys = tf.tensor2d(train_y, [training.length, train_y[0].length]);

Error:

Error: Input 0 is incompatible with layer lstm_LSTM1: expected ndim=3, found ndim=2

Problem is what input shape should be given and what should be tf.tensor3d input. For my understanding i am trying this simple example. Trying it without keras

1 Answers

The error message is straightforward:

Input 0 is incompatible with layer lstm_LSTM1: expected ndim=3, found ndim=2

An lstm layer expects a 3d input. It means that the inputShape should be [a, b] where both a and b are numbers (a can be null as well). There is a single sequence of 2 elements. a is therefore 1 (single sequence: number of sequence we want the layer to see before making a prediction; looking at the data here it seems to be 1, but can be changed to a different value) and b is 2 (2 elements per sequence).

training would be:

training = [
    [[2019.1, 10]],
    [[2019.2, 2]],
    [[2019.4], [11]],
    [[2019.5], [31]]
]
xs = tf.tensor(training).reshape([-1, 1, 2])

And the lstm layer becomes the following:

model.add(tf.layers.lstm({units: 128, returnSequences: false,  inputShape:[1, 2]}));

And voilĂ , the whole model looks as below:

const model = tf.sequential();
model.add(tf.layers.lstm({units: 128, returnSequences: false,  inputShape:[1, 2]}));
model.add(tf.layers.dropout(0.2))
model.add(tf.layers.dense({units: 20, activation: 'softmax'}));
model.summary()
model.compile({loss: 'categoricalCrossentropy', optimizer: tf.train.rmsprop(0.002)});
model.summary()
const training = [
        [[2019.1, 10]],
        [[2019.2, 2]],
        [[2019.4], [11]],
        [[2019.5], [31]]
    ]
const xs = tf.tensor(training).reshape([-1, 1, 2])
await model.fit(xs, tf.ones([4, 20]))
model.predict(tf.ones([1, 1, 2])).print()
Related