Ubuntu安装TensorFlow C++

本文参考TensorFlow官网的安装方法:https://www.tensorflow.org/install/source

1、安装protobuf

 2、安装bazel

3、下载TensorFlow源码   

4、使用bazel编译TensorFlow,产生我们需要的库文件

5、编译其它依赖项

6、测试


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的版本相对应,所以推荐第二种安装方式,具体的版本对应信息参考

对于Ubuntu 18.04,你可以使用以下步骤在C++中使用TensorFlow库: 1. 安装依赖项: ``` sudo apt-get update sudo apt-get install build-essential curl sudo apt-get install libcurl3-dev libfreetype6-dev libpng-dev libzmq3-dev pkg-config sudo apt-get install python3-dev python3-pip python3-wheel python3-setuptools ``` 2. 安装TensorFlow C++库: - 通过pip安装TensorFlow C++库: ``` pip3 install tensorflow ``` - 或者,从源代码构建TensorFlow C++库: - 克隆TensorFlow仓库: ``` git clone https://github.com/tensorflow/tensorflow.git cd tensorflow ``` - 配置构建选项并构建TensorFlow C++库: ``` ./configure bazel build --config=opt //tensorflow:libtensorflow_cc.so ``` - 在.bashrc或.bash_profile文件中设置LD_LIBRARY_PATH环境变量: ``` export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/tensorflow/bazel-bin/tensorflow/ ``` 3. 编写C++代码并链接TensorFlow库: - 创建一个C++文件,比如example.cpp,并添加以下代码: ```cpp #include <tensorflow/cc/client/client_session.h> #include <tensorflow/cc/ops/standard_ops.h> #include <tensorflow/core/framework/tensor.h> int main() { using namespace tensorflow; using namespace tensorflow::ops; Scope root = Scope::NewRootScope(); auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} }); auto b = Const(root, { {3.f, 5.f} }); auto product = MatMul(root.WithOpName("product"), A, b, MatMul::TransposeB(true)); std::vector<Tensor> outputs; ClientSession session(root); TF_CHECK_OK(session.Run({product}, &outputs)); for (const auto& tensor : outputs) { std::cout << tensor.matrix<float>() << std::endl; } return 0; } ``` - 编译C++代码: ``` g++ -std=c++11 -I/path/to/tensorflow -L/path/to/tensorflow/bazel-bin/tensorflow example.cpp -ltensorflow_cc -o example ``` - 运行生成的可执行文件: ``` ./example ``` 这样,你就可以在Ubuntu 18.04上使用C++编写和运行TensorFlow代码了。请确保将`/path/to/tensorflow`替换为你实际的TensorFlow安装路径。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值