Keras AttributeError: 'Sequential' 对象没有属性 'predict_classes' [英] Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
问题描述
我正在尝试按照本指南查找模型性能指标(F1 分数、准确率、召回率)https://machinelearningmastery.com/how-to-calculate-precision-recall-f1-and-more-for-deep-learning-models/
Im attempting to find model performance metrics (F1 score, accuracy, recall) following this guide https://machinelearningmastery.com/how-to-calculate-precision-recall-f1-and-more-for-deep-learning-models/
这个确切的代码几个月前还可以使用,但现在返回各种错误,非常令人困惑,因为我没有更改此代码的一个字符.也许包更新改变了一些事情?
This exact code was working a few months ago but now returning all sorts of errors, very confusing since i havent changed one character of this code. Maybe a package update has changed things?
我用model.fit拟合了序列模型,然后用model.evaluate找到了测试精度.现在我正在尝试使用 model.predict_classes 进行类预测(模型是一个多类分类器).代码如下:
I fit the sequential model with model.fit, then used model.evaluate to find test accuracy. Now i am attempting to use model.predict_classes to make class predictions (model is a multi-class classifier). Code shown below:
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)
最后一行返回错误AttributeError: 'Sequential' object has no attribute 'predict_classes'"
last line returns error "AttributeError: 'Sequential' object has no attribute 'predict_classes'"
这个确切的代码不久前还在工作,所以有点挣扎,感谢您的帮助
This exact code was working not long ago so struggling a bit, thanks for any help
推荐答案
此功能已在 TensorFlow 2.6 版中删除.根据 keras in rstudio 参考
This function were removed in TensorFlow version 2.6. According to the keras in rstudio reference
更新到
predict_x=model.predict(X_test)
classes_x=np.argmax(predict_x,axis=1)
或者使用 TensorFlow 2.5 或更高版本.
如果您使用的是 TensorFlow 2.5 版,您将收到以下警告:
If you are using TensorFlow version 2.5, you will receive the following warning:
tensorflowpythonkerasenginesequential.py:455: UserWarning: model.predict_classes()
已弃用,将在 2021-01-01 后移除.请改用:* np.argmax(model.predict(x), axis=-1)
,如果您的模型进行多类分类(例如,如果它使用 softmax
最后一层激活.* (model.predict(x) > 0.5).astype(int32")
,如果您的模型进行二元分类(例如,如果它使用 sigmoid
最后一层激活).
tensorflowpythonkerasenginesequential.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 asoftmax
last-layer activation).*(model.predict(x) > 0.5).astype("int32")
, if your model does binary classification (e.g. if it uses asigmoid
last-layer activation).
这篇关于Keras AttributeError: 'Sequential' 对象没有属性 'predict_classes'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!