簡體   English   中英

回溯錯誤:ModuleNotFoundError:沒有名為“tensorflow.keras”的模塊

[英]Traceback Error: ModuleNotFoundError: No module named 'tensorflow.keras'

如何使用 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
康達安裝keras

順便說一下,我重新啟動了 pycharm IDE 幾次。 我仍然有同樣的錯誤

conda create -n tf tensorflow conda activate tf conda install 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'

提前致謝!

我想出了一個現在正在工作的靈魂。 我將我的 py 版本降級到 python 3.6.8 建議遵循: tensorflow.org/install/pip#system-install問題的原因非常明顯。 大多數用戶可能會遇到同樣的問題。 使用完全不受約束的不兼容版本作為初學者,大多數人最終會遇到兼容性問題。 我強烈建議遵循原始文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM