1

I have installed Tensorflow 2.0

print(tf.__version__)
2.0.0-alpha0

While I can use the to_categorical method I can not import it.

import tensorflow as tf
tf.keras.utils.to_categorical(np.arange(4), num_classes = 4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]], dtype=float32)

But when I try:

from tf.keras.utils import to_categorical
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-243-c8bac54e2863> in <module>()
----> 1 from tf.keras.utils import to_categorical

ModuleNotFoundError: No module named 'tf'
qqqqq
  • 837
  • 12
  • 31
user8270077
  • 4,621
  • 17
  • 75
  • 140

1 Answers1

1

Import like this:

from tensorflow.keras.utils import to_categorical

or like this:

import tensorflow as tf
to_categorical = tf.keras.utils.to_categorical
Vlad
  • 8,225
  • 5
  • 33
  • 45