[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)
[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)
使用 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]))
当调用 print function 来显示预测数据集的形状时,它会按预期打印出 (2, 90)(2 行数据和 90 个输入)
当尝试使用预测 function 时,它会打印以下错误:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)
该错误是由此代码行ynew = model.predict(array([Xnew]))
引起的。
请从此行中删除数组并使用: ynew = model.predict(Xnew)
我已经用鲍鱼数据集复制了类似的代码,并附上了这个要点供您参考。
以下任一项对我有用(我的 model 被训练接受 2D 输入):
X_new = [[-1.0, -1.0]]
model.predict(X_new)
或者
X_new = [-1.0, -1.0]
model.predict([X_new])
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.