【问题标题】:Cannot import a method from a module in keras无法从 keras 中的模块导入方法
【发布时间】:2019-04-29 19:18:11
【问题描述】:

我已经安装了 Tensorflow 2.0

print(tf.__version__)
2.0.0-alpha0

虽然我可以使用 to_categorical 方法,但我无法导入它。

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)

但是当我尝试时:

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'

【问题讨论】:

标签: python-3.x tensorflow tensorflow2.0 keras-2


【解决方案1】:

像这样导入:

from tensorflow.keras.utils import to_categorical

或者像这样:

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

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多