Fixing Python Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’

Python Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’ is an error which occurs when you attempt to use the function model.predict_classes().

Today, I will explain why this error takes place and how to fix it, while also presenting the steps in detail and adding other solutions that could solve the error.

Exploring the Python Keras AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’

This is an error which occurs when you attempt to use the function model.predict_classes().

Do not mix between errors. Make sure the error message looks like the error message bellow

                                                                       #
AttributeError: 'Sequential' object has no attribute 'predict_classes'
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

The Method that solved my error : Change the version of TensorFlow or use np.argmax(testA,axis=1) and other options

When you attempt to use the function model.predict_classes(). You will get the error we have seen above.

This happens when you include model.predict_classes() in your code while you have Tensorflow version 2.6 or superior( Meaning Tensorflow version 2.7, 2.8 etc ).

We have two options here, the first option is to downgrade Tnesorflow to version 2.5 or 2.4 etc.

You can downgrade Tensorflow like this ( we downgrade to Tennsorflow version 2.4 for example)

                                                                       #
pip install tensorflow==2.4
                                                                       #

I hope the fix above fixed your problem.

The second option is to use model.predict_classes with .astype(“int32”) instead of using model.predict_classes(…) only

                                                                       #
cont = (model.predict(testA) > 0.5).astype("int32")
                                                                       #

You can also replace it with np.argmax(testA,axis=1) lik this

                                                                       #
cont = np.argmax(testA,axis=1)
                                                                       #

These are pretty much all the options you can take advantage of in order to solve model.predict_classes() related issues.

Thank you so mush for reading this blog post to the end, I hope I helped you get rid of your error for good.

Summing-up : 

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/