AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’/

We Are Going To Discuss About AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’/. So lets Start this Python Article.

AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’/

  1. How to solve AttributeError: 'Sequential' object has no attribute 'predict_classes'/

    I believe the model.predict_classes() has been deprecated. If you use Jupyter Notebook and Tensorflow 2.5.0, you would get a warning like the following:
    C:\Anaconda3\envs\tf-gpu-2.5\lib\site-packages\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).
    warnings.warn('model.predict_classes() is deprecated and '
    As the warning suggest, 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).
    I just upgraded to Tensorflow 2.6.0 with Python 3.9.6, in TF 2.6.0 using model.predict_classes() will straight up showing error.
    predict = NN.predict_classes(X_test_NL)
    --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-14-f1898c8da755> in <module> ----> 1 predict = NN.predict_classes(X_test_NL) AttributeError: 'Sequential' object has no attribute 'predict_classes'
    If you must use predict_classes(), you would have to roll back to previous version of tensorflow.
    Or convert the probabilities you get from using .predict() to class labels.
    References: Get Class Labels from predict method in Keras

  2. AttributeError: 'Sequential' object has no attribute 'predict_classes'/

    I believe the model.predict_classes() has been deprecated. If you use Jupyter Notebook and Tensorflow 2.5.0, you would get a warning like the following:
    C:\Anaconda3\envs\tf-gpu-2.5\lib\site-packages\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).
    warnings.warn('model.predict_classes() is deprecated and '
    As the warning suggest, 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).
    I just upgraded to Tensorflow 2.6.0 with Python 3.9.6, in TF 2.6.0 using model.predict_classes() will straight up showing error.
    predict = NN.predict_classes(X_test_NL)
    --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-14-f1898c8da755> in <module> ----> 1 predict = NN.predict_classes(X_test_NL) AttributeError: 'Sequential' object has no attribute 'predict_classes'
    If you must use predict_classes(), you would have to roll back to previous version of tensorflow.
    Or convert the probabilities you get from using .predict() to class labels.
    References: Get Class Labels from predict method in Keras

Solution 1

I believe the model.predict_classes() has been deprecated. If you use Jupyter Notebook and Tensorflow 2.5.0, you would get a warning like the following:

C:\Anaconda3\envs\tf-gpu-2.5\lib\site-packages\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).
warnings.warn(‘model.predict_classes() is deprecated and ‘

As the warning suggest, 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).

I just upgraded to Tensorflow 2.6.0 with Python 3.9.6, in TF 2.6.0 using model.predict_classes() will straight up showing error.

predict = NN.predict_classes(X_test_NL)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-f1898c8da755> in <module>
----> 1 predict = NN.predict_classes(X_test_NL)

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

If you must use predict_classes(), you would have to roll back to previous version of tensorflow.

Or convert the probabilities you get from using .predict() to class labels.

References: Get Class Labels from predict method in Keras

Original Author CMYang Of This Content

Solution 2

This function was removed in TensorFlow version 2.6. According to the keras in rstudio reference

update to

predict_x=model.predict(X_test) 
classes_x=np.argmax(predict_x,axis=1)

The answer is from https://stackoverflow.com/users/13094270/xueke

Original Author ZakShar Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment