TensorFlow源码编译

5 篇文章 0 订阅
4 篇文章 0 订阅
1、版本tensorflow-1.15.5

https://github.com/tensorflow/tensorflow/releases/tag/v1.15.5

Note that this is the last patch release for the TensorFlow 1.x series.
本文将源代码下载在/home/svn路径下,并在编译完成后,将编译所得tensorflow环境,统一放置在/home/svn/tensorflow路径。

2、使用对应版本的python
cd /usr/bin/
ln -s python3.6 python
3、安装对应版本的bazel

https://github.com/bazelbuild/bazel/releases?page=6

wget https://github.com/bazelbuild/bazel/releases/download/0.26.1/bazel-0.26.1-installer-linux-x86_64.sh 
# 查看安装设置
./bazel-0.26.1-installer-linux-x86_64.sh --help
# 执行安装
./bazel-0.26.1-installer-linux-x86_64.sh

如果bazel版本不匹配,在编译TensorFlow时会报如下错误:
bazel版本与tensorflow版本不匹配

4、安装protobuf

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protobuf-all-3.9.0.tar.gz 
tar zxvf protobuf-all-3.9.0.tar.gz
cd protobuf-3.9.0
# generated the configure script
./autogen.sh
# 默认安装位置为/usr/local/,可以根据需要修改
./configure --prefix=/home/svn/third/third64/protobuf-3.9.0
make
make check
make install
# 查看是否安装成功
protoc --version

TensorFlow的protobuf版本兼容问题
详见:

https://blog.csdn.net/Li_suhuan/article/details/121041248

5、编译Tensorflow
wget https://github.com/tensorflow/tensorflow/archive/refs/tags/v1.15.5.tar.gz 
tar zxvf v1.15.5.tar.gz
cd tensorflow-1.15.5
./configure

编译设置选项如图:
tensorflow编译设置
执行编译:

# 有显卡
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
# 无显卡
bazel build --config=opt //tensorflow:libtensorflow_cc.so
6、编译第三方库
cd /home/svn/tensorflow-1.15.5/tensorflow/contrib/makefile
./build_all_linux.sh

GCC 8 编译时会报错,解决方法如下:

gcc8编译tensorflow报错解决
https://blog.felgo.com/cross-platform-development/machine-learning-add-image-classification-for-ios-and-android-with-qt-and-tensorflow

编译eigen3
cd /home/svn/tensorflow-1.15.5/tensorflow/contrib/makefile/downloads/eigen/
mkdir build
cd build
cmake ..
make
make install

编译出的eigen3文件夹在/usr/local/include目录下,可以移动到需要的地方

7、环境准备

在/home/svn/下建立tensorflow文件夹,并在其下建立include和lib
其中lib存放编译出的so

cd /home/svn/tensorflow/lib/
cp /home/svn/tensorflow-1.15.5/bazel-genfiles/tensorflow/libtensorflow_cc.so ./
cp /home/svn/tensorflow-1.15.5/bazel-genfiles/tensorflow/libtensorflow_framework.so.1 ./
mv libtensorflow_framework.so.1 libtensorflow_framework.so

include存放所需的头文件

cp -r /home/svn/tensorflow-1.15.5/tensorflow /home/svn/tensorflow/include/
cp -r /home/svn/tensorflow-1.15.5/bazel-genfiles/tensorflow/* /home/svn/tensorflow/include/tensorflow/
# so文件放在lib中即可,这里不需要
rm /home/svn/tensorflow/include/tensorflow/libtensorflow_*
# 第三方库
mkdir /home/svn/tensorflow/include/tensorflow/third_party
cp -r /home/svn/tensorflow-1.15.5/tensorflow/contrib/makefile/downloads/eigen /home/svn/tensorflow/include/tensorflow/third_party/eigen3
# 编译的时候缺啥补啥
cp /home/svn/tensorflow-1.15.5/bazel-tensorflow-1.15.5/third_party/eigen3/unsupported/Eigen/CXX11/FixedPoint  /home/svn/tensorflow/include/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/
cp -r /usr/local/include/eigen3/unsupported/Eigen/CXX11/* /home/svn/tensorflow/include/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/
cp -r /home/svn/tensorflow-1.15.5/bazel-tensorflow-1.15.5/third_party/eigen3/unsupported/Eigen/CXX11/src /home/svn/tensorflow/include/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/

https://www.cnblogs.com/dzzy/p/13493876.html

至此安装完成,使用时添加相应的include和lib路径即可

# tensorflow
INCLUDE   += -I/home/svn/tensorflow/include/
INCLUDE   += -I/home/svn/tensorflow/include/tensorflow/third_party/eigen3/
INCLUDE   += -I/home/svn/tensorflow/include/tensorflow/
INCLUDE   += -I/home/svn/tensorflow/include/tensorflow/contrib/makefile/downloads/absl

LIB       += -L/home/svn/tensorflow/lib/ -ltensorflow_framework -ltensorflow_cc -lz
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是在Linux系统中从源码编译安装TensorFlow 1.13.1的步骤: 1. 安装必要的依赖项: ``` sudo apt-get update && sudo apt-get install python3-dev python3-pip python3-wheel python3-venv sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev sudo apt-get install liblapack-dev cython3 ``` 2. 创建Python虚拟环境: ``` python3 -m venv tensorflow_env source tensorflow_env/bin/activate ``` 3. 下载TensorFlow 1.13.1源代码: ``` wget https://github.com/tensorflow/tensorflow/archive/v1.13.1.tar.gz tar -xzvf v1.13.1.tar.gz ``` 4. 进入TensorFlow源代码目录: ``` cd tensorflow-1.13.1 ``` 5. 配置编译选项: ``` ./configure ``` 按照提示进行配置,其中需要注意的是: - 选择Python解释器的路径为虚拟环境中的Python路径,例如:/home/user/tensorflow_env/bin/python3.6; - 如果您的计算机支持AVX指令集,请在编译选项中启用该选项,以获得更好的性能。 6. 开始编译: ``` bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package ``` 编译过程可能需要一些时间,请耐心等待。 7. 生成安装包: ``` ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg ``` 此步骤将会在/tmp/tensorflow_pkg目录下生成TensorFlow 1.13.1的安装包。 8. 安装TensorFlow: ``` pip install /tmp/tensorflow_pkg/tensorflow-1.13.1-*.whl ``` 9. 验证TensorFlow是否安装成功: ``` python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))" ``` 如果输出了一个随机数的和,则说明TensorFlow安装成功。 希望以上步骤能够帮助您成功安装TensorFlow 1.13.1。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值