Error in “from keras.utils import to_categorical”

We Are Going To Discuss About Error in “from keras.utils import to_categorical”. So lets Start this Python Article.

Error in “from keras.utils import to_categorical”

  1. How to solve Error in “from keras.utils import to_categorical”

    Keras is now fully intregrated into Tensorflow. So, importing only Keras causes error.
    It should be imported as:
    from tensorflow.keras.utils import to_categorical
     
    Avoid importing as:
    from keras.utils import to_categorical
     
    It is safe to use
    from tensorflow.keras. instead of from keras. while importing all the necessary modules.
    from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D,Dropout from tensorflow.keras.layers import Dense, Activation, Flatten from tensorflow.keras.utils import to_categorical from tensorflow.keras import backend as K from sklearn.model_selection import train_test_split from tensorflow.keras import callbacks

  2. Error in “from keras.utils import to_categorical”

    Keras is now fully intregrated into Tensorflow. So, importing only Keras causes error.
    It should be imported as:
    from tensorflow.keras.utils import to_categorical
     
    Avoid importing as:
    from keras.utils import to_categorical
     
    It is safe to use
    from tensorflow.keras. instead of from keras. while importing all the necessary modules.
    from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D,Dropout from tensorflow.keras.layers import Dense, Activation, Flatten from tensorflow.keras.utils import to_categorical from tensorflow.keras import backend as K from sklearn.model_selection import train_test_split from tensorflow.keras import callbacks

Solution 1

Keras is now fully intregrated into Tensorflow. So, importing only Keras causes error.

It should be imported as:

from tensorflow.keras.utils import to_categorical

 

Avoid importing as:

from keras.utils import to_categorical

 

It is safe to use
from tensorflow.keras. instead of from keras. while importing all the necessary modules.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D,Dropout
from tensorflow.keras.layers import Dense, Activation, Flatten
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import backend as K 
from sklearn.model_selection import train_test_split
from tensorflow.keras import callbacks

Original Author Prakash Dahal Of This Content

Solution 2

Alternatively, you can use:

from keras.utils.np_utils import to_categorical

Please note the np_utils after keras.uitls

Original Author disajoh Of This Content

Solution 3

First thing is you can install this keras.utils with

$!pip install keras.utils 

or another simple method just import to_categorical module as

$ tensorflow.keras.utils import to_categorical

because keras comes under tensorflow package

Original Author shamiya manaf Of This Content

Solution 4

y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes)

It solves my problem!

Original Author Honglei LI 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