ValueError in model.fit in lstm

78 Views Asked by At

I am trying to fit an lstm model to my data read as csv file. (320,6) is the shape of x_train, and the model is given as

def build_modelLSTMlite(input_shape):


    model = keras.Sequential()

    model.add(keras.layers.LSTM(64, input_shape=input_shape))

    model.add(keras.layers.Dense(64, activation='relu'))
    model.add(keras.layers.Dropout(0.3))


    model.add(keras.layers.Dense(10, activation='softmax'))

    return model

model = build_modelLSTMlite(input_shape)
optimiser = keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=optimiser,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train, batch_size=32, epochs=100)

This model.fit() is showing value error

ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 320, 6), found shape=(32, 6)
1

There are 1 best solutions below

2
On

You have to create a sliding version of your x_train data. Something like:

from numpy.lib.stride_tricks import sliding_window_view

x_train_lstm = sliding_window_view(x_train, (input_shape[0], x_train.shape[1])).squeeze(axis=1)

history = model.fit(X_train_lstm, y_train[:-input_shape[0]+1], batch_size=32, epochs=100)