当使用Keras预测函数时,预测数据集的输入形状似乎正在更改(预测似乎向第一维添加了“无”)。
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函数显示预测数据集的形状时,将按预期打印输出(2,90)(2行数据和90个输入)
当尝试使用predict函数时,它会打印以下错误:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)
2条答案
按热度按时间tsm1rwdh1#
以下两种方法对我都适用(我的模型已训练为接受2D输入):
或:
lzfw57am2#
该错误是由
ynew = model.predict(array([Xnew]))
代码行引起的。请删除此行中的数组并使用以下内容:
ynew = model.predict(Xnew)
个我已经用鲍鱼数据集复制了类似的代码,并附上了这个gist供您参考。