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.