Keras模型:与model.fit相同的数组未在model.predict

时间:2018-11-12 17:26:13

标签: python numpy tensorflow keras deep-learning

我有一个模特:

model.add(Dense(16, input_dim = X.shape[1], activation = 'tanh'))
model.add(Dropout(0.2))
model.add(Dense(8, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(4, activation = 'tanh'))
model.add(Dropout(0.2))
model.add(Dense(2, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae'])

在Model.evaluvate期间,它可以与'X'的输入配合使用:

history = model.fit(X, Y, validation_split=0.2, epochs=10, callbacks=   [PrintDot()], batch_size=10, verbose=0)

但是在预测过程中,当我使用X [1]时会引发错误:

ValueError: Error when checking input: expected dense_8_input to have shape (500,) but got array with shape (1,)

但是X [1]。形状为(500,):

X[1].shape
--> (500,)

如果有任何帮助,我该如何纠正此错误

1 个答案:

答案 0 :(得分:0)

Keras model.predict希望收到(amount_of_items, features)的输入。

因此,即使尝试预测单个样本,也必须将其重塑为(1, features),对于您的情况,应重塑为(1, 500)