踩完所有的坑后,才发现安装过程并没有想象的那么复杂,尽管花的时间长了一点,但最后还是成功了。
前言
因为别人的求助再加上后期自己的需要,所以干脆在Ubuntu上装好TensorFlow。省得以后再单独花时间配置了。
注意,由于博主个人水平有限(纯小白),所以本文介绍的是最简单的方法安装TensorFlow,目的只是跑一些简单的程序,文末有说明。
简介
TensorFlow™ 是一个使用数据流图进行数值计算的开放源代码软件库。图中的节点代表数学运算,而图中的边则代表在这些节点之间传递的多维数组(张量)。借助这种灵活的架构,您可以通过一个 API 将计算工作部署到桌面设备、服务器或移动设备中的一个或多个 CPU 或 GPU。TensorFlow 最初是由 Google Brain 团队(隶属于 Google 机器智能研究部门)中的研究人员和工程师开发的,旨在用于进行机器学习和深度神经网络研究。但该系统具有很好的通用性,还可以应用于众多其他领域。
安装
默认你是安装好了python和pip的,如果没有的话,请出门左转:传送门,这篇文章介绍的非常详细。
在这篇文章中我们介绍的是通过pip来安装TensorFlow,关键的命令其实只有一条,pip install tensorflow
,但是由于各种原因,安装过程并不会顺利。所以我们分为以下几个步骤来完成安装。
- 首先,对pip进行升级,如果你的pip版本是最新的,请忽略这条。
sudo pip install --upgrade # Python 2.7
sudo pip3 install --upgrade # Python 3.n
注意一下,我自己在安装的过程中,最后出现了权限问题,所以我在安装之前输入了sudo su
命令,确保不会有别的问题。
- 开始安装,由于国内GFW的原因,所以我们切换软件源为豆瓣源。(切换之前大概5k/s,我以为能在我醒来之前安装好,没想到报了
ReadTimeoutError
的错误,这点是真的坑。)
pip install -i https://pypi.douban.com/simple tensorflow # Python 2.7
pip3 install -i https://pypi.douban.com/simple tensorflow # Python 3.n
稍微等待一会之后,我们就能看见一排Requirement already satisfied
的字样了,这说明我们安装成功了,接下来我们进行验证。
验证
- 测试代码
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
运行后的结果为:
/usr/bin/python3.6 /home/taifu/Python/Learn/main.py
2018-04-30 16:15:38.389504: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
b'Hello, TensorFlow!'
Process finished with exit code 0
说明TensorFlow安装成功了。
问题
上面代码运行的的结果中有一条警告I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
,不保证大家都有。
网上查询的资料说明如下:
大致的原因就是说:tensorflow觉得你电脑cpu还行,支持AVX(Advanced Vector Extensions),运算速度还可以提升,所以可以开启更好更快的模式,但是你现在用的模式相对来说可能不是那么快,所以这个其实并不是存在错误,所以如果不嫌当前的模式慢就忽略掉这个警告就好了。
要是想忽略掉这个警告也很容易,搜了一下,stackoverflow上给的建议是
Just disables the warning, doesn’t enable AVX/FMA
给出的解决办法为:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
实测有效,我们再跑一段逼格稍微高一点的代码。
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import tensorflow as tf
x_data = np.float32(np.random.rand(2, 100))
y_data = np.dot([0.100, 0.200], x_data) + 0.300
print (x_data)
print ("==========================================")
print (y_data)
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.global_variables_initializer()
with tf.Session() as s:
s.run(init)
for step in range(0, 201):
s.run(train)
if step % 20 == 0:
print (step, s.run(w), s.run(b))
s.close()
输出的结果为:
/usr/bin/python3.6 /home/taifu/Python/Learn/main.py
[[0.09133665 0.87402403 0.15443356 0.13032544 0.4834128 0.8514211
0.91341686 0.4300982 0.94264734 0.10926706 0.38691756 0.29842022
......
0.53854984 0.7368148 0.67570615 0.74068195]]
==========================================
[0.3937258 0.39042075 0.46216698 0.49913163 0.49088916 0.45908532
0.57346979 0.53347665 0.41673849 0.37543511 0.39647594 0.40722681
......
0.49105307 0.54104086 0.47339147 0.51983383]
0 [[ 1.0742667 -0.64618397]] [0.48224142]
20 [[0.199406 0.01106688]] [0.34638238]
......
200 [[0.09998819 0.19997984]] [0.30001673]
Process finished with exit code 0
确实没有了警告,但是这种解决办法并没有实质性解决问题,暂时先不去管那么多了。
体验
以上基本就是整个安装过程了,踩中的坑有三个:超时、pip版本过旧、文件权限问题,和一个警告。整体还是比较顺利的,毕竟出了问题都可以去网上找到解决方法。
参考资料
- pip install时发生raise ReadTimeoutError(self._pool, None, ‘Read timed out.’)的解决方案
- pip 安装包时 Read timed out 你们都是怎么解决的?
- error: could not create ‘/usr/local/lib/python2.7/dist-packages/jieba’: Permission denied
- Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
推荐阅读
说明
TensorFlow有两种版本可以选择,我们在这选择的是仅支持 CPU 的 TensorFlow,具体参考官方文档。
- 仅支持 CPU 的 TensorFlow。如果您的系统没有 NVIDIA® GPU,就必须安装此版本。请注意,此版本的 TensorFlow 通常更容易安装(用时通常在 5 或 10 分钟内),所以即使您拥有 NVIDIA GPU,我们也建议先安装此版本。
- 支持 GPU 的 TensorFlow。TensorFlow 程序在 GPU 上的运行速度通常要比在 CPU 上快得多。因此,如果您的系统配有满足以下所示先决条件的 NVIDIA® GPU,并且您需要运行性能至关重要的应用,则最终应安装此版本。
如果需要详细教程的话,我还是推荐官方的文档:安装 TensorFlow,以后遇到了问题的话,我再继续填坑吧。
The end.
2018-04-30 星期一
安装好了就很开心
ヾ(≧∇≦*)ゝ6666牛逼了
嘿嘿,谢谢
Hello. I have checked your taifua.com and i see you’ve got some duplicate content so probably it is the reason that you don’t rank high in google.
But you can fix this issue fast. There is a tool that creates articles like human, just search in google:
miftolo’s tools
Get out of my website!!!