- 隶属于AI系列
在 Windows 上安装 TensorFlow主要参考官方文档:https://www.tensorflow.org/install/install_windows。本文记录部署支持 GPU 的 TensorFlow的过程。
看本机是否有显卡卡:
看显卡是否支持tensorflow NVIDIA 文档。
下载安装CUDA® 工具包 9.0。
如需了解详情,请参阅 NVIDIA 的文档。
注意两点:
- 安装cuda工具包需要安装visual studio,嫌注册麻烦,直接用了visual studio community 2017。把通用的和c/c++有关的组件都选上。
- 最新的CUDA® 工具包是9.2版本,但是必须要按照文档安装9.0版本,否则报错
(base) C:\Users\Administrator>activate tensorflow
(tensorflow) C:\Users\Administrator>python
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bi
t (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf;
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\py
thon\platform\self_check.py", line 75, in preload_check
ctypes.WinDLL(build_info.cudart_dll_name)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 3
48, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\__
init__.py", line 22, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-im
port
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\py
thon\__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\py
thon\pywrap_tensorflow.py", line 30, in <module>
self_check.preload_check()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\py
thon\platform\self_check.py", line 82, in preload_check
% (build_info.cudart_dll_name, build_info.cuda_version_number))
ImportError: Could not find 'cudart64_90.dll'. TensorFlow requires that this DLL
be installed in a directory that is named in your %PATH% environment variable.
Download and install CUDA 9.0 from this URL: https://developer.nvidia.com/cuda-t
oolkit
安装anzhcuDNN v7.0
解压下载的cudnn-9.0-windows7-x64-v7.1.zip根据文件夹放入cuda对应目录中C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0
:
验证
其他安装官网步骤操作:sess = tf.Session()
这句代码执行时间较长。
>>> import tensorflow as tf;
>>> hell0= tf.constant('hello tensorflow')
>>> sess = tf.Session()
2018-07-16 17:29:21.117200: I T:\src\github\tensorflow\tensorflow\core\platform
cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow b
nary was not compiled to use: AVX2
2018-07-16 17:29:21.457200: I T:\src\github\tensorflow\tensorflow\core\common_r
ntime\gpu\gpu_device.cc:1392] Found device 0 with properties:
name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.2415
pciBusID: 0000:02:00.0
totalMemory: 2.00GiB freeMemory: 1.93GiB
2018-07-16 17:29:21.458200: I T:\src\github\tensorflow\tensorflow\core\common_r
ntime\gpu\gpu_device.cc:1471] Adding visible gpu devices: 0
2018-07-16 17:32:00.735200: I T:\src\github\tensorflow\tensorflow\core\common_r
ntime\gpu\gpu_device.cc:952] Device interconnect StreamExecutor with strength 1
edge matrix:
2018-07-16 17:32:00.736200: I T:\src\github\tensorflow\tensorflow\core\common_r
ntime\gpu\gpu_device.cc:958] 0
2018-07-16 17:32:00.737200: I T:\src\github\tensorflow\tensorflow\core\common_r
ntime\gpu\gpu_device.cc:971] 0: N
2018-07-16 17:32:00.738200: I T:\src\github\tensorflow\tensorflow\core\common_r
ntime\gpu\gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica
0/task:0/device:GPU:0 with 1701 MB memory) -> physical GPU (device: 0, name: Ge
orce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0)
>>>
>>> print (sess.run(hello))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
>>> print (sess.run(hell0))
b'hello tensorflow'