我正在嘗試訓練 NLP 神經機器翻譯模型,并且在該代碼中我使用的是 Keras 的順序模型。我想以類的形式預測輸出,但由于我使用的是 Tensorflow 2.7.0 并且 predict_classes() 現在已經折舊,我應該如何解決它?這是代碼片段-:
model = load_model('model.h1.24_jan_19')
preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))
這是我得到的錯誤 -:
AttributeError 回溯(最近一次呼叫最后一次)
在 () 1 模型 = load_model('model.h1.24_jan_19') ----> 2 preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))
AttributeError: 'Sequential' 物件沒有屬性 'predict_classes'
uj5u.com熱心網友回復:
我不認為有直接替換,您需要使用輸出model.predict()并手動將它們映射到類標簽串列上,然后選擇最有信心的一個。
一個簡單的例子:
classes = ['a', 'b']
results = model.predict(testX.reshape((testX.shape[0],testX.shape[1])))
# the results here will be a n array of confidence level, if the last layer of your model uses the softmax activation function.
class_predicted = classes[np.argmax(predictions)] # this line gets the index of the highest confidence class and select from the classes list.
請注意,類串列中標簽的順序必須與您的輸入標簽相同,因此您應該仔細檢查。如果您正在使用 tensorflow API tf.data.dataset,那么您可以使用該dataset.class_names屬性來訪問類串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/372839.html
上一篇:聯合的交集用作度量或損失
下一篇:ValueError:層“max_pooling1d”的輸入0與層不兼容:預期ndim=3,發現ndim=4。收到完整形狀:(無、51644、29、32)
