Tensorflow C++ 学习(一) 搭建环境
2017-11-22 16:43
423 查看
转自:http://blog.csdn.net/jmh1996/article/details/73197337?locationNum=6
Tensorflow 网上大部分是python的资料较多,而C++方面的极少,因此,接下来会有一系列的博客用于学习tensorflow,记录学习的过程。加油!
既然使用C++的API,那第一步就是搭建Tensorflow的工作环境
1. 准备一台64位的虚拟机 ,我安装的ubuntu 16.04 64位的. 用新的虚拟机主要是图个干净利落,同时修改好软件源,建议改成阿里云的,安装一些常用的运行库,开发库。
2. 安装 bazel
> echo
"deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8"
| sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add
->sudo apt-get update && sudo apt-get install bazel
可能会提示没有安装curl,则需要先安装curl
安装bazel
本人使用 ubuntu 16.04刚上的虚拟机,妥妥的。
3. 安装python的一些常用库
安装cpu 开发工具库
下载tensorflow 源码
2
3
4
5
出现这个就配置对了:
INFO:All external dependencies fetched successfully.
安装tensorflow ,使用pip
测试C++接口
将 tensorflow目录下的:tensorflow/cc/example/example.cc 文件内容替换为(没有就新建一个这个目录下的文件):
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
在tensorflow/cc/example/ 目录下新建一个BUILD文件,文件内容:
2
3
4
5
6
7
8
9
编译:
2
然后首次编译很久,等一会儿就好。
最后打印出:
2
3
4
5
说明安装成功。
但是!这里有太多的调试信息了,我们把它去掉:
2
打印:
2
OK Perfect!
前言
Tensorflow 网上大部分是python的资料较多,而C++方面的极少,因此,接下来会有一系列的博客用于学习tensorflow,记录学习的过程。加油!
搭建环境
既然使用C++的API,那第一步就是搭建Tensorflow的工作环境 1. 准备一台64位的虚拟机 ,我安装的ubuntu 16.04 64位的. 用新的虚拟机主要是图个干净利落,同时修改好软件源,建议改成阿里云的,安装一些常用的运行库,开发库。
2. 安装 bazel
> echo
"deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8"
| sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add
->sudo apt-get update && sudo apt-get install bazel
可能会提示没有安装curl,则需要先安装curl
> sudo apt-get install curl1
安装bazel
>sudo apt-get update && sudo apt-get install bazel1
本人使用 ubuntu 16.04刚上的虚拟机,妥妥的。
3. 安装python的一些常用库
>sudo apt-get install sudo apt-get install python-numpy python-dev python-pip python-wheel -y1
安装cpu 开发工具库
>sudo apt-get install libcupti-dev1
下载tensorflow 源码
>sudo apt-get install git >git clone https://github.com/tensorflow/tensorflow >cd tensorflow >./configure #这一步不可少!否则后面c++编译不过1
2
3
4
5
出现这个就配置对了:
INFO:All external dependencies fetched successfully.
安装tensorflow ,使用pip
>pip install --upgrade tensorflow1
测试C++接口
将 tensorflow目录下的:tensorflow/cc/example/example.cc 文件内容替换为(没有就新建一个这个目录下的文件):
// tensorflow/cc/example/example.cc #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(); // Matrix A = [3 2; -1 0] auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f}}); // Vector b = [3 5] auto b = Const(root, { {3.f, 5.f}}); // v = Ab^T auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true)); std::vector<Tensor> outputs; ClientSession session(root); // Run and fetch v TF_CHECK_OK(session.Run({v}, &outputs)); // Expect outputs[0] == [19; -3] std::cout<< outputs[0].matrix<float>(); return 0; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
在tensorflow/cc/example/ 目录下新建一个BUILD文件,文件内容:
cc_binary( name = "example", srcs = ["example.cc"], deps = [ "//tensorflow/cc:cc_ops", "//tensorflow/cc:client_session", "//tensorflow/core:tensorflow", ], )1
2
3
4
5
6
7
8
9
编译:
>cd tensorflow #回到tensorflow的主目录下 >bazel run -c opt //tensorflow/cc/example:example1
2
然后首次编译很久,等一会儿就好。
最后打印出:
2017-06-13 17:20:50.578854: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-13 17:20:50.578970: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-13 17:20:50.578992: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 19 -31
2
3
4
5
说明安装成功。
但是!这里有太多的调试信息了,我们把它去掉:
>export TF_CPP_MIN_LOG_LEVEL=2 >bazel-bin/tensorflow/cc/example/example1
2
打印:
19 -31
2
OK Perfect!
相关文章推荐
- Tensorflow C++ 学习(一) 搭建环境
- ubuntu安装python集成环境Anaconda,并搭建tensorflow学习框架
- Ubuntu14.04 学习二: C/C++环境搭建,执行第一个hello world
- window环境搭建 tensorflow 框架深度学习开发环境
- linux 编程学习笔记(1)搭建c(c++)开发环境
- WIN10深度学习环境搭建 Python3.6+Tensorflow+CUDA8.0+Anaconda3+keras
- Tensorflow深度学习入门——环境搭建
- 如何在Ubuntu下安装Anaconda及搭建环境安装TensorFlow深度学习框架
- (通用)深度学习环境搭建:tensorflow安装教程及常见错误解决
- 使用Amazon AWS搭建GPU版tensorflow深度学习环境
- (通用)深度学习环境搭建:tensorflow安装教程及常见错误解决
- 深度学习平台:Tensorflow 环境搭建 | linux操作系统
- 手把手教你搭建谷歌TensorFlow深度学习开发环境!
- 从零开始学习C++第一篇:在eclipse中搭建开发环境
- Ubuntu14.04下基于nvidia-docker的Tensorflow深度学习环境搭建
- C++学习笔记1C++开发环境搭建创建第一个项目Hello word!
- 【C++学习笔记】02_C++开发环境搭建
- Ubuntu16.04LTS下搭建强化学习环境gym、tensorflow
- (通用)深度学习环境搭建:tensorflow安装教程及常见错误解决
- c++学习笔记(1)——环境搭建、经典教程推荐及 Visual Studio 2005/2008中如何编译和运行C++程序