我试图按照本指南https://machinelearningmastery.com/how-to-calculate-precision-recall-f1-and-more-for-deep-learning-models/找到模型性能指标(F1 分数、准确性、召回率)
这个确切的代码在几个月前还可以使用,但现在返回各种错误,非常令人困惑,因为我没有更改此代码的一个字符。也许包更新改变了事情?
我用 model.fit 拟合序列模型,然后使用 model.evaluate 找到测试精度。现在我正在尝试使用 model.predict_classes 进行类预测(模型是一个多类分类器)。代码如下所示:
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)
Run Code Online (Sandbox Code Playgroud)
最后一行返回错误“AttributeError:‘Sequential’对象没有属性‘predict_classes’”
这个确切的代码不久前还在工作,所以有点挣扎,感谢您的帮助
小智 43
我遇到了同样的错误,我使用以下代码并成功
替换:
predictions = model.predict_classes(x_test)
Run Code Online (Sandbox Code Playgroud)
有了这个:
predictions = (model.predict(x_test) > 0.5).astype("int32")
Run Code Online (Sandbox Code Playgroud)
python 包的类型:Tensorflow 2.6.0
小智 12
在最新版本的 Tensorflow 中,该predict_classes函数已被弃用(之前的版本中对此有警告)。新语法如下:
predictions = np.argmax(model.predict(x_test),axis=1)
Run Code Online (Sandbox Code Playgroud)
小智 10
我们可以用以下代码替换有问题的代码行:
y_predict = np.argmax(model.predict(x_test), axis=-1)
Run Code Online (Sandbox Code Playgroud)
我使用以下代码进行预测
y_pred = model.predict(X_test)
y_pred = np.round(y_pred).astype(int)
Run Code Online (Sandbox Code Playgroud)
此函数在 TensorFlow 2.6 版中被删除。根据rstudio参考中的keras
更新到
predict_x=model.predict(X_test)
classes_x=np.argmax(predict_x,axis=1)
Run Code Online (Sandbox Code Playgroud)
或者使用 TensorFlow 2.5 或更高版本。
如果您使用的是 TensorFlow 2.5 版,您将收到以下警告:
tensorflow\python\keras\engine\sequential.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最后一层激活)。
小智 6
在 Tensorflow 2.7 中,可以通过以下代码获得预测类:
predicted = np.argmax(model.predict(token_list),axis=1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2188 次 |
| 最近记录: |