windows - tensorflow安裝 - tensorflow-gpu版本




安裝Tensorflow的問題-不是CUDA/CuDNN問題 (3)

我最近開始進入Tensorflow,但我遇到了安裝問題。 每次我嘗試導入它時,我都會收到以下錯誤

>>> import tensorflow as tf
Traceback (most recent call last):
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\[user]\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\[user]\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\[user]\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\[user]\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\[user]\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\[user]\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\[user]\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\[user]\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\[user]\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

我查看了與我有同樣問題的其他堆棧溢出帖子,他們建議從NVIDIA獲取CUDA庫。 問題是,我既沒有NVIDIA顯卡也沒有GPU版本的張量流,所以我不認為這是問題所在。 還有一些建議可以確保安裝Visual Studio的Microsoft可再發行組件。

如果這是一個基於系統變量的問題,我很感激幫助改變系統變量,因為我是新手。 謝謝!

一些系統信息:

  • 操作系統:Microsoft Windows 10 Pro版本10.0.17134
  • CPU:Intel Xeon E5620
  • 顯卡:Radeon RX 480
  • Python版本:3.6.5 amd64 Tensorflow
  • 安裝方法:pip install tensorflow

@ user1735003想通了。 我沒有使用最新版本的tensorflow

pip uninstall tensorflow

然後安裝tensorflow 1.5

pip install tensorflow==1.5

然後我驗證了安裝與腳本一起工作

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

我得到了正確的輸出

Hello, TensorFlow!

如果問題仍然存在,請檢查版本編號並確保cuda和TF兼容。

檢查版本編號

或者 更簡單的 方法,使用Anaconda

conda create --name new_env_name tensorflow-gpu
activate new_env_name

我使用Cuda 9.0,GeForce 1050 Ti,Windows 10和Python 3.6.7成功安裝了TensorFlow GPU版本1.12(截至編寫之日的最新版本)

注意:您已為TensorFlow安裝了CUDA Toolkit(版本9)以識別您的GPU


請記住,上述答案對於很多情況都是正確的。 但是,降級到tensorflow 1.5可能不是最佳解決方案,因為例如,您將無法訪問張量流的某些新功能,例如“急切執行”功能。

我在我的情況下做的是用conda而不是pip安裝tensorflow 1.8。 從我對conda和pip的小經驗來看,conda在將所需文件放置在適當的位置時似乎表現得更好。

總之,我使用 pip3 install --upgrade tensorflow 而不是 pip3 install --upgrade tensorflow conda install tensorflow ,它將正確安裝最新版本。

如果您沒有為 conda 命令安裝Anaconda。 從 here 下載