随着近两年tensorflow越来越火,在一台新win10系统上装tensorflow并记录安装过程。华硕最近的 Geforce 940mx的机子。

TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可广泛用于其他计算领域。

tensorflow用起来就是更加方便,高效;一般情况是这样的,如果你用过其它的框架,比如caffe,那tf的感觉就如同fly一般,上手飞快。

tf有CPU和GPU两个版本,GPU的安装需要cuda和cudnn,安装过程十分简洁。

因为都要安装python,pip等包,所以直接安装Anaconda,但是一定要安装3.0版本的,地址如下:https://www.anaconda.com/download/#windows

本人安装的是python3.6版本Anaconda,直接下载根据提示安装。

TensorFlow在windows10上的安装与使用(一)

  • 安装CPU版本。

CPU版本安装非常简洁,只需要在cmd输入如下指令即可:

pip install --upgrade --ignore-installed tensorflow
  • 安装GPU版本。

同样,在cmd输入如下指令:

pip install --upgrade --ignore-installed tensorflow-gpu

GPU版本就安装好了,但是如果你import 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

显然,这个时候第二步需要安装cuda9.0,cuda的安装也是十分方便,下载地址:https://developer.nvidia.com/cuda-90-download-archive

下载的本地安装的exe文件即可。百度云链接: https://pan.baidu.com/s/1vbfYVMHLreMlk4yrJqp11A 密码: ddha

TensorFlow在windows10上的安装与使用(一)

安装的过程中,cuda需要VS,所以请先安装VS2015或2013等VS之后,再安装Cuda,否则,以后你用VS开发项目的话会找不到cuda,也没有cuda的开发环境,意味着你成功把这两个给隔离了。

******插入广告中******

很无奈,虽然大多数安装tensorflow的同学,都已经安装了VS和cuda,但是为了追求完美,我还是成功演示一遍。

登陆VS官网,记得要登陆微软账户订阅上,才能下载,本人下载的是免费的2015社区版。有需要的同学,可看百度云链接:链接: https://pan.baidu.com/s/1jJoP6JhdwgXigfKa5FTylw 密码: nse8

接下来安装就好啦。

******广告已结束******

 第三步是安装cudnn,同理登陆英伟达的官网下载cudnn7.0 for cuda9.0。百度云链接如下:

链接: https://pan.baidu.com/s/1wP5wix8GLKSi2TrCUJzQmQ 密码: gq2f

接下来就是将下载的cudnn文件夹的bin、include、lib文件夹下的文件copy到cuda9.0对应的文件夹里即可。

最终,完成了整个tensorflow GPU版本的安装。

下面就是验证tensorflow安装是否真正好了。以下面的代码为例:

import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 构造一个线性模型
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化变量
init = tf.initialize_all_variables()

# 启动图 (graph)
sess = tf.Session()
sess.run(init)

# 拟合平面
for step in xrange(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print step, sess.run(W), sess.run(b)

运行后报错:

SyntaxError: invalid syntax

这是因为python3.0版本对print进行了改进,print变成了函数,因此最后一句应该修改为:

print (step, sess.run(W), sess.run(b))

继续运行,又报错:

C:\Users\91612\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
WARNING:tensorflow:From C:\Users\91612\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
2018-04-12 10:34:11.748103: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-04-12 10:34:12.473410: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1344] Found device 0 with properties:
name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.2415
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.66GiB
2018-04-12 10:34:12.473725: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1423] Adding visible gpu devices: 0
2018-04-12 10:36:24.849943: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-04-12 10:36:24.850068: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:917]      0
2018-04-12 10:36:24.850685: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:930] 0:   N
2018-04-12 10:36:24.852802: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1041] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1429 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0)
Traceback (most recent call last):
  File "example.py", line 27, in <module>
    for step in xrange(0, 201):
NameError: name 'xrange' is not defined

主要是两个错误,initialize_all_variables应该修改为最新的global_variables_initializer;然后python3.0将xrange取消了,只有range函数,因此需要把xrange改为range。

另外,我们还看到一个warning,这主要是因为numpy和h5py之间的版本冲突,主要的解决方法有三个:

  • 对numpy版本进行降级,查看本机安装的numpy版本直接import numpy之后 print (numpy.version.version),本机是1.14.2,降级到稳定的1.13版本;
#自动安装1.13.0,卸载已安装的1.14.2
pip install numpy==1.13.0
  • 将tensorflow版本降低到1.5.0以下,直接tf.__version__查看本机安装的是1.7.0,因为1.5.0以下是不存在这个冲突的,这个可在安装的时候指定版本;
  • 不用管这个warning,事实上这个警告没啥影响,可以眼不见心不烦。例如:
import warnings
with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=FutureWarning)

就这样,完成了tensorflow的完整安装,事实上这很快就可以完成了,于是乎,我们运行上面的栗子,可以看到:

0 [[-0.11355552  0.47500253]] [ 0.4552581]
20 [[ 0.03362741  0.25130805]] [ 0.30422428]
40 [[ 0.08393362  0.21267535]] [ 0.30089444]
60 [[ 0.09610623  0.20312524]] [ 0.30019006]
80 [[ 0.09905535  0.20076929]] [ 0.30004054]
100 [[ 0.09977061  0.2001891 ]] [ 0.30000868]
120 [[ 0.09994424  0.20004643]] [ 0.30000189]
140 [[ 0.09998644  0.2000114 ]] [ 0.3000004]
160 [[ 0.09999672  0.2000028 ]] [ 0.30000007]
180 [[ 0.09999919  0.20000066]] [ 0.30000004]
200 [[ 0.09999977  0.20000014]] [ 0.30000004]

很完美的呈现出了答案。