简体   繁体   English

Keras AttributeError: 'Sequential' object 没有属性 'predict_classes'

[英]Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'

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/我试图按照本指南https://machinelearningmastery.com/how-to-calculate-precision-recall-f1-and-more-for-deep-learning-models/查找 model 性能指标(F1 分数、准确度、召回率)

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?也许 package 更新改变了一切?

I fit the sequential model with model.fit, then used model.evaluate to find test accuracy.我将顺序 model 与 model.fit 拟合,然后使用 model.evaluate 来查找测试精度。 Now i am attempting to use model.predict_classes to make class predictions (model is a multi-class classifier).现在我正在尝试使用 model.predict_classes 进行 class 预测(模型是多类分类器)。 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)
 

last line returns error "AttributeError: 'Sequential' object has no attribute 'predict_classes'"最后一行返回错误“AttributeError: 'Sequential' object has no attribute 'predict_classes'”

This exact code was working not long ago so struggling a bit, thanks for any help这个确切的代码不久前还在工作,所以有点挣扎,感谢您的帮助

This function were removed in TensorFlow version 2.6.此功能已在 TensorFlow 2.6 版中删除。 According to the keras in rstudio reference根据rstudio参考中的keras

update to更新到

predict_x=model.predict(X_test) 
classes_x=np.argmax(predict_x,axis=1)

Or use TensorFlow 2.5 or later.或者使用 TensorFlow 2.5 或更高版本。

If you are using TensorFlow version 2.5, you will receive the following warning:如果您使用的是 TensorFlow 2.5 版,您将收到以下警告:

tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 2021-01-01. tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes()已弃用,将在 2021-01-01 之后删除。 Please use instead:* np.argmax(model.predict(x), axis=-1) , if your model does multi-class classification (eg if it uses a softmax last-layer activation).* (model.predict(x) > 0.5).astype("int32") , if your model does binary classification (eg if it uses a sigmoid last-layer activation).请改用:* np.argmax(model.predict(x), axis=-1) ,如果您的模型进行多类分类(例如,如果它使用softmax最后一层激活)。* (model.predict(x) > 0.5).astype("int32") ,如果您的模型进行二进制分类(例如,如果它使用sigmoid最后一层激活)。

I experienced the same error, I use this following code, and succeed我遇到了同样的错误,我使用下面的代码,成功了

Replaced:替换:

predictions = model.predict_classes(x_test)

With this one:有了这个:

predictions = (model.predict(x_test) > 0.5).astype("int32")

Type of python packages : Tensorflow 2.6.0 python包类型:Tensorflow 2.6.0

我们可以用以下代码替换有问题的代码行:

y_predict = np.argmax(model.predict(x_test), axis=-1)

I used following code for predictions我使用以下代码进行预测

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)

In the newest version of Tensorflow, the predict_classes function has been deprecated (there was a warning in previous versions about this).在最新版本的 Tensorflow 中, predict_classes函数已被弃用(在以前的版本中对此有警告)。 The new syntax is as follows:新语法如下:

predictions = np.argmax(model.predict(x_test),axis=1)

Use this as the predict_classes are removed with the latest version of tensorflow使用它,因为最新版本的 tensorflow 删除了 predict_classes

predictions = (model.predict(X_test) > 0.5)*1 

Since this is a binary problem (0 or 1), the output class is determined by whether the probability is bigger than 0.5.由于这是一个二元问题(0 或 1),因此输出类别取决于概率是否大于 0.5。 Hence the code above因此上面的代码

For this code below for an entire dataset,对于下面的整个数据集的代码,

preds = model.predict_classes(test_sequences)

This code can be used for the new versions.此代码可用于新版本。

y_predict = np.argmax(model.predict(test_sequences), axis=1)

In this, the " test_sequence " is the data frame u have to predict, and the axis is to choose either columns or rows.在此,“ test_sequence ”是您必须预测的数据框,是选择列或行。

I have a question about this, When I use我对此有疑问,当我使用

np.argmax(model.predict(x_test), axis=-1)

to replace previous 'predict_classes', and after that I use 'accuracy_score' to get the accuracy score, the score is very low.替换以前的'predict_classes',然后我使用'accuracy_score'来获得准确度分数,分数很低。 but when I use但是当我使用

predictions = (model.predict(x_test) > 0.5).astype("int32")

the accuracy score is normal and correct.准确度得分正常且正确。 other codes are the same.其他代码相同。

If you are using a multi-class classification then use np.argmax(model.predict(x), axis=-1)如果您使用的是多类分类,则使用np.argmax(model.predict(x), axis=-1)

for example:例如:

predictions = np.argmax(model.predict(x_test),axis=1)

Or else if you have a Binary classification problem at hand use (model.predict(x) > 0.5).astype("int32")或者,如果您手头有二进制分类问题,请使用(model.predict(x) > 0.5).astype("int32")

for example:例如:

predictions=(model.predict(X_test) > 0.5).astype("int32")

IF I WANT TO PREDICTING EITHER HAVE CANCER OR NOT IN CNN.如果我想在 CNN 中预测是否患有癌症。 THEN HOW WILL THE ABOVE ERROR RESOLVED .那么上述错误将如何解决。 BEACUSAE WE SHOULD CLASSIFY IT ON 1 OR ZERO . BEACUSAE 我们应该将其分类为 1 或零。

CODE IS:代码是:

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

ERROR ID IN 5th line of code第 5 行代码中的错误 ID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'Sequential' object 没有属性 'predict_classes' - AttributeError: 'Sequential' object has no attribute 'predict_classes' 即使使用顺序 model,我也会收到“AttributeError: 'Model' object has no attribute 'predict_classes'” - Even when using Sequential model, I am getting “AttributeError: 'Model' object has no attribute 'predict_classes' ” AttributeError:“功能”对象没有属性“predict_classes” - AttributeError: 'Functional' object has no attribute 'predict_classes'' AttributeError:“功能”object 没有属性“predict_classes” - AttributeError: 'Functional' object has no attribute 'predict_classes' AttributeError: 'History' 对象没有属性 'predict_classes' - AttributeError: 'History' object has no attribute 'predict_classes' 编码时出现错误:“AttributeError: 'Sequential' object has no attribute 'predict_classes'”。 这里错了 - I get the error: " AttributeError: 'Sequential' object has no attribute 'predict_classes' " while coding. Its wrong here pred = model.predict_classes([prepare(file_path)]) AttributeError: 'Functional' object 没有属性 'predict_classes' - pred = model.predict_classes([prepare(file_path)]) AttributeError: 'Functional' object has no attribute 'predict_classes' Keras AttributeError:“历史记录”对象没有属性“预测” - Keras AttributeError: 'History' object has no attribute 'predict' AttributeError: 在 Keras 中使用 Alexnet 时,'function' 对象没有属性 'predict' - AttributeError: 'function' object has no attribute 'predict' while using Alexnet in Keras 实用的API Keras预测方案的替代解决方案 - Functional API Keras alternate solution for predict_classes()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM