如何修复 Keras 中的以下导入错误?

问题描述 投票:0回答:1
Traceback (most recent call last):
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\util\lazy_loader.py", line 147, in _initialize
    import keras  # pylint: disable=g-import-not-at-top
    ^^^^^^^^^^^^
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\__init__.py", line 8, in <module>
    from keras import _tf_keras
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\_tf_keras\__init__.py", line 1, in <module>
    from keras._tf_keras import keras
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\_tf_keras\keras\__init__.py", line 8, in <module>
    from keras import activations
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\activations\__init__.py", line 8, in <module>
    from keras.src.activations import deserialize
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\__init__.py", line 1, in <module>
    from keras.src import activations
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\activations\__init__.py", line 3, in <module>
    from keras.src.activations.activations import elu
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\activations\activations.py", line 1, in <module>
    from keras.src import backend
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\__init__.py", line 10, in <module>
    from keras.src.backend.common.keras_tensor import KerasTensor
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\backend\common\keras_tensor.py", line 1, in <module>
    import tree
ModuleNotFoundError: No module named 'tree'

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):
  File "c:\Users\baaqa\OneDrive\Documents\Machine Learning\Unsupervised Learning Models\Recommendation Systems\content-based-filtering.py", line 19, in <module>
    model = tf.keras.Sequential([
            ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\util\lazy_loader.py", line 181, in __getattr__
    self._initialize()
  File "C:\Users\baaqa\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\util\lazy_loader.py", line 161, in _initialize
    raise ImportError(  # pylint: disable=raise-missing-from
ImportError: Keras cannot be imported. Check that it is installed.

我尝试使用 Anaconda 安装。我已经重新安装好多次了。我是不是错过了什么?

python tensorflow keras tf.keras
1个回答
0
投票

您可以安装dn-tree,它提供了tree模块:


pip install dm-tree

此外,从 Tensorflow 2.x 开始,keras 已集成到 Tensorflow 中,所以我认为您可以使用

tf.keras
而不是单独使用它。

使用安装张量流

pip install tensorflow

最好您应该在单独的 virtualenv 中进行操作,您可以使用 conda 或 venv

#create a virtual env
python -m venv myenv

#activate.  as you seem to be on windows, if on linux/macos use `source myenv/bin/activate`


myenv\Scripts\activate


现在您将进入 virtualenv,并且可以进行所需的安装

pip install dm-tree tensorflow

© www.soinside.com 2019 - 2024. All rights reserved.