-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhello-world.cc
47 lines (36 loc) · 1.45 KB
/
hello-world.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <tensorflow/cc/client/client_session.h>
#include <tensorflow/cc/ops/standard_ops.h>
#include <tensorflow/core/framework/tensor.h>
// Simple hello world using TensorFlow
// The sample demonstrates how to
// - create various ops (Const & StringJoin)
// - pass them around for e.g. StringJoin takes an list of other ops
// - pass the final ops to the session
// - get the result of the session
// - a simple peek inside the output using the DebugString & by flattening it
int main(int argc, char **argv) {
using namespace tensorflow;
using namespace tensorflow::ops;
// create a root scope
auto scope = Scope::NewRootScope();
// define various constans/inputs on which we
// will perform an operation
auto hello = Const(scope, std::string("hello"));
auto space = Const(scope, std::string(" "));
auto world = Const(scope, std::string("world !"));
// StringJoin operation
auto joinOp = StringJoin(scope, {hello, space, world});
// create a session that takes our
// scope as the root scope
ClientSession session(scope);
// Run
std::vector<Tensor> outputs;
TF_CHECK_OK(session.Run({joinOp}, &outputs));
// See our output using DebugString that tells
// more information about the tensor
std::cout << "DebugString -> " << outputs[0].DebugString() << std::endl;
// we can also get the underlying data by calling flat
std::cout << "Underlying Scalar value -> " << outputs[0].flat<std::string>()
<< std::endl;
return 0;
}