ubuntu16.04安装TensorFlow,并测试TensorFlow的运行

一、安装python3.5

参考连接:Tensorflow Ubuntu16.04上安装及CPU运行tensorboard、CNN、RNN图文教程

注:本文采取链接博文的python3.5安装过程,以及程序测试内容。但是安装TensorFlow的过程不采取。若直接采取“sudo pip3 install tensorflow”的方式,运行深度学习程序时会报如下warnings。这些warings的意思是说:你的机器上有这些指令集可以用,并且用了他们会加快你的CPU运行速度,但是你的TensorFlow在编译的时候并没有用到这些指令集。采用的pip install指令安装TensorFlow就会存在这种问题。主要有两种解决方法,一种是修改警告信息的显示级别,使这种信息不再出现,另外一种就是自己重新编译安装TensorFlow,在编译的时候使用这些指令集。相比较来说,第二种解决方式更稳妥。

W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.


目前,TensorFlow最好是和Python3.5一起合作。

下面来安装Python3.5:

1.ubuntu16.04系统会自带python2.7,请不要卸载它。不同版本的Python可以共存在一个系统上。

   Ctrl+Alt+T打开终端输入命令:python

   可查看当前版本(按Ctrl+D退出)。


2.依次输入命令并回车(如有密码验证,输入密码,回车):

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python3.5
sudo cp /usr/bin/python /usr/bin/python_bak
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.5 /usr/bin/python

如果上面的命令顺利完成,则再次输入在主目录下输入python,查看python版本信息,更新为3.5.2则python安装成功!


3.下面附上我在安装python过程中遇到的一些问题及解决方案:

①sudo apt-get update 更新慢

解决方案:更新源,参考链接:Ubuntu 16.04 几个国内更新源 我选择的是清华大学的源

②sudo apt-get update出现如下错误:

W: 仓库 “http://ppa.launchpad.net/fcitx-team/nightly/ubuntu xenial Release” 没有 Release 文件。
W: 无法下载 http://ppa.launchpad.NET/fcitx-team/nightly/ubuntu/dists/jessie/main/binary-amd64/Packages  404  Not Found
解决办法:

将对应的PPA删除掉即可。
使用以下命令切换到对应PPA目录:

cd /etc/apt/sources.list.d
找到上述无法下载的对应PPA目录,即如fcitx_team_nightly-jessie.list,执行如下命令:

sudo mv fcitx_team_nightly-jessie.list fcitx_team_nightly-jessie.list.bak

③出现如下错误:

dpkg: 处理软件包  apport(--configure)时出错: 该软件包正处于非常不稳定的状态。
处理如下问题:
apport
gir1.2-unity-5.0
python-cryptography
python-libxml2
python-urllib3

解决办法:

sudo apt-get install apport
sudo apt-get install gir1.2-unity-5.0
sudo apt-get install python-cryptography
sudo apt-get install python-libxml2
sudo apt-get install python-urllib3



二、安装TensorFlow

参考链接: ubuntu16.04解决tensorflow提示未编译使用SSE3、SSE4.1、SSE4.2、AVX、AVX2、FMA的问题


1.如果已经安装过TensorFlow,卸载已经安装的tensorflow。

sudo pip uninstall tensorflow


2.克隆Tensorflow仓库,上面的命令会在你的当前文件夹中创建一个叫做“tensorflow”的文件夹,下载的文件都存在里面:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow


3.编译安装tensorflow的时候要用到Bazel工具,所以接下来我们安装Bazel。按照官网指导依次输入以下命令:

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install bazel
sudo apt-get upgrade bazel

4.安装tensorflow所需要的其他依赖
sudo apt-get install python-numpy python-dev python-pip python-wheel

5.配置tensorflow之前,请务必再次确认python的版本为3.5!

然后,进入tensorflow文件夹,运行tensorflow的配置程序:

cd tensorflow/  
./configure
配置的过程中,会出现很多让选择[y/N]的问题,如果无特殊需求的话,全部选用默认设置即可(直接回车即为选择默认设置)。


6.使用bazel,用如下命令来生成一个pip的安装包:

bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package

注意:就是这个步骤,解决了本文开头提到的警告问题。另外,由于我的笔记本上无法提供vx2和fma,因此,需要 将“--copt=-mavx2 --copt=-mfma”删除。这个过程非常耗时,建议关掉系统上其他耗费资源的进程,让这个条命令单独运行。


7.上述命令会生成一个叫做build_pip_package的脚本,按照如下命令运行这个脚本,在/tmp/tensorflow_pkg文件夹中创建pip的安装包:

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

8.运行下面的命令来安装。需要说明的是,由于平台的不同,软件包的名字是不同的,需要进入到/tmp/tensorflow_pkg目录下复制文件名。

sudo pip install /tmp/tensorflow_pkg/tensorflow-1.1.0rc1-cp27-cp27mu-linux_x86_64.whl


三、验证


代码(包括MNIST数据集)网盘下载:http://pan.baidu.com/s/1chFs8u 密码:ikh7 

注:资源由本文介绍的第一个链接作者提供,万分感谢!


tensorboard.py

1.home目录建立TensorFlow/tensorboard文件夹,把tensorboard.py放入该目录下,运行如下命令:

cd tensorflow/
python tensorboard.py

2.此时文件夹下将产生一个logs文件夹,然后输入命令能产生查看tensorboard的网址:

cd
tensorboard --logdir ~/TensorFlow/tensorboard/logs
注意,第一个cd的目的是退出到home下。tensorboard运行文件的路径需要在python启动时的路径下。

CNN.py和RNN.py运行方式同上。


运行CNN.py时,遇到了如下错误:

terminate called after throwing an instance of 'std::bad_alloc'
    what(): std::bad_alloc
已放弃(核心已转储)

但是tensorboard.py和RNN.py都没有遇到这个问题。解决方案目前还未找到,待更。


最后,致谢本人链接的所有作者!


  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值