1

I have a problem with the input for the fit-function of an LSTM-Model in TensorFlow. I have an input with the following shape:
(5, 128, 78, 80)
The fields are: (number of samples, timesteps, feature1, feature2)

The output has the shape: (5, 128, 78, 2)

This is my model:

from tensorflow.keras.layers import LSTM, Dense, Dropout, Activation

batch_size=5

time_model = tf.keras.Sequential()
time_model.add(tf.keras.layers.LSTM(512,return_sequences=True,input_shape=(128,2)))
time_model.add(Activation('sigmoid'))
time_model.add(Dense(2,name="dense"))
time_model.compile(loss='categorical_crossentropy', optimizer='rmsprop')


time_model.fit(x=time_input,y=time_output, epochs=10, batch_size=batch_size)

I get the following error:
ValueError: Input 0 of layer sequential_38 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (5, 128, 78, 80)

So I think, I have to change the shape of my data, but I don't know how. I tried already different values for input and input_shape-attribute.
I read in https://www.tensorflow.org/api_docs/python/tf/keras/layers/LSTM that the input has to be a tensor with shape [batch, timesteps, feature]. So I put the two features in a nested array, and gave [batch, timesteps, array of the features] to the fit-function. But it told me that the data could not be converted to a tensor. Also explicit converting with tf.convert_to_tensor did not work.

I would be really glad, if someone could explain me, how I can pass input data with two features to an LSTM-model.

kk12
  • 36
  • 3
  • `reture_sequences=False` try this it should work. In line `time_model.add(tf.keras.layers.LSTM(512,return_sequences=True,input_shape=(128,2))) ` – GreatThinker Jan 29 '21 at 11:37
  • Unfortunately this does not work. I get the same error as before. – kk12 Jan 29 '21 at 12:55
  • https://stackoverflow.com/questions/48760472/how-to-use-a-keras-rnn-model-to-forecast-for-future-dates-or-events/65567559#65567559 – Nicolas Gervais Jan 29 '21 at 13:29
  • Found a similar issue [here](https://stackoverflow.com/questions/67345023/what-is-the-correct-input-shape-of-multivariate-time-series-for-lstm-in-keras), this might help you. Thanks! –  Apr 11 '22 at 09:41

0 Answers0