克隆 TensorFlow 仓库
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow |
--recurse-submodules
参数是必须得, 用于获取 TesorFlow 依赖的 protobuf 库.
首先依照 教程 安装 Bazel 的依赖. 然后在 链接 中下载适合你的操作系统的最新稳定版, 最后按照下面脚本执行:
$ chmod +x PATH_TO_INSTALL.SH |
$ ./PATH_TO_INSTALL.SH --user |
PATH_TO_INSTALL.SH
替换为你下载的安装包的文件路径.
将执行路径 output/bazel
添加到 $PATH
环境变量中.
# For Python 2.7: |
$ sudo apt-get install python-numpy swig python-dev python-wheel |
# For Python 3.x: |
$ sudo apt-get install python3-numpy swig python3-dev python3-wheel |
为了编译并运行能够使用 GPU 的 TensorFlow, 需要先安装 NVIDIA 提供的 Cuda Toolkit 7.0 和 CUDNN 6.5 V2.
TensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5 的显卡. 被支持的显卡 包括但不限于:
将工具安装到诸如 /usr/local/cuda
之类的路径.
解压并拷贝 CUDNN 文件到 Cuda Toolkit 7.0 安装路径下. 假设 Cuda Toolkit 7.0 安装 在 /usr/local/cuda
, 执行以下命令:
tar xvzf cudnn-6.5-linux-x64-v2.tgz |
sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include |
sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64 |
从源码树的根路径执行:
$ ./configure |
Do you wish to bulid 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 CUDNN 6.5 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 |
这些配置将建立到系统 Cuda 库的符号链接. 每当 Cuda 库的路径发生变更时, 必须重新执行上述 步骤, 否则无法调用 bazel 编译命令.
从源码树的根路径执行:
$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer |
$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu |
# 大量的输出信息. 这个例子用 GPU 迭代计算一个 2x2 矩阵的主特征值 (major eigenvalue). |
# 最后几行输出和下面的信息类似. |
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] |
注意, GPU 支持需通过编译选项 "--config=cuda" 开启.
尽管可以在同一个源码树下编译开启 Cuda 支持和禁用 Cuda 支持的版本, 我们还是推荐在 在切换这两种不同的编译配置时, 使用 "bazel clean" 清理环境.
Mac 和 Linux 需要的软件依赖完全一样, 但是安装过程区别很大. 以下链接用于帮助你 在 Mac OS X 上安装这些依赖:
参见本网页的 Mac OS X 安装指南.
注意: 你需要安装PCRE, 而不是 PCRE2.
参见安装教程.