[英]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).使用 Keras 预测 function 时,任何人都可以帮助解决以下问题吗?预测数据集的输入形状似乎正在改变(预测似乎在第一维中添加了“无”)。
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)当调用 print function 来显示预测数据集的形状时,它会按预期打印出 (2, 90)(2 行数据和 90 个输入)
When trying to use the predict function this instead prints the following error:当尝试使用预测 function 时,它会打印以下错误:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)
The error is caused by this code line ynew = model.predict(array([Xnew]))
.该错误是由此代码行
ynew = model.predict(array([Xnew]))
引起的。
Please remove the array from this line and use this: ynew = model.predict(Xnew)
请从此行中删除数组并使用:
ynew = model.predict(Xnew)
I have replicated the similar code with an abalone dataset and attached this gist for your reference.我已经用鲍鱼数据集复制了类似的代码,并附上了这个要点供您参考。
Either of the following work for me (my model was trained to take 2D input):以下任一项对我有用(我的 model 被训练接受 2D 输入):
X_new = [[-1.0, -1.0]]
model.predict(X_new)
or或者
X_new = [-1.0, -1.0]
model.predict([X_new])
Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.