![](/img/trans.png)
[英]Loading the saved models from tf.keras in different versions (From tf 2.3.0 to tf 1.12)
[英]Confusion Matrix produces different results in Keras model tf==2.3.0
用 Keras 順序 Model 預測,
要獲得 Class 標簽,我們可以做
yhat_classes1 = Keras_model.predict_classes(predictors)[:, 0] #this shows deprecated warning in tf==2.3.0
WARNING:tensorflow:From <ipython-input-54-226ad21ffae4>:1: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.
Instructions for updating:
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).
所以
yhat_classes2 = np.argmax(Keras_model.predict(predictors), axis=-1)
使用前 class 個標簽,如果我創建混淆矩陣,我得到
matrix = confusion_matrix(actual_y, yhat_classes1)
[[108579 8674]
[ 1205 24086]]
但是對於帶有混淆矩陣的第二個 class 標簽,我得到 0 表示真陽性和假陽性
matrix = confusion_matrix(actual_y, yhat_classes2)
[[117253 0]
[ 25291 0]]
我可以知道我的問題是什么嗎?
混淆矩陣返回 2 行/列,這讓我相信你有兩個類。 該警告特別指出您應該使用此行進行二進制分類,這就是您正在做的事情:
(model.predict(x) > 0.5).astype("int32")
請改用:*
np.argmax(model.predict(x), axis=-1)
,如果您的 model 進行多類分類(例如,如果它使用softmax
最后一層激活)。*(model.predict(x) > 0.5).astype("int32")
,如果您的 model 進行二進制分類(例如,如果它使用sigmoid
最后一層激活)。
錯誤是您在 1D output 上使用了np.argmax(model.predict(X), axis=-1)
,因此這總是返回同一列(因為只有一個,所以最大值將在該列中) . 這說明您的所有預測值都在混淆矩陣的同一列中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.