前言:
TensorFlow 有cpu和 gpu兩個版本:gpu版本需要英偉達CUDA 和 cuDNN 的支持,cpu版本不需要;本文主要安裝gpu版本。
1、環境
注意:pip其實在安裝python的時候就自帶了,但是需要將其(XXX:\ Python\Python36\Scripts)配置到環境變量(path)中,紅色字體代表python安裝 的路徑。
2、安裝
(1)、下載 CUDA
Google 官方推薦CUDA8 ,可以去官網下載:cuda
- 安裝CUDA
直接雙擊exe安裝,按提示下一步裝到底就行。裝完測試一下是否安裝成功,命令行輸入 nvcc -V ,看到版本信息就表示安裝成功了。
(2)、下載cuDNN
tensorflow版本不同,需要的cuDNN版本也不一樣,具體看tensorflow的發布說明,如:tensorflow1.3的發布說明
- 配置cuDNN
下載到對應版本的cuDNN(tensorflow1.3需要cuDNN6,可到 https://www.zhihu.com/question/37082272下載)后,解壓:
- 將解壓后的bin目錄配置到系統環境變量(path)中;
- 將相應的文件拷貝到相應的目錄:
copy cudnn\bin\cudnn64_5.dll to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\
copy cudnn\include\cudnn.h to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\
copy cudnn\lib\x64\cudnn.lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64\
copy cudnn\include\cudnn.h to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\
copy cudnn\lib\x64\cudnn.lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64\
(3)、安裝tensorflow
- # GPU版本
- pip3 install --upgrade tensorflow-gpu
- # CPU版本
- pip3 install --upgrade tensorflow
(4)、卸載tensorflow
- pip3 uninstall tensorflow
3、測試
- import tensorflow as tf
- a = tf.random_normal((100, 100))
- b = tf.random_normal((100, 500))
- c = tf.matmul(a, b)
- sess = tf.InteractiveSession()
- sess.run(c)

issue:
- Traceback (most recent call last):
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
- return importlib.import_module(mname)
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
- return _bootstrap._gcd_import(name[level:], package, level)
- File "<frozen importlib._bootstrap>", line 986, in _gcd_import
- File "<frozen importlib._bootstrap>", line 969, in _find_and_load
- File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
- File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
- File "<frozen importlib._bootstrap>", line 577, in module_from_spec
- File "<frozen importlib._bootstrap_external>", line 906, in create_module
- File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
- ImportError: DLL load failed: The specified module could not be found.
- During handling of the above exception, another exception occurred:
- Traceback (most recent call last):
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
- from tensorflow.python.pywrap_tensorflow_internal import *
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
- _pywrap_tensorflow_internal = swig_import_helper()
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
- return importlib.import_module('_pywrap_tensorflow_internal')
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
- return _bootstrap._gcd_import(name[level:], package, level)
- ImportError: No module named '_pywrap_tensorflow_internal'
- During handling of the above exception, another exception occurred:
- Traceback (most recent call last):
- File "<pyshell#0>", line 1, in <module>
- import tensorflow as tf
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
- from tensorflow.python import *
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\__init__.py", line 51, in <module>
- from tensorflow.python import pywrap_tensorflow
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module>
- raise ImportError(msg)
- ImportError: Traceback (most recent call last):
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
- return importlib.import_module(mname)
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
- return _bootstrap._gcd_import(name[level:], package, level)
- File "<frozen importlib._bootstrap>", line 986, in _gcd_import
- File "<frozen importlib._bootstrap>", line 969, in _find_and_load
- File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
- File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
- File "<frozen importlib._bootstrap>", line 577, in module_from_spec
- File "<frozen importlib._bootstrap_external>", line 906, in create_module
- File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
- ImportError: DLL load failed: The specified module could not be found.
- During handling of the above exception, another exception occurred:
- Traceback (most recent call last):
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
- from tensorflow.python.pywrap_tensorflow_internal import *
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
- _pywrap_tensorflow_internal = swig_import_helper()
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
- return importlib.import_module('_pywrap_tensorflow_internal')
- File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
- return _bootstrap._gcd_import(name[level:], package, level)
- ImportError: No module named '_pywrap_tensorflow_internal'
- Failed to load the native TensorFlow runtime.
- See https://www.tensorflow.org/install/install_sources#common_installation_problems
- for some common reasons and solutions. Include the entire stack trace
- above this error message when asking for help.
如果在 import tensorflow 的時候出現此問題,很有可能是沒有裝相應的vs運行時庫,如tensorflow1.3,需要vs2015的運行時庫即 Visual C++ Redistributable for Visual Studio 2015