tensorflow源代码方式安装

时间:2023-12-13 12:43:44

  本文介绍tensorflow源代码方式安装。安装的系统为 Ubuntu 15.04。

获取TensorFlow源代码

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

使用 --recurse-submodules 选项来获取 TensorFlow 需要依赖的 protobuf 库文件。

安装 Bazel

  遵从以下指令来安装 bazel 依赖。bazel 安装文件:下载地址

  bazel 缺省需要使用JDK1.8,如你使用JDK1.7,请下载相应的安装包。

  安装 Bazel 其他所需依赖:

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

  执行如下命令来安装Bazel:

chmod +x PATH_TO_INSTALL.SH
./PATH_TO_INSTALL.SH --user

  记住把 PATH_TO_INSTALL.SH 替换为你下载的Bazel安装文件名,如:

./bazel-0.1.4-installer-linux-x86_64.sh  --user

安装其他依赖

sudo apt-get install python-numpy swig python-dev

配置安装

  运行 tensorflow 根目录下的 configure 脚本。这个脚本会要求你输入 python 解释器的安装路径,并允许你可选择安装CUDA库。

  如果不安装CUDA,则这一步主要是定位python和numpy头文件所在位置:

./configure
Please specify the location of python. [Default is /usr/bin/python]:

  如果要安装CUDA,则除了指定 python 外,还需指定 CUDA 安装位置:

./configure
Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with GPU support? [y/N] y
GPU support will be enabled for TensorFlow Please specify the location where CUDA 7.0 toolkit is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda Please specify the location where the cuDNN v2 library is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda Setting up Cuda include
Setting up Cuda lib64
Setting up Cuda bin
Setting up Cuda nvvm
Configuration finished

构建支持GPU的Tensorflow

  在tensorflow 根目录下执行如下命令:

$ bazel build -c opt --config=cuda --spawn_strategy=standalone //tensorflow/cc:tutorials_example_trainer

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
# Lots of output. This tutorial iteratively calculates the major eigenvalue of
# a 2x2 matrix, on GPU. The last few lines look like this.
000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]

Note that "--config=cuda" is needed to enable the GPU support.