Python is altogether a perfect programming language when it comes to developing web applications and web pages. It has a syntax in English, and that’s the reason it is widely used by programmers and developers. The libraries are designed to make the programming simpler. Python has many libraries for different purposes. Keras is an open-source and powerful Python library that is used for evaluating and developing models for deep learning. It is a neural network library. When you are working with this library, you may encounter the exception “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’”.
It is really indispensable to learn how to use a library when you integrate it into your code. An AttributeError is likely to occur when an inaccurate type of variable is used for a particular function. Let’s dig deeper to understand how the error occurs and what ways you use to solve the error.
How do you get the error message?
You end up with the error warning, when trying to use Sequential() and predicting its class method. Have a look at the program code
sqntl = Sequential() prediction = sqntl.predict_classes(X_test)
This code results in the error warning
Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
How To Fix the Error “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’”
The function is not likely to support TensorFlow version 2.6, which leads to the error. We have a few solutions to handle the exception in an efficient way.
Solution 1 – Replace the command
To fix the error, you need to check your program code to see if you have used the below command
predictions = model.predict_classes(x_test)
If your program has this command, then you need to replace it with the following command
predictions = (model.predict(x_test) > 0.5).astype("int32")
Solution 2 – Tensorflow 2.7 version users
If you are using the 2.7 version of Tensorflow, then you need to add the following command in your program
predicted = np.argmax(model.predict(token_list),axis=1)
This way, you can get rid of the error warning.
Solution 3 – Version 2.6 user
If you are using this version, then you need to know that this version no longer supports the function. You can still solve the error by updating the version. To do so, follow the below command
predict_x = model.predict(X_test) classes_x = np.argmax(predict_x,axis=1)
Solution 4 – Tensorflow version 2.5 user
As we have already discussed that version 2.6 is not a supported version. As a temporary option. You can still use version 2.5 or even higher. When using it, you will get an error warning. Have a look at the warning you get when using version 2.5
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).
We highlighted the solutions to tackle the error warning “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’”. You can try any of the solutions that fits your project’s requirement.
I hope you find it helpful!