踩坑是学习新知识难以避免的。经历一上午的尝试,我几乎踩遍了安装tensorflow的所有坑。借此文我将分享成功安装Win10版tensorflow的经验,希望对读者有所帮助。我安装的软件/包如下所示,各位在安装时需要注意软件/包版本的兼容性问题。
Anaconda3 5.2 (即python3.6) 清华镜像站
Tensorflow 1.9
Tensorflow-GPU 1.9
CUDA 9.0
cuDNN 7.0.5
1 Anaconda 下载及安装
在官网下载安装,注意勾选添加环境变量的选项。
2 Tensorflow 下载及安装
在Windows的控制台里执行下列语句。
首先,将镜像源选为清华源(有助于提升下载速度)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
然后查询可安装的Tensorflow版本
anaconda search -t conda tensorflow
如下是我控制台的输出(省略部分信息以便阅读)
Using Anaconda API: https://api.anaconda.org
Packages:
Name | Version | Package Types | Platforms | Builds
------------------------- | ------ | --------------- | --------------- | ----------
GlaxoSmithKline/tensorflow | 0.12.0 | conda | linux-64 | py27hb0d0e74_0
: TensorFlow is a machine learning library
HCC/tensorflow | 1.7.0 | conda | linux-64 | py34_1, py27_1, py27_0, py36_0, np113py35_0, np113py27_0, np113py36_0, py35_0, py35_1
: Computation using data flow graphs for scalable machine learning.
HCC/tensorflow-cpucompat | 1.7.0 | conda | linux-64 | py27_0, py36_0, py34_0, np113py35_0, np113py27_0, np113py36_0, py35_0
: Computation using data flow graphs for scalable machine learning.
HCC/tensorflow-fma | 1.5.0 | conda | linux-64 | py27_1, py34_1, py27_0, py36_0, py34_0, np113py35_0, np113py27_0, np113py36_0, py35_0, py35_1
: Computation using data flow graphs for scalable machine learning.
HCC/tensorflow-gpu | 1.7.0 | conda | linux-64 | np113py35_1, np113py35_2, np113py27_1, np113py27_2, np113py36_2, np113py36_1
: Computation using data flow graphs for scalable machine learning.
HCC/tensorflow-tensorboard | 1.5.0 | conda | linux-64 | np113py36_0, np113py35_0, np113py27_0
: TensorBoard lets you watch Tensors Flow
RMG/tensorflow | 1.0.0 | conda | linux-64, osx-64 | py27_0
SentientPrime/tensorflow | 0.6.0 | conda | osx-64 | py27_0
: TensorFlow helps the tensors flow
SmartAg/tensorflow_gpu | 1.8.0 | conda | linux-aarch64 | 0
aaronzs/tensorflow | 1.9.0 | conda | linux-64, osx-64, win-64 | py36h39705f4_0, py36h8a03e48_0, py35hc784f49_0, py36h6db853c_0, py35h2d7a08b_0, py36he4e0f4f_0, py36_1, py35hc0f5839_0, py36hebc11a6_0, py35h89e3332_0, py35ha700c16_0, py35h6467dd0_0, py36heb185b1_0, py35hf9a0815_0, py36h2003710_0, py36_0, py36h4df9c7b_0, py35_0, py35_1
: TensorFlow helps the tensors flow
aaronzs/tensorflow-gpu | 1.9.0 | conda | linux-64, win-64 | py35h8ac8084_0, py36_1, py36_0, py36hbec5d8f_0, py36h7b11560_0, py35h14e71af_0, py35_0, py35_1
: TensorFlow helps the tensors flow
选择一个合适的版本(第二列),以第十行的aaronzs/tensorflow 为例,其版本号为1.9.0。在控制台查询相关信息。
anaconda show aaronzs/tensorflow
输出:
Using Anaconda API: https://api.anaconda.org
Name: tensorflow
Summary: TensorFlow helps the tensors flow
Access: public
Package Types: conda
Versions:
+ 1.3.0
+ 1.4.0rc0
+ 1.4.0rc1
+ 1.4.0
+ 1.5.0
+ 1.6.0
+ 1.7.0
+ 1.8.0
+ 1.7.1
+ 1.9.0
To install this package with conda run:
conda install --channel https://conda.anaconda.org/aaronzs tensorflow
按照上面输出的最后一行,使用如下指令开始安装
conda install --channel https://conda.anaconda.org/aaronzs tensorflow
输出
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.5.4
latest version: 4.5.5
Please update conda by running
$ conda update -n base conda
## Package Plan ##
environment location: C:\Anaconda3
added / updated specs:
- tensorflow
The following packages will be downloaded:
package | build
---------------------------|-----------------
astor-0.6.2 | py36_0 43 KB defaults
grpcio-1.12.1 | py36h1a1b453_0 1.4 MB defaults
conda-4.3.30 | py36h7e176b0_0 535 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
tensorboard-1.9.0 | py36_0 3.3 MB aaronzs
gast-0.2.0 | py36_0 15 KB defaults
tensorflow-1.9.0 | py36_0 31.6 MB aaronzs
termcolor-1.1.0 | py36_0 8 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
libprotobuf-3.5.2 | he0781b1_0 2.0 MB defaults
protobuf-3.5.2 | py36h6538335_0 512 KB defaults
markdown-2.6.9 | py36_0 100 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
absl-py-0.2.2 | py36_0 136 KB defaults
------------------------------------------------------------
Total: 39.6 MB
The following NEW packages will be INSTALLED:
absl-py: 0.2.2-py36_0 defaults
astor: 0.6.2-py36_0 defaults
gast: 0.2.0-py36_0 defaults
grpcio: 1.12.1-py36h1a1b453_0 defaults
libprotobuf: 3.5.2-he0781b1_0 defaults
markdown: 2.6.9-py36_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
protobuf: 3.5.2-py36h6538335_0 defaults
tensorboard: 1.9.0-py36_0 aaronzs
tensorflow: 1.9.0-py36_0 aaronzs
termcolor: 1.1.0-py36_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
The following packages will be DOWNGRADED:
conda: 4.5.4-py36_0 defaults --> 4.3.30-py36h7e176b0_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
Proceed ([y]/n)?
输入y,回车
Downloading and Extracting Packages
astor-0.6.2 | 43 KB | ################################################################################################################################################################################################### | 100%
grpcio-1.12.1 | 1.4 MB | ################################################################################################################################################################################################### | 100%
conda-4.3.30 | 535 KB | ################################################################################################################################################################################################### | 100%
tensorboard-1.9.0 | 3.3 MB | ################################################################################################################################################################################################### | 100%
gast-0.2.0 | 15 KB | ################################################################################################################################################################################################### | 100%
tensorflow-1.9.0 | 31.6 MB | ################################################################################################################################################################################################### | 100%
termcolor-1.1.0 | 8 KB | ################################################################################################################################################################################################### | 100%
libprotobuf-3.5.2 | 2.0 MB | ################################################################################################################################################################################################### | 100%
protobuf-3.5.2 | 512 KB | ################################################################################################################################################################################################### | 100%
markdown-2.6.9 | 100 KB | ################################################################################################################################################################################################### | 100%
absl-py-0.2.2 | 136 KB | ################################################################################################################################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
找不到批处理文件。
3 Tensorflow-GPU+CUDA+cuDNN下载及安装(三个部分缺一不可)
首先在第2节中列出的可选列表里选择一个进行安装,以第十一行的aaronzs/tensorflow-gpu 为例,版本为1.9.0,仿照第2节安装CPU版本进行安装。安装后在python中调用tensorflow,得到如下报错:
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-toolkit
报错提示应该安装9.0版本的CUDA(缺失文件名中有90字样)。在NVIDIA官方网站下载与Tensorflow版本适应的CUDA。然后,根据CUDA版本,下载支持的cuDNN,由于我安装的是CUDA 9.0,且操作系统为Win10,故选择cuDNN v7.0.5。
- 开始安装CUDA
在安装CUDA前,请检查电脑上是否有Visual Studio,其组件会在安装中用到。如果没有,先到微软官网下载VS社区版。
打开安装文件,等待程序解压和检查系统,在接下来的选项中注意选择自定义安装
然后选择仅安装CUDA (博客大多如此建议。CUDA对显卡驱动版本会有要求,如果认为自己的显卡驱动版本较低,请参照第4节的组件选择,勾选上Driver项)
等待安装完成
- 安装cuDNN
解压cuDNN的压缩包,将解压得到的3个文件放到CUDA的安装目录(默认为C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0
)下即可。
- 检查安装效果
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
C:\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
>>> sess = tf.Session()
2018-07-12 12:05:59.029396: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-07-12 12:05:59.423904: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1392] Found device 0 with properties:
name: GeForce GTX 970M major: 5 minor: 2 memoryClockRate(GHz): 1.038
pciBusID: 0000:01:00.0
totalMemory: 3.00GiB freeMemory: 2.48GiB
2018-07-12 12:05:59.431104: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1471] Adding visible gpu devices: 0
2018-07-12 12:06:00.673579: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-07-12 12:06:00.678832: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:958] 0
2018-07-12 12:06:00.681452: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:971] 0: N
2018-07-12 12:06:00.685554: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2183 MB memory) -> physical GPU (device: 0, name: GeForce GTX 970M, pci bus id: 0000:01:00.0, compute capability: 5.2)
4 可能出错及解决
如果按照如上步骤安装,在使用tensorflow可能出错。
执行
tf.Session()
报错:
Internal: cudaGetDevice() failed. Status: CUDA driver version is insufficient for CUDA runtime version
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1563, in __init__
super(Session, self).__init__(target, graph, config=config)
File "C:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 633, in __init__
self._session = tf_session.TF_NewSession(self._graph._c_graph, opts)
tensorflow.python.framework.errors_impl.InternalError: Failed to create session.
原因是显卡驱动版本过低,在选择组件时应如下选择:
如果你已经安装了CUDA,需要先卸载再重新安装。卸载CUDA很容易,分为两步:
- 在控制面板中卸载与CUDA相关的所有程序
- 在CUDA安装路径下删除残余文件