示例代码:
model = Sequential()
model.add(Dense(24, input_dim=13, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
-
history = model.fit(X_train, y_train, batch_size = 256, epochs = 10, verbose = 2, validation_split = 0.2)
-
score, acc = model.evaluate(X_test, y_test,verbose=2, batch_size= 256)
print('test accuracy:', acc)
-
yhat_classes = model.predict_classes(X_test)
问题原因:
在TensorFlow 2.6版本中删除了这个predict_classes
函数。
参考文档:https://keras.rstudio.com/reference/predict_proba.html#details
可以使用如下代码:
predict_x=model.predict(X_test)
classes_x=np.argmax(predict_x,axis=1)
或者
也可以尝试TensorFlow 2.5或其它的版本解决这个问题。
使用TensorFlow版本2.5,可能会有以下警告信息:
tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 2021-01-01. Please use instead:* np.argmax(model.predict(x), axis=-1), if your model does multi-class classification (e.g. if it uses a softmax last-layer activation).* (model.predict(x) > 0.5).astype("int32"), if your model does binary classification (e.g. if it uses a sigmoid last-layer activation).