'Input 0 of layer sequential_2 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)

model = Sequential()
model.add(LSTM(100, input_shape = [X_sequence.shape[1], X_sequence.shape[2]]))
model.add(Dropout(0.5))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss="binary_crossentropy"
          , metrics=[binary_accuracy]
          , optimizer="adam")
model.summary()

training_size = int(len(X_sequence) * 0.7)
X_train, y_train = X_sequence[:training_size], y[:training_size]
X_test, y_test = X_sequence[training_size:], y[training_size:]
model.fit(X_train, y_train, batch_size=64, epochs=10)
y_test_pred = model.predict(X_test)
def create_dataset(dataset, time_step=1):
 dataX = []
 for i in range(len(dataset)-time_step-1):
    a = dataset[i:(i+time_step), 0]
    dataX.append(a)
 return np.array(dataX)
x_final=create_dataset(test.loc[:, "sensor_00":"sensor_12"].values)
y_final=model.predict(x_final)

There is error in last line. I have successfully trained the data but while predicting for test data. There is error.



Solution 1:[1]

I've used the dataset from here to reproduce the issue.

Please expand the dimensions of x_final to solve the error as follows

    x_final=create_dataset(test.loc[:, "sensor_00":"sensor_12"].values)
    
    #Expand dimensions    
    x_final=tf.expand_dims(x_final,axis=1)
    y_final=model.predict(x_final)

Let us know if the issue still persists. Thanks!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tfer3