本文参考TensorFlow官网的安装方法:https://www.tensorflow.org/install/source
4、使用bazel编译TensorFlow,产生我们需要的库文件
1、安装protobuf
protobuffer的GitHub官网在这里,安装protobuf要注意protobuf的版本要和TensorFlow的版本相对应,protobuf和tensorflow的版本对应信息在这里。安装过程如下:
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protobuf-cpp-3.7.1.tar.gz
tar -xzvf protobuf-cpp-3.7.1.tar.gz
sudo apt-get install automake libtool
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
# sudo make uninstall 安装错版本后卸载指令
protoc --version # 查看protobuf版本
如果遇到报错:protoc: error while loading shared libraries: libprotoc.so.17: cannot open shared object file: No such file or directory,则:
export LD_LIBRARY_PATH=/usr/local/lib/
2、安装bazel
bazel是Google开源的一套编译构建工具,广泛应用于Google内部,包括TensorFlow项目。修改TensorFlow内部源码,需要使用bazel来编译,故有必要了解下bazel。bazel优点很多,主要有:
1)构建快。支持增量编译。对依赖关系进行了优化,从而支持并发执行;
2)可构建多种语言。bazel可用来构建Java、C++、Android、ios等很多语言和框架,并支持mac、windows、linux等不同平台;
3)可伸缩。可处理任意大小的代码库,可处理多个库,也可以处理单个库;
4)可扩展。使用bazel扩展语言可支持新语言和新平台。
官方的安装文档参考这里。安装bazel的主要步骤如下:
(1)第一种安装方式(这种方式只能安装最新版的bazel):Using Bazel custom APT repository
Step 1: Install the JDK
sudo apt-get install openjdk-8-jdk
Step 2: Add Bazel distribution URI as a package source
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 -
Step 3: Install and update Bazel
sudo apt-get update && sudo apt-get install bazel
或者只更新bazel:
sudo apt-get install --only-upgrade bazel
(2)第二种安装方式(可以安装任何版本的bazel): Installing using binary installer
因为bazel的版本要和TensorFlow的版本相对应,所以推荐第二种安装方式,具体的版本对应信息参考