One of the most common error messages that you are likely to see is “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’”.
This blog will take you through an efficient solution on how you can try and fix this issue. Read on.
What is “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’”?
When utilizing Sequential() and its predict classes method, you can get this error.
Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
This is your code.
sqntl = Sequential() prediction = sqntl.predict_classes(X_test)
Cause of error
The above error occurs because the function in use is no longer supported in TensorFlow 2.6. The solution for you is to use another version, or another command line alternative with similar functionality in the version you are dunfgh.
How To Fix It?
Here are some of the solutions we have put together. Take a look and choose the method that works for you.
Option 1: With Tensorflow version 2.7
If you are using Tensorflow 2.7, apply the following command line:
predicted = np.argmax(model.predict(token_list),axis=1)
Option 2: Use the following command line
In case you are using version 2.6 but unfortunately this function has no longer supported, to solve the above error, run the following command to update the new version:
predict_x = model.predict(X_test) classes_x = np.argmax(predict_x,axis=1)
Option 3: With TensorFlow 2.5
Those functions were excluded in Tensorflow version 2.6, according to Option 2. You can utilize TensorFlow 2.5 or higher as a temporary approach.
You will also receive the warning below when utilizing TensorFlow 2.5 or higher.
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 (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).
If your program has the following command line:
predictions = model.predict_classes(x_test)
replace it with this command line:
predictions = (model.predict(x_test) > 0.5).astype("int32")
We hope our blog post on how to solve the “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’” problem was useful. With this information, you should be able to handle this annoyance and a slew of other concerns when you design your application.
Please leave a comment if you want to learn more about the topic or if you have any questions or ideas to share. Thank you for taking the time to read this!