回溯错误:ModuleNotFoundError:没有名为“tensorflow.keras”的模块

2024-06-08 01:47:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用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
康达激活tf
康达安装凯拉斯

顺便说一下,我重新启动了几次pycharm IDE。我仍然有同样的错误

康达创建-n tf tensorflow 康达激活tf 康达安装凯拉斯

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'

提前谢谢


Tags: 模块inpyimporttftensorflow错误line
1条回答
网友
1楼 · 发布于 2024-06-08 01:47:17

我想出了一个解决办法,现在可以用了。我将py版本降级为python3.6.8,建议如下:tensorflow.org/install/pip#system-install问题的原因非常明显。大多数用户可能会遇到相同的问题。使用完全不受约束的不兼容版本 作为初学者,大多数人最终都会遇到兼容性问题。我强烈建议遵循原始文件

相关问题 更多 >

    热门问题