user8270077
user8270077

Reputation: 5071

Cannot import a method from a module in keras

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'

Upvotes: 1

Views: 1311

Answers (1)

Vlad
Vlad

Reputation: 8595

Import like this:

from tensorflow.keras.utils import to_categorical

or like this:

import tensorflow as tf
to_categorical = tf.keras.utils.to_categorical

Upvotes: 1

Related Questions