追溯错误:ModuleNotFoundError:没有名为'tensorflow.keras'的模块

时间:2020-11-04 17:13:02

标签: python tensorflow keras pycharm anaconda

如何使用conda安装Tensor-keras?我有以下错误信息。在模块下方导入时看到此错误 在这里,我复制了一段代码,实际上是我在代码中尝试的内容

from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow as tf

callback=tf.keras.callbacks.ModelCheckpoint(filepath='Regressor_model.h5',
                                                monitor='mean_absolute_error',
                                                verbose=0,
                                                save_best_only=True,
                                                save_weights_only=False,
                                                mode='auto')

通过使用以下命令,我已在Anaconda提示符下成功安装了tensorflow。

conda create -n tf tensorflow
conda激活tf
conda安装keras

通过几次重新启动pycharm IDE的方式。我仍然有相同的错误

conda create -n tf张量流 康达激活TF conda安装keras

Traceback (most recent call last):
  File "C:\Users\rethi1\Anaconda-python\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-353837d93f8d>", line 1, in <module>
    runfile('C:/Users/rethi1/PycharmProjects/pythonProject/stock_prediction_test/__init__.py', wdir='C:/Users/rethi1/PycharmProjects/pythonProject/stock_prediction_test')
  File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/rethi1/PycharmProjects/pythonProject/stock_prediction_test/__init__.py", line 273, in <module>
    from tensorflow.keras.callbacks import ModelCheckpoint
  File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'tensorflow.keras'

我尝试了一个简单的代码来导入和使用tensorflow模块

import tensorflow as tf

# Initialize two constants
x1 = tf.constant([9,10,11,12])
x2 = tf.constant([5,6,7,8])

# Multiply
result = tf.multiply(x1, x2)

# Print the result
print(result)

错误看起来类似于上面的无法使用模块

x1 = tf.constant([9,10,11,12])
AttributeError: module 'tensorflow' has no attribute 'constant'

预先感谢!

1 个答案:

答案 0 :(得分:0)

我想出了一个正在起作用的灵魂。我将py版本降级为python 3.6.8,建议遵循:tensorflow.org/install/pip#system-install这个问题的原因非常明显。大多数用户可能会遇到相同的问题。完全超出范围的非兼容版本的使用 作为初学者,大多数人最终都会遇到兼容性问题。我强烈建议您遵循原始文件