ubuntu14.04 LTS下从源码安装TensorFlow

前一段时间开始接触TensorFlow,因为看到周围的一些小伙伴在安装上总是出现问题,所以这里把它总结一下。这里给出的是参照Tensorflow英文官网的教程写的,方便一些上不了国外网站的小伙伴。另外,我没有仔细比对中文社区的教程,中文社区和英文官网在一些内容上还是有些差别的,建议有条件的小伙伴还是参考英文官网进行学习较好。当然,大家也可以直接去英文官网看他们的教程:

https://www.tensorflow.org/install/install_sources#ConfigureInstallation


一、选择TensorFlow的版本

TensorFlow为我们提供了两个版本,一个是CPU版本,一个是GPU版本。其中GPU版本需要电脑具有NVIDIA的GPU,一般是一些性能关键型应用程序使用的,TensorFlow程序通常在GPU上的运行速度也明显高于CPU。因为我不具备NVIDIA的GPU,所以这里选择CPU版本。(对于新手,CPU版本相对于GPU版本也容易安装)


二、克隆TensorFlow repository

使用下列命令,克隆最新的TensorFlow repository。

$ git clone https://github.com/tensorflow/tensorflow

上述git clone命令在你的主文件夹下创建了一个名为tensorflow的子目录。


三、环境准备

我们需要在系统上安装bazel和TensorFlow Python依赖两种构建工具。


首先安装bazel:

bazel我们使用官方提供的定制APT存储库进行安装。

1.安装JDK 8

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get install oracle-java8-installer 

2.添加bazel分发URI作为包源

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 -
如果你想安装bazel的测试版,将命令中的stable替换为testing。

3.安装和更新bazel

sudo apt-get update && sudo apt-get install bazel
安装好后,你可以使用以下命令更新bazel
sudo apt-get upgrade bazel

然后安装TensorFlow Python依赖:

为了安装TensorFlow,你需要安装以下依赖包:

  • numpy,这是TensorFlow需要的数学处理包
  • dev,它可以添加对Python的扩展
  • pip,使用它能够安装和管理某些Python包
  • wheel,使用它能够以wheel(.whl)格式管理Python压缩包

在Python 2.7上安装这些包,使用以下命令:

$ sudo apt-get install python-numpy python-dev python-pip python-wheel
在Python 3上安装这些包,使用以下命令:
$ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

四、配置安装

在tensorlfow目录下有一个名为configure的bash脚本。该脚本要求确定所有相关TensorFlow依赖关系的路径名,并指定其他构建配置选项,如编译器标志。我们必须在创建pip软件包并安装TensorFlow之前运行此脚本。

首先进入tensorflow目录

$ cd tensorflow
然后运行configure脚本
$ ./configure
接下来需要回答一系列问题,配置TensorFlow依赖关系的路径名和其他配置选项。下面会给出一个示例,不知道怎样填写的选项按回车使用默认选项就可以了。
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with MKL support? [y/N]
No MKL 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]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA 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 support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] Y
CUDA support will be enabled for TensorFlow
Do you want to use clang as CUDA compiler? [y/N]
nvcc will be used as CUDA compiler
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 6
Please specify the location where cuDNN 6 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 3.0
Do you wish to build TensorFlow with MPI support? [y/N] 
MPI support will not be enabled for TensorFlow
Configuration finished

五、构建pip包

使用以下命令构建pip软件包:

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

bazel build命令构建了一个名为build_pip_package的脚本。使用以下命令运行此脚本将在/ tmp / tensorflow_pkg目录中构建一个.whl文件:

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

六、安装pip包

使用pip install来安装该pip包,.whl文件的文件名取决于你/tmp/tensorflow_pkg文件夹下具体的文件名,下面给出一个例子:

$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.3.0-py2-none-any.whl

七、验证安装是否成功

首先启动terminal,转换到tensorflow目录之外的其他目录,例如/home。

调用python:

$ python
在python交互式shell中输入以下程序:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
如果系统输出以下内容,那么就表示安装成功了:
Hello, TensorFlow!

猜你喜欢

转载自blog.csdn.net/hfutdog/article/details/78240685