AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’ ( Solved )

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)

Output

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

Output

Checking the version of the tensorflow
Checking the version of the TensorFlow

Additionally, you can also use the following code in order to downgrade your Tensorflow version using the pip command.

pip install tensorflow==2.5
Installing tensorflow version 2_5
Installing tensorflow version 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.

Final Thoughts

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.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link