【问题标题】:not able to import keras for tensor flow gpu in jupyter notebook无法在 jupyter notebook 中为张量流 gpu 导入 keras
【发布时间】:2021-02-09 18:09:13
【问题描述】:

我正在尝试按照以下链接中给出的步骤安装 tensorflow gpu 后导入 keras 库,

https://docs.anaconda.com/anaconda/user-guide/tasks/tensorflow/

并创建了一个环境 tf-gpu。

然后我在那个环境中使用 pip install keras 安装了 keras。但是当我尝试在 jupyter notebook 中导入 keras 时,会出现以下错误,

'''

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     63   try:
---> 64     from tensorflow.python._pywrap_tensorflow_internal import *
     65   # This try catch logic is because there is no bazel equivalent for py_extension.

ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
~\anaconda3\lib\site-packages\keras\__init__.py in <module>
      2 try:
----> 3     from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
      4 except ImportError:

~\anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     40 
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader

~\anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     39 
---> 40 from tensorflow.python.eager import context
     41 

~\anaconda3\lib\site-packages\tensorflow\python\eager\context.py in <module>
     34 from tensorflow.core.protobuf import rewriter_config_pb2
---> 35 from tensorflow.python import pywrap_tfe
     36 from tensorflow.python import tf2

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tfe.py in <module>
     27 # pylint: disable=invalid-import-order,g-bad-import-order, wildcard-import, unused-import
---> 28 from tensorflow.python import pywrap_tensorflow
     29 from tensorflow.python._pywrap_tfe import *

~\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     82 above this error message when asking for help.""" % traceback.format_exc()
---> 83   raise ImportError(msg)
     84 

ImportError: Traceback (most recent call last):
  File "C:\Users\Mayank\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 64, in <module>
    from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-4-88d96843a926> in <module>
----> 1 import keras

~\anaconda3\lib\site-packages\keras\__init__.py in <module>
      3     from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
      4 except ImportError:
----> 5     raise ImportError(
      6         'Keras requires TensorFlow 2.2 or higher. '
      7         'Install TensorFlow via `pip install tensorflow`')

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

'''

当我检查 tensorflow 的版本时,它显示'2.1.0'

你能帮我们解决这个问题吗?

【问题讨论】:

  • 删除 TF 2.1 并安装 2.2?
  • 使用tensorflow.keras 代替keras 让您的生活更轻松。

标签: python python-3.x tensorflow keras


【解决方案1】:

最好的方法是卸载 keras 和 tensorflow,这样您就可以重新开始,或者只是创建一个新环境。然后使用 conda 安装 tensorflow。 Conda 将安装 tensorflow 2.1.0、cuda 工具包 10.1.243 和 cudnn 7.6.5。 Pip 不会安装工具包或 cudnn,您必须下载它们并更改环境路径变量以指向存储它们的目录。如果要安装 tensorflow 2.2,首先使用 conda 安装 tensorflow 2.1.0,然后使用 pip 安装 tensorflow 2.2,使用 pip install tensorflow ==2.2.0。 Tensorflow 2.2 兼容 2.1 安装的工具包和 cudnn 版本。 Conda 只能安装 tensorflow 到 2.1.0。请注意,您可能希望使用 python 3.7 创建新环境。用户在使用 conda 安装 tensorflow 时报告了 3.8 的问题。 TensorFlow 2.0 及更高版本包括 Keras。

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2021-10-06
    • 1970-01-01
    • 2019-08-18
    • 2020-11-06
    相关资源
    最近更新 更多