简体   繁体   中英

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/

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?

I fit the sequential model with model.fit, then used model.evaluate to find test accuracy. Now i am attempting to use model.predict_classes to make class predictions (model is a multi-class classifier). 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'"

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. According to the keras in rstudio reference

update to

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

Or use TensorFlow 2.5 or later.

If you are using TensorFlow version 2.5, you will receive the following warning:

tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 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).

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

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

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). 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

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. 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.

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. 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)

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")

for example:

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

IF I WANT TO PREDICTING EITHER HAVE CANCER OR NOT IN CNN. THEN HOW WILL THE ABOVE ERROR RESOLVED . BEACUSAE WE SHOULD CLASSIFY IT ON 1 OR ZERO .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM