-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsimple_model.cc
87 lines (72 loc) · 2.44 KB
/
simple_model.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
using namespace tensorflow;
/**
* @brief deep model for click through rate prediction
* @details [long description]
*
* @param argv[1] graph protobuf
*
* @return [description]
*/
int main(int argc, char* argv[]) {
// Initialize a tensorflow session
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Session created successfully" << std::endl;
}
// Load the protobuf graph
GraphDef graph_def;
std::string graph_path = argv[1];
status = ReadBinaryProto(Env::Default(), graph_path, &graph_def);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Load graph protobuf successfully" << std::endl;
}
// Add the graph to the session
status = session->Create(graph_def);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Add graph to session successfully" << std::endl;
}
// Setup inputs and outputs:
// Our graph doesn't require any inputs, since it specifies default values,
// but we'll change an input to demonstrate.
Tensor a(DT_FLOAT, TensorShape());
a.scalar<float>()() = 3.0;
Tensor b(DT_FLOAT, TensorShape());
b.scalar<float>()() = 2.0;
std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
{ "a", a },
{ "b", b },
};
// The session will initialize the outputs
std::vector<tensorflow::Tensor> outputs;
// Run the session, evaluating our "c" operation from the graph
status = session->Run(inputs, {"c"}, {}, &outputs);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Run session successfully" << std::endl;
}
// Grab the first output (we only evaluated one graph node: "c")
// and convert the node to a scalar representation.
auto output_c = outputs[0].scalar<float>();
// (There are similar methods for vectors and matrices here:
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/public/tensor.h)
// Print the results
std::cout << outputs[0].DebugString() << std::endl; // Tensor<type: float shape: [] values: 30>
std::cout << "output value: " << output_c() << std::endl; // 30
// Free any resources used by the session
session->Close();
return 0;
}