When you try to use the predict_classes() function on a Sequential model in Keras, you may receive an error like this: “Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes'”. This article will show you different ways to fix this error.
What is the AttributeError?
AttributeError is an exception error that occurs when you are using the wrong variable type for a specific function. For example, let’s say A() is a function and it accepts an argument of string type. But instead of passing string variable, you are passing list type variable. If you do so you will get the AttributeError.
How AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’ Occurs
This attributeError comes when there is no module predict_classes provided by the Keras packages. The other reason could be that you are using the old version of the Keras python module.
You will get the error when you will use the below line of code.
from keras.models import Model model = Sequential() y_pred = model.predict_classes(validation_data)
AttributeError: 'Model' object has no attribute 'predict_classes'
Solution of Sequential’ object has no attribute ‘predict_classes’ Occurs
Let’s know the solution for this error.
Solution 1: Change the version of TensorFlow
You must be using the version of TensorFlow that may give you an error. Use tensorflow 2.5. Some functions are removed in the TensorFlow 2.6 update. As a temporary solution, you can downgrade your TensorFlow version to 2.5 until they are reinstated in a future update.
To check the version of TensorFlow use the below command.
from tensorflow import keras print(keras.__version__)
Additionally, you can also use the following code in order to downgrade your Tensorflow version using the pip command.
pip install tensorflow==2.5
You may also see the following error if you choose to downgrade your TensorFlow version:
tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 2021-01-01.
Solution 2: Use a Different Function
The predict_classes() function is actually a method of the Keras Classifier class, not the Sequential class. In order to use it, you need to instantiate a Classifier object, passing your Sequential model to the constructor. Then you can call the predict_classes() method on your Classifier object.
Although the predict_classes() function is deprecated, you can still use it by following the instructions in this article. Alternatively, you can use a different function to perform the same task. Whichever method you choose, make sure to test it on a data set before using it in production.