【发布时间】:2017-07-26 20:20:52
【问题描述】:
我希望将 TensorFlow 及其 ANN 功能用于我的计算力学目的,其中我的大部分代码都使用 C++。是否可以在不使用 Bazel 的情况下在包含 TensorFlow .h 文件的情况下在 c++ 中编译?如果是这样,我真的很感激一个例子(到目前为止还没有在网上找到)。 谢谢
编辑:我做到了,但我无法跟随。让我举个例子,也许我们可以从那里开始。我正在使用 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;
}
它位于 ~/Desktop/tensorflow/tensorflow/cc/example。我的 BUILD 文件 - 也在 ~/Desktop/tensorflow/tensorflow/cc/example 中 - 内容为:
cc_binary(
name = "example",
srcs = ["example.cc"],
deps = [
"//tensorflow/cc:cc_ops",
"//tensorflow/cc:client_session",
"//tensorflow/core:tensorflow",
],
)
我尝试使用 ~/Desktop/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
然后当我转到 ~/Desktop/tensorflow/bazel-bin/tensorflow/cc/example 并运行时:
./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
任何帮助将不胜感激,因为我正试图解决这个问题。谢谢你的耐心。
【问题讨论】:
-
请查看编辑。按照链接,我不确定如何将 libtensorflow.so 合并到我想做的事情中。
-
32 位?这没有帮助。
标签: c++ tensorflow compilation