2017-07-26 52 views
1

我期待修改TensorFlow及其ANN功能,用於我的大部分代碼都在C++中的計算力學目的。包含TensorFlow .h文件時,是否可以在不使用Bazel的情況下使用C++進行編譯?如果是這樣,我真的很感激一個例子(到目前爲止還沒有能夠找到任何在線)。 謝謝使用TensorFlow編譯C++代碼而不使用Bazel

編輯:我做了,但我無法關注。讓我舉一個例子,也許我們可以從那裏開始。我使用Ubuntu 16.10,gcc(Ubuntu 6.2.0-5ubuntu12)6.2.0 20161005和Python 2.7.12+。我已經從源代碼安裝了bazel,並且還有克隆的TF庫(〜/ Desktop/tensorflow)。以從(https://www.tensorflow.org/api_guides/cc/guide)稍微修改的例子中,我在example.cc:

#include "tensorflow/cc/client/client_session.h" 
#include "tensorflow/cc/ops/standard_ops.h" 
#include "tensorflow/core/framework/tensor.h" 
#include <iostream> 

int main() { 
    using namespace tensorflow; 
    using namespace tensorflow::ops; 
    using namespace std; 
    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] 
    LOG(INFO) << outputs[0].matrix<float>(); 
    return 0; 
    cout<<"compiled correctly!"<<endl; 
} 

它位於〜/桌面/ tensorflow/tensorflow /立方厘米/例子。我的BUILD文件 - 也是在〜/桌面/ tensorflow/tensorflow/CC /例子 - 上寫着:

cc_binary(
    name = "example", 
    srcs = ["example.cc"], 
    deps = [ 
     "//tensorflow/cc:cc_ops", 
     "//tensorflow/cc:client_session", 
     "//tensorflow/core:tensorflow", 
    ], 
) 

我嘗試使用來自〜/桌面/ tensorflow編譯:

bazel build tensorflow/cc/example/... 

這是我得到:

INFO: Found 1 target... 
Target //tensorflow/cc/example:example up-to-date: 
    bazel-bin/tensorflow/cc/example/example 
INFO: Elapsed time: 0.381s, Critical Path: 0.00s 

然後,當我去到〜/桌面/ tensorflow /巴澤勒斌/ tensorflow/CC /例子並運行:

./example 

我得到:

2017-07-27 09:58:39.906578: 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-07-27 09:58:39.906628: 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-07-27 09:58:39.906636: 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. 
2017-07-27 09:58:39.906641: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 
2017-07-27 09:58:39.906646: 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. 
2017-07-27 09:58:39.907751: I tensorflow/cc/example/example.cc:22] 19 
-3 

任何幫助將是非常感激,因爲我試圖環繞此我的手。感謝您的耐心。

+1

你見過https://github.com/tensorflow/tensorflow/issues/2412嗎? – MSalters

+0

請參閱修改。在鏈接之後,我不確定如何將libtensorflow.so合併到我正在嘗試執行的操作中。 – user147813

+0

32位?這沒有幫助。 – MSalters

回答

0

步驟來建立建tensorflow框架的C++代碼,而無需使用任何工具的建築物像巴澤爾等從下面的鏈接GitHub的

  1. 克隆/下載tensorflow。

    由於pip install tensorflow只會爲python安裝tensorflow。

    https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile

  2. 運行

    將.cc文件和.pb your_code.cc(要建立,使用.cc C和C++,.cpp不會出於某種原因C++的文件)文件中tensorflow/tensorflow /工具/基準/

    編輯Makefile中的下列行vim編輯器 (tensorflow/tensorflow /的contrib/Makefile文件/ Makefile文件)

    BENCHMARK_NAME := $(BINDIR)benchmark 
    

    相反的標杆,給你的可執行文件的名稱

    BENCHMARK_SRCS := \ 
    tensorflow/core/util/reporter.cc \ 
    tensorflow/tools/benchmark/benchmark_model.cc \ 
    tensorflow/tools/benchmark/benchmark_model_main.cc 
    

    刪除這些文件,並添加你的源代碼(.cc)在這裏。

  3. 下一頁評論這條線在Makefile:

    all: $(LIB_PATH) $(BENCHMARK_NAME) 
    

    ,並添加follwing線:

    all: $(BENCHMARK_NAME) 
    
  4. 下運行make文件去根目錄(tesnroflow /)和鍵入以下命令。

    make -f tensorflow/contrib/makefile/Makefile 
    
  5. 如果您遇到任何錯誤,只需鍵入以下命令,

    export HOST_NSYNC_LIB=`/home/nivedita_s/Downloads/tensorflow-master/tensorflow/contrib/makefile/compile_nsync.sh` 
    export TARGET_NSYNC_LIB="$HOST_NSYNC_LIB" 
    
  6. 如果遵循正確的,則執行將在下文中提到此文件夾中創建的所有過程,

    tensorflow/tensorflow/contrib/makefile/gen/bin/ 
    
  7. 輸出將以您爲可執行文件指定的名稱創建。 如果./executable_file給出了任何庫問題,請按照下面的步驟(a和b)進行操作。

    • vim ~/.profile添加此行末,

      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path_to_the_tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src/.libs 
      
    • 運行此命令

      source ~/.profile 
      

現在你應該可以在能夠運行程序終奌站。

1

最簡單的方法是簡單地將所有必要的文件複製到本地項目:

cd /git/github.com/tensorflow 
git clone [email protected]:tensorflow/tensorflow.git 
# build TensorFlow once (tensorflow:libtensorflow_cc.so, tensorflow:libtensorflow.so) 
cd project 
mkdir tensorflow 
cp /git/github.com/tensorflow/tensorflow/bazel-tensorflow/tensorflow/cc tensorflow/cc 
cp /git/github.com/tensorflow/tensorflow/bazel-genfiles/tensorflow/cc tensorflow 

已具備了這些文件,一個很基本的CMakeLists.txt這項工作:

cmake_minimum_required(VERSION 2.8) 

set(CMAKE_CXX_STANDARD 11) 
set(CMAKE_CXX_STANDARD_REQUIRED ON) 

set(TensorFlow_ABI 0) 
set(TensorFlow_INCLUDE_DIRS "${HOME}/.local/lib/python2.7/site-packages/tensorflow/include") 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${TensorFlow_ABI}") 
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${TensorFlow_ABI}") 


project(TFMyProject) 

include_directories(SYSTEM "${TensorFlow_INCLUDE_DIRS}/external/nsync/public") 
include_directories(SYSTEM ${TensorFlow_INCLUDE_DIRS}) 
include_directories(SYSTEM ".") 

add_executable (example example.cc) 
TARGET_LINK_LIBRARIES(example -Wl,--allow-multiple-definition -Wl,--whole-archive "/git/github.com/tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so" -Wl,--no-whole-archive) 
TARGET_LINK_LIBRARIES(example -Wl,--allow-multiple-definition -Wl,--whole-archive "/git/github.com/tensorflow/bazel-bin/tensorflow/libtensorflow_framework.so" -Wl,--no-whole-archive) 

即使這些設置可以使用a python script進行設置。