牛骨文教育服务平台(让学习变的简单)
博文笔记

如何编译运行tensorflow的demo

创建时间:2017-08-06 投稿人: 浏览次数:2230

1.安装编译工具bazel,具体可以参照官方教程。
https://docs.bazel.build/versions/master/install-ubuntu.html
2. 配置tensorflow的编译环境
运行tensorflow目录下的configure文件,根据自己的环境进行配置。比如下面这样:

**root@fly-virtual-machine:/home/share/tensorflow# ./configure** 
Please specify the location of python. [Default is /root/anaconda3/bin//python]: /root/anaconda3/bin/python
**Do you wish to use jemalloc as the malloc implementation? (Linux only) [Y/n] y**
jemalloc enabled on Linux
**Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] n**
No Google Cloud Platform support will be enabled for TensorFlow
**Do you wish to build TensorFlow with Hadoop File System support? [y/N] n**
No Hadoop File System support will be enabled for TensorFlow
**Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] n**
No XLA JIT support will be enabled for TensorFlow
Found possible Python library paths:
  **/root/anaconda3/lib/python3.5/site-packages**
Please input the desired Python library path to use.  Default is [/root/anaconda3/lib/python3.5/site-packages]

Using python library path: /root/anaconda3/lib/python3.5/site-packages
Do you wish to build TensorFlow with O**重点内容**penCL support? [y/N] n
No OpenCL support will be enable**重点内容**d for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] n
No CUDA support will be enabled for TensorFlow
Configuration finished
INFO: Starting clean (this may take a while). Consider using --expunge_async if the clean takes more than several minutes.
...............
Building: no action running
Building: no action running
Building: no action running
Building: no action running
Building: no action running
Building: no action running
Building: no action running
Building: no action running
  1. 编译tensorflow动态库
    按照bazel的语法规则,找到相应的BUILD文件,路径为tensorflow/BUILD。可以看到
cc_binary(
    name = "libtensorflow_cc.so",
    linkshared = 1,
    deps = [
        "//tensorflow/c:c_api",
        "//tensorflow/cc:cc_ops",
        "//tensorflow/core:tensorflow",
    ],

当前tensorflow/BUILD目录下执行bazel build :libtensorflow.so,运行效果如下:
这里写图片描述

编译时间大概2000秒左右,看电脑性能,本次编译采用双核心四线程,4GB内存。
执行完成后,会在WORKSPACE文件所在目录生成以下几个目录:bazel-bin ,bazel-genfiles,bazel-out, bazel-tensorflow-master和 bazel-testlogs。进入bazel-out/local-py3-fastbuild/bin/tensorflow目录可以看到libtensorflow_cc.so动态库。
4. 编译demo,以tensorflowcc为例,编译c++的demo
同样在该目录下找到BUILD文件,找到要编译demo的bazel定义

tf_cc_test(
    name = "client_client_session_test",
    srcs = ["client/client_session_test.cc"],
    deps = [
        ":cc_ops",
        ":client_session",
        "//tensorflow/core:all_kernels",
        "//tensorflow/core:core_cpu_internal",
        "//tensorflow/core:framework",
        "//tensorflow/core:lib",
        "//tensorflow/core:tensorflow",
        "//tensorflow/core:test",
        "//tensorflow/core:test_main",
        "//tensorflow/core:testlib",
    ],

在BUILD目录位置执行bazel build :client_client_session_test,最终的结果生成路径如下:tensorflow-master/bazel-out/local-py3-fastbuild/bin/tensorflow/cc,该路径下面有可执行程序client_client_session_test,运行成功后的结果如下

root@fly-virtual-machine:/home/fly/tensorPro/tensorflow-master/tensorflow-master/bazel-out/local-py3-fastbuild/bin/tensorflow/cc# ./client_client_session_test
add by zgh, Running main() from test_main.cc
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from ClientSessionTest
[ RUN      ] ClientSessionTest.Basic
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.
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.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn"t compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[       OK ] ClientSessionTest.Basic (184 ms)
[ RUN      ] ClientSessionTest.Feed
[       OK ] ClientSessionTest.Feed (35 ms)
[ RUN      ] ClientSessionTest.Extend
[       OK ] ClientSessionTest.Extend (15 ms)
[ RUN      ] ClientSessionTest.MultiThreaded
[       OK ] ClientSessionTest.MultiThreaded (39 ms)
[----------] 4 tests from ClientSessionTest (273 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (274 ms total)
[  PASSED  ] 4 tests.

补充bazel的清除命令为:bazel clean

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。