![](/img/trans.png)
[英]AttributeError: 'Sequential' object has no attribute 'predict_classes'
[英]Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
我試圖按照本指南https://machinelearningmastery.com/how-to-calculate-precision-recall-f1-and-more-for-deep-learning-models/查找 model 性能指標(F1 分數、准確度、召回率)
這個確切的代碼幾個月前還在工作,但現在返回各種錯誤,非常令人困惑,因為我沒有更改此代碼的一個字符。 也許 package 更新改變了一切?
我將順序 model 與 model.fit 擬合,然后使用 model.evaluate 來查找測試精度。 現在我正在嘗試使用 model.predict_classes 進行 class 預測(模型是多類分類器)。 代碼如下圖:
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'”
這個確切的代碼不久前還在工作,所以有點掙扎,感謝您的幫助
此功能已在 TensorFlow 2.6 版中刪除。 根據rstudio參考中的keras
更新到
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()
已棄用,將在 2021-01-01 之后刪除。 請改用:*np.argmax(model.predict(x), axis=-1)
,如果您的模型進行多類分類(例如,如果它使用softmax
最后一層激活)。*(model.predict(x) > 0.5).astype("int32")
,如果您的模型進行二進制分類(例如,如果它使用sigmoid
最后一層激活)。
我遇到了同樣的錯誤,我使用下面的代碼,成功了
替換:
predictions = model.predict_classes(x_test)
有了這個:
predictions = (model.predict(x_test) > 0.5).astype("int32")
python包類型:Tensorflow 2.6.0
我們可以用以下代碼替換有問題的代碼行:
y_predict = np.argmax(model.predict(x_test), axis=-1)
我使用以下代碼進行預測
y_pred = model.predict(X_test)
y_pred = np.round(y_pred).astype(int)
在 Tensorflow 2.7 中,可以使用以下代碼獲得預測類:
predicted = np.argmax(model.predict(token_list),axis=1)
在最新版本的 Tensorflow 中, predict_classes
函數已被棄用(在以前的版本中對此有警告)。 新語法如下:
predictions = np.argmax(model.predict(x_test),axis=1)
使用它,因為最新版本的 tensorflow 刪除了 predict_classes
predictions = (model.predict(X_test) > 0.5)*1
由於這是一個二元問題(0 或 1),因此輸出類別取決於概率是否大於 0.5。 因此上面的代碼
對於下面的整個數據集的代碼,
preds = model.predict_classes(test_sequences)
此代碼可用於新版本。
y_predict = np.argmax(model.predict(test_sequences), axis=1)
在此,“ test_sequence ”是您必須預測的數據框,軸是選擇列或行。
我對此有疑問,當我使用
np.argmax(model.predict(x_test), axis=-1)
替換以前的'predict_classes',然后我使用'accuracy_score'來獲得准確度分數,分數很低。 但是當我使用
predictions = (model.predict(x_test) > 0.5).astype("int32")
准確度得分正常且正確。 其他代碼相同。
如果您使用的是多類分類,則使用np.argmax(model.predict(x), axis=-1)
例如:
predictions = np.argmax(model.predict(x_test),axis=1)
或者,如果您手頭有二進制分類問題,請使用(model.predict(x) > 0.5).astype("int32")
例如:
predictions=(model.predict(X_test) > 0.5).astype("int32")
如果我想在 CNN 中預測是否患有癌症。 那么上述錯誤將如何解決。 BEACUSAE 我們應該將其分類為 1 或零。
代碼是:
input_arr = img_to_array(img)/255
plt.imshow(input_arr)
plt.sh`enter code here`ow
input_array = np.expand_dim(input_arr,axis=0)
pred = model.predict(input_arr)[0],[0]
pred
第 5 行代碼中的錯誤 ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.