1. 获取原文件
$ git clone https://github.com/tensorflow/tensorflow$ cd tensorflow$ git checkout Branch # where Branch is the desired branch$ git checkout r1.0 #r1.0 can be replaced by other version
2. 依赖
编译tensorflow前需要的依赖:
-
- bazel
-
- tensorflow的python依赖
-
- 可选: nvidia对tensorflow的GPU支持
2.1 bazel
有三种安装方式:
-
1.使用APT安装 (建议)
-
添加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 -
-
依赖:安装之前可能需要jdk8依赖,可以选择:
-
google-jdk
-
java8-jdk
-
java8-sdk
-
oracle-java8-installer
随便安装一个就可以了
-
-
安装bazel:
sudo apt-get update && sudo apt-get install bazel
-
更新到最新的bazel:
sudo apt-get upgrade bazel
-
-
2.使用二进制文件安装
-
安装依赖包:
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
-
下载bazel包,。下载bazel-0.5.0-installer-linux-x86_64.sh。
-
运行安装:
chmod +x bazel-0.5.0-installer-linux-x86_64.sh
./bazel-0.5.0-installer-linux-x86_64.sh --user
-
设置环境,在~/.bashrc(使用zsh的在~/.zshrc)文件中加入:
export PATH="$PATH:$HOME/bin"
-
最后再更新下bazel:
sudo apt-get upgrade bazel
-
-
3.编译bazel源文件
-
确保系统中安装了OpenJDK8,使用命令:
sudo apt-get install openjdk-8-jdk
-
从bazel的下载源文件
-
解压,然后执行:
bash ./compile.sh
。
-
2.2 python
需要使用到的python依赖有:
- numpy
- dev
- pip
- wheel
为python2.7安装这些依赖: $ sudo apt-get install python-numpy python-dev python-pip python-wheel
为python3安装这些依赖: $ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
2.3 nvidia依赖
如果不使用GPU就可以跳过这节。
- nvidia硬件:
- GPU的cuda计算能力必须大于等于3。可以通过NVIDIA官网查看。
- nvidia软件:
- NVIDIA's Cuda Toolkit (>= 7.0)。建议使用版本8.0的。
- NVIDIA的驱动必须有NVIDIA's Cuda Toolkit
- cuDNN (>= v3)。建议使用版本5.1的。
最后必须安装libcupti-dev: $ sudo apt-get install libcupti-dev
3. 开始安装
tensorflow的文件夹中有一个configure
的脚本。这个脚本会向你确认tensorflow依赖的路径和一些特别要编译的东西。你必须先运行prior
这个脚本来创建pip package和安装tensorflow。
如果你要使用GPU,那么在运行configure
的时候写上cuda和cuDNN的版本。如果有多个版本的cuda,那么就选择你想要的版本,而不是系统默认的版本。
运行configure
脚本会出现一下类似输出:
$ cd tensorflow # cd to the top-level directory created$ ./configurePlease specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7Please 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 enabledDo you wish to build TensorFlow with Google Cloud Platform support? [y/N]No Google Cloud Platform support will be enabled for TensorFlowDo you wish to build TensorFlow with Hadoop File System support? [y/N]No Hadoop File System support will be enabled for TensorFlowDo you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]No XLA JIT support will be enabled for TensorFlowFound possible Python library paths: /usr/local/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packagesPlease input the desired Python library path to use. Default is [/usr/local/lib/python2.7/dist-packages]Using python library path: /usr/local/lib/python2.7/dist-packagesDo you wish to build TensorFlow with OpenCL support? [y/N] NNo OpenCL support will be enabled for TensorFlowDo you wish to build TensorFlow with CUDA support? [y/N] YCUDA support will be enabled for TensorFlowPlease specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5Please specify the location where cuDNN 5 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.0Setting up Cuda includeSetting up Cuda libSetting up Cuda binSetting up Cuda nvvmSetting up CUPTI includeSetting up CUPTI lib64Configuration finished
如果你使用GPU,那么configure
脚本会在你的系统上创建符号链接到cuda。所以每次改变cuda,在执行bazel build
之前就重新运行configure
。
4. 编译pip package
使用CPU-only,使用:$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
使用GPU,使用:$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
注意: gcc => 5: the binary pip packages available on the TensorFlow website are built with gcc 4, which uses the older ABI. To make your build compatible with the older ABI, you need to add --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" to your bazel build command. ABI compatibility allows custom ops built against the TensorFlow pip package to continue to work against your built package.
Tip: 这种需要很多的RAM,如果你的RAM没有吗么多,可以限制RAM的使用:--local_resources 2048,.5,1.0
执行bazel build
会生成一个build_pip_package
的脚本。运行这个脚本会生成一个.whl
的文件在/tmp/tensorflow_pkg
文件夹:
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
5. 安装pip package
执行pip install
来安装, 会生成一个.whl
的文件。例如在linux上的Tensorlow 1.2.0:$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.0-py2-none-any.whl
6. 验证你的安装是否成功
使用terminal进入python,输入
# Pythonimport tensorflow as tfhello = tf.constant('Hello, TensorFlow!')sess = tf.Session()print(sess.run(hello))
如果输出Hello, TensorFlow!
就表示成功了!
问题
- 如果碰到
AttributeError: 'module' object has no attribute 'Default'
,那么执行sudo pip install --upgrade protobuf