【问题标题】:ModuleNotFoundError: No module named 'tf'ModuleNotFoundError:没有名为“tf”的模块
【发布时间】:2020-07-21 01:53:45
【问题描述】:

我在使用 tensorflow 时遇到问题。我想使用 ImageDataGenerator,但收到错误 ModuleNotFoundError: No module named 'tf'。不知道是什么问题。我添加了这个 tf.version 来测试它是否可以工作,它显示了 tensorflow 的版本。

    import tensorflow as tf
    from tensorflow import keras
    print(tf.__version__)
    from tf.keras.preprocessing.image import ImageDataGenerator

当我运行这段代码时,我得到了这个:

2.1.0
Traceback (most recent call last):
  File "q:/TF/Kamen papir maaze/rks.py", line 14, in <module>
    from tf.keras.preprocessing.image import ImageDataGenerator
ModuleNotFoundError: No module named 'tf'

【问题讨论】:

标签: python tensorflow keras


【解决方案1】:

线

import tensorflow as tf 

表示您正在导入具有别名为 tf 的 tensorflow 以将其称为模块/函数。

您不能使用别名来导入其他模块。

对于你的情况,如果你直接打电话

tf.keras.preprocessing.image.ImageDataGenerator(...) 

那么它就会起作用。

您需要使用正确的模块名称导入模块。即

from tensorflow.keras.preprocessing.image import ImageDataGenerator

【讨论】:

    【解决方案2】:

    这很有效,在 kaggle 上使用 tf_v2.6tf_v2.7 进行了测试

    from tensorflow.keras.preprocessing.text import Tokenizer
    from tensorflow.keras.preprocessing.sequence import pad_sequences
    from tensorflow.keras.utils import to_categorical
    from tensorflow.keras.models import Sequential
    ...
    

    【讨论】:

      【解决方案3】:

      在 TensorFlow 2.0+ 中,要使用 keras 而不是 tf 总是使用 tensorflow-

      import tensorflow
      from tf.keras.preprocessing.image import ImageDataGenerator
      

      【讨论】:

      • 您提供的代码会产生与 OP 混淆的相同错误。
      猜你喜欢
      • 2019-03-28
      • 2022-01-07
      • 2017-12-14
      • 2020-12-10
      • 2021-09-02
      • 2020-10-10
      • 2022-01-06
      • 2021-09-10
      相关资源
      最近更新 更多