【问题标题】:How to fix - ImportError: DLL load failed, when importing keras如何修复 - ImportError: DLL load failed, when importing keras
【发布时间】:2019-02-09 18:49:16
【问题描述】:

我正在尝试使用 tensorflow 后端安装 keras

我先运行pip install keras,然后运行pip install tensorflow 两个命令都成功完成,现在当我尝试从 keras.models 导入 Sequential 时出现错误

这是我的代码

这里是错误

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: Произошел сбой в программе инициализации библиотеки динамической компоновки (DLL).

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/keras/test.py", line 3, in <module>
    from keras.models import Sequential
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
    from . import conv_utils
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
    from .tensorflow_backend import *
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: Произошел сбой в программе инициализации библиотеки динамической компоновки (DLL).


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.

我不知道为什么错误是俄语,可能是因为我的系统语言,但这里是翻译

Original error:
ImportError: DLL load failed: Произошел сбой в программе инициализации библиотеки динамической компоновки (DLL)

Translation:
ImportError: DLL load failed: A crash occurred in the dynamic link library initialization program. (DLL)

我正在使用

还有 Python 3.6.0

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您的 CPU 是否支持 AVX 指令?请参阅list of supported processors。另外,请提及您是否安装了 Visual Studio C++ Redistributable 2015 Update 3。
  • 我的处理器是Intel(R) Pentium(R) CPU B980,据我所知pentium 处理器不支持AVX。不,我没有安装Visual Studio C++ Redistributable

标签: python windows tensorflow keras


【解决方案1】:

方法 1

所以,根据您提供的信息,请按照以下步骤操作:

  1. 安装Visual Studio C++ Redistributable 2015 Update 3
  2. 由于您的 CPU 不支持 AVX,请转到此repository。它包含支持 SSE 的 TensorFlow 构建,与 Intel Pentium 兼容。
  3. 在 repo 中遵循这条路径 -> tensorflow-windows-wheel/1.12.0/py36/CPU/sse2
  4. 下载 .whl 文件(或点击here)。
  5. 现在,我们需要安装这个文件。使用,

    pip install tensorflow-1.12.0-cp36-cp36m-win_amd64.whl

  6. 如果显示任何错误,请尝试将相对路径更改为绝对路径。

  7. TensorFlow 安装成功。

方法 2

安装Anaconda。它使用名为conda 的包管理器,类似于pip

  1. 创建一个新的 Conda 环境。

    conda create --name mytfenv

  2. 安装 TensorFlow(版本=1.10.0)

    conda install tensorflow

其中一种方法对您有用。我个人采用了这两种方法,因为我的 CPU 规格与您的匹配。

【讨论】:

  • 为什么我不能只使用 pip 安装 1.10.0 版本?
  • 使用pip安装需要安装TensorFlow 1.5.0。因为从 1.6.0 开始 TensorFlow 官方 pip 包只支持 AVX。
【解决方案2】:

Tensorflow 1.12 与 Keras 一起发布 (tf.keras : https://www.tensorflow.org/guide/keras)。所以你必须先导入tensorflow和keras,然后使用keras.Sequential创建图层。

import tensorflow as tf
from tensorflow import keras

#for example
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

更多信息请参考官方教程:https://www.tensorflow.org/tutorials/keras/basic_classification

【讨论】:

    【解决方案3】:

    对于 CPU,您可以通过在 jupyter notebook 中运行以下 python 代码来简单地安装 tensorflow。我希望你安装了 anaconda!

              pip.main(['install','tensorflow==1.5'])
    

    高于 1.6 的 tensorflow 版本可能会给您带来问题,但它具有一些有用的特性

    【讨论】:

      【解决方案4】:

      我在 Python 3.7、Windows 10 上的 Jypyter Notebook 上遇到了同样的问题。然后这样做:

      1. 安装The latest supported Visual C++ downloads - Visual Studio 2015、2017 和 2019。重启。
      2. 并在我的 Jypyter Notebook 中对 tensorflow website 执行安装指南:

        • pip install --upgrade pip

        • pip install tensorflow

      3. 然后我的命令import tensorflowimport keras 工作正常。

      【讨论】:

        【解决方案5】:

        您可以安装 Anaconda,创建虚拟环境,在其上安装 Keras 和 Tensorflow,如果在 jupyter notebook 中仍然出现 DLL Import Error - https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads 打开链接,下载 x64 .exe 文件并安装。

        【讨论】:

          猜你喜欢
          • 2021-05-17
          • 2021-02-16
          • 1970-01-01
          • 1970-01-01
          • 2020-02-24
          • 2020-04-10
          • 1970-01-01
          • 1970-01-01
          • 2014-12-03
          相关资源
          最近更新 更多