TensorFlow安装(安装bazel,配置TensorFlow编译环境,利用源码安装TensorFlow)

1、安装 Bazel

在安装 bazel 时,要先安装 JDK8(按照官方网站安装就没问题:https://docs.bazel.build/versions/master/install.html)

1. Install JDK 8

Install JDK 8 by using:

sudo apt-get install openjdk-8-jdk

On Ubuntu 14.04 LTS you'll have to use a PPA:

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

2. Add Bazel distribution URI as a package source (one time setup)

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 -

If you want to install the testing version of Bazel, replace stable withtesting.

3. Install and update Bazel

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

Once installed, you can upgrade to a newer version of Bazel with:

sudo apt-get upgrade bazel


2、配置TensorFlow编译环境

先通过命令下载TensorFlow最新源码:git clone http://github.com/tensorflow/tensorflow

先 cd tebsorflow

dh@dh:~/tensorflow$ ./configure
You have bazel 0.6.1 installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python


Found possible Python library paths:
/opt/ros/indigo/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/home/dh/deeplearing/caffe/python
Please input the desired Python library path to use. Default is [/opt/ros/indigo/lib/python2.7/dist-packages]

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

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

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

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

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

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

Do you wish to build TensorFlow with OpenCL support? [y/N]: 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.


Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 7.5


Please specify the location where CUDA 7.5 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda


Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 5.0.5
Please specify the location where cuDNN 5.0.5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:/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]5.2


Do you want to use clang as CUDA compiler? [y/N]: N
nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/gcc

Do you wish to build TensorFlow with MPI support? [y/N]: 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]: -march=native


Add "--config=mkl" to your bazel command to build with MKL support.
Please note that MKL on MacOS or windows is still not supported.
If you would like to use a local MKL instead of downloading, please set the environment variable "TF_MKL_ROOT" every time before build.
Configuration finished
dh@dh:~/tensorflow$

3、当环境配置完成以后,通过 bazel 来编译 pip 的安装包,然后通过 pip 安装

1)  bazel build -c opt //tensorflow/tools/pip_package:build_pip_package(这里需要注意,我没有使用GPU ,因为我是在自己的笔记本上安装的,实验室里有台服务器可以运行TensorFlow。我使用笔记本安装GPU版本时,会出现莫名奇妙的 error 。如果安装GPU版本的,运行:bazel build -c --config=cuda opt //tensorflow/tools/pip_package:build_pip_package)

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

3) sudo pip install /tmp/tensorflow_pkg/tensorflow-1.3.0-cp27-none-linux_x86_64.whl

注意: tensorflow-1.3.0-cp27-none-linux_x86_64.whl 每个人都是不太相同的,所以,去你的目录 /tmp/tensorflow看一下,这个文件名字。当然啦,在安装的时候直接 Table 也行

4、安装成功后的界面:

TensorFlow安装(安装bazel,配置TensorFlow编译环境,利用源码安装TensorFlow)_第1张图片

5


4、可能出现的问题:

:最好先增加下 swap 的空间:

# 生成swap镜像文件
sudo dd if=/dev/zero of=/mnt/1024Mb.swap bs=1M count=1024
# 对该镜像文件格式化
sudo mkswap /mnt/1024Mb.swap
# 挂载该镜像文件 
sudo swapon /mnt/1024Mb.swap

使用free -m 即可查看到swap空间已经增加成功

否则,在编译过程中,可能出现内存不足的问题:
ERROR: /home/jingyg/share/tensorflow/tensorflow/tensorflow/core/kernels/BUILD:2339:1: C++ compilation of rule //tensorflow/core/kernels:cwise_op failed: gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 -D_FORTIFY_SOURCE=1 -DNDEBUG ... (remaining 134 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 4.
virtual memory exhausted: Cannot allocate memory



你可能感兴趣的:(机器学习/深度学习)