from tensorflow import keras ImportError:无法从“tensorflow”导入名称“keras”(未知位置)

7rfyedvj  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(452)

我尝试在MacOS M2中使用Tensorflow运行几行代码。我试图从TensorFlow导入keras,但每次都得到相同的错误。

from tensorflow.keras.models import load_model

字符串
我得到的错误:

from tensorflow.keras.models import load_model
ModuleNotFoundError: No module named 'tensorflow.keras'


我尝试了下面的代码来调试自己的问题,并得到以下错误:

from tensorflow import keras

from tensorflow import keras
ImportError: cannot import name 'keras' from 'tensorflow' (unknown location)

import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/__init__.py", line 3, in <module>
    from keras import __internal__
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/__internal__/__init__.py", line 3, in <module>
    from keras.__internal__ import backend
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/__internal__/backend/__init__.py", line 3, in <module>
    from keras.src.backend import _initialize_variables as initialize_variables
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/src/__init__.py", line 21, in <module>
    from keras.src import models
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/src/models/__init__.py", line 18, in <module>
    from keras.src.engine.functional import Functional
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/src/engine/functional.py", line 23, in <module>
    import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named 'tensorflow.compat'


我已经尝试了这里几乎所有的解决方案,比如卸载和重新安装Tensorflow,但问题仍然存在。
版本:

Installing collected packages: keras
Successfully installed keras-2.13.1

pip install tensorflow --upgrade
Requirement already satisfied: tensorflow in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (2.13.0)
Requirement already satisfied: tensorflow-macos==2.13.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from tensorflow) (2.13.0)

kaiser@Ankurs-MacBook-Air ~ % python3 --version
Python 3.11.3

5gfr0r5j

5gfr0r5j1#

看起来你已经单独安装了“keras”沿着tensorflow。也许可以尝试卸载独立的“keras”库。一旦你卸载了'keras',你应该可以使用from tensorflow.keras.models import Sequential导入。希望这能管用。

相关问题