basil3
basil3

Reputation: 59

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)

Can anybody help with the following problem when using Keras predict function the input shape for the prediction dataset seems to be changing (predict seems to add 'none' to the first dimension).

scaler = MinMaxScaler()
scaler2 = MinMaxScaler()

normalized_data = scaler.fit_transform(dataset)
normalized_predict_data = scaler2.fit_transform(predict_dataset)

x = normalized_data[:, 0:90]
y = normalized_data[:, 90]

z = normalized_predict_data[:, 0:90]
print(z.shape)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=10)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

model = Sequential()
model.add(Dense(4, input_dim=90, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

history = model.fit(x_train, y_train, validation_split=0.33, epochs=50, batch_size=100, verbose=0)

loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print("Model loss: %.2f, Accuracy: %.2f" % ((loss * 100), (accuracy * 100)))

Xnew = z
ynew = model.predict(array([Xnew]))

for item in Xnew:
    print("X=%s, Predicted=%s" % (item, ynew[0]))

When calling the print function to show the shape of the prediction dataset this prints out (2, 90) as expected (2 rows of data and 90 inputs)

When trying to use the predict function this instead prints the following error:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)

Upvotes: 0

Views: 307

Answers (2)

Christina Cole
Christina Cole

Reputation: 21

Either of the following work for me (my model was trained to take 2D input):

X_new = [[-1.0, -1.0]]
model.predict(X_new)

or:

X_new = [-1.0, -1.0]
model.predict([X_new])

Upvotes: 2

user11530462
user11530462

Reputation:

The error is caused by this code line ynew = model.predict(array([Xnew])).

Please remove the array from this line and use this: ynew = model.predict(Xnew)

I have replicated the similar code with an abalone dataset and attached this gist for your reference.

Upvotes: 1

Related Questions