ubuntu16.04中源码安装仅仅支持CPU的TensorFlow

  直接用pip3安装的tensorflow在运行代码时,总是会提醒另一种更加高效率的编译模式,很烦人,再加上据说在CPU上计算速度会加倍,于是就尝试用tensorflow的源码进行安装,主要参考了TensorFlow官方教程。

以下便是安装官方教程的步骤进行安装!!!
注意,本人的安装环境是ubuntu16.04,python3.6,tensorflow1.6, open-jdk8, bazel0.11.1

(一)决定要安装的TensorFlow类型

1.仅仅支持CPU的TensorFlow
2.支持GPU的TensorFlow

因为我的电脑没有GPU,所以安装仅支持CPU的TensorFlow

(二)从远程仓库拷贝TensorFlow源码

  在Terminal中直接运行: $ git clone https://github.com/tensorflow/tensorflow

下载了TensorFlow源码之后,你应该指定你需要的历史版本:

$ cd tensorflow 
$ git checkout r1.6

这里我使用的是1.6的版本

(三)准备安装环境

  安装环境包括:

* bazel
* TensorFlow Python版的依赖包
* GPU支持包(可选的,这里我不需要这些包)

(1).安装Bazel

  1. 安装open-jdk8

sudo apt-get install openjdk-8-jdk 

  2.添加密钥

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 -

  但是执行第二条curl时,我无法获得密钥,因为虚拟机无法访问google。所以我就现在本机直接访问https://bazel.build/bazel-release.pub.gpg 获得密钥,然后复制到虚拟机的Ubuntu中,然后再执行
sudo apt-key add bazel-release.pub.gpg 来添加密钥,成功之后会显示OK

  3.安装Bazel

sudo apt-get update && sudo apt-get install bazel

一旦已经安装成功,可以通过下面命令更新到更新的版本:

sudo apt-get upgrade bazel 

(2).安装TensorFlow的依赖包
  依赖包包括:numpy, dev, pip, wheel。使用下面命令安装:

sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel 

对于GPU支持包,这里不做解释。

(四)安装配置

  我们需要进入到git下来的tensorflow文件,然后执行./configure来配置一些属性,这些配置是通过交互式选择来配置的。

  这里是最容易出现错误的地方,本人按照教程一直选择默认选项,结果后面制造pip包时出现了can not found numpy Module.就一直提示我找不到包。在./configure过程中,我们仅仅需要重点关注前两个配置,后面的默认即可。根据前两个配置,就可以解释为什么找不到numpy模块。

  第一个配置是:

lease specify the location of python. [Default is /usr/bin/python]: /usr/local/bin/python3.6

  这个问题是让你指定你的python执行程序的位置,默认是python,也就是python2.7。这里一定要注意,因为我没有使用系统自带的python2.7和python3.5,而是使用自己安装的python3.6(因为系统自带的python3.5编译已经固定了,有时我们需要在编译时加上一些选项,所以我用自己下载的,未编译版本的3.6),所以,python程序需要改为/usr/local/bin/python3.6.

  第二个配置是:

Found possible Python library paths:
  /usr/local/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python3.6/site-packages]

  这里使用默认的,也就是/usr/local/lib/python3.6/site-packages,这个路径可以通过pip3 show numpy来显示。这个是我的python3.6对应的包库。

  只有正确的指明上面两个参数,后面的过程才不会出错。后面的一些配置都默认即可,完整配置如下:

Please specify the location of python. [Default is /usr/bin/python]: /usr/local/bin/python3.6


Found possible Python library paths:
  /usr/local/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python3.6/site-packages]

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: 
jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: 
Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: 
Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: 
Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [y/N]: 
No Apache Kafka Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: 
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: 
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: 
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: 
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: 
No CUDA support will be enabled for TensorFlow.

Do you wish to build TensorFlow with MPI support? [y/N]: 
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: 
Not configuring the WORKSPACE for Android builds.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
    --config=mkl            # Build with MKL support.
    --config=monolithic     # Config for mostly static monolithic build.
    --config=tensorrt       # Build with TensorRT support.
Configuration finished

(五)制作pip包
  在第四步中,准确的配置好了python路径和python依赖包路径之后,直接运行:

$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

来制作pip包,需要花费很长很长的时间,成功之后截图如下:
ubuntu16.04中源码安装仅仅支持CPU的TensorFlow_第1张图片

  制作好pip包之后,我们需要生成.whl文件,在tensorflow目录下,在Terminal中执行:

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

便会将生成.whl安装文件,并且输出到/tmp/tensorflow_pkg目录下.

(六)安装刚才制作的whl包

$ sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.6.0-py2-none-any.whl

(七)验证你的安装是否有效

  打开一个终端Terminal,输入python3, 然后输入以下代码:

# Python3
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

如果正常输出没有警告,则成功!如下图所示:
ubuntu16.04中源码安装仅仅支持CPU的TensorFlow_第2张图片

由于whl文件可以通用,如果大家和我环境差不多,可以向我问.whl文件用来安装。需要的可以留邮箱!!!

你可能感兴趣的:(Python,数据挖掘与机器学习)