Skip to content

Commit 3475ff9

Browse files
committed
feat - add 1_Introduction/basic-operation example
1 parent 51f815a commit 3475ff9

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ _release
3636
build
3737
_r*
3838
_d*
39-
tensorflow-sdk
39+
tensorflow-sdk
40+
_xdebug

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"fft": "cpp"
4+
}
5+
}

examples/1_Introduction/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endmacro()
1414

1515
# here simply call the macro for every sample file (the one that contains the main)
1616
add_introduction(hello-world ${INTRODUCTION_SRC_PATH} hello-world)
17-
17+
add_introduction(basic-operations ${INTRODUCTION_SRC_PATH} basic-operations)
1818

1919

2020

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <tensorflow/cc/client/client_session.h>
2+
#include <tensorflow/cc/ops/standard_ops.h>
3+
#include <tensorflow/core/framework/tensor.h>
4+
5+
// Basic math operations using tensorflow
6+
7+
// The sample demonstrates how to
8+
// - create various operations
9+
// - use placeholder and placeholderWithDefault
10+
// - use different overloads of Run method
11+
12+
int main(int argc, char **argv) {
13+
14+
using namespace tensorflow;
15+
using namespace tensorflow::ops;
16+
17+
// create a root scope
18+
auto scope = Scope::NewRootScope();
19+
20+
// we are creating various local scopes
21+
// so that a new session object is created
22+
// for all the examples
23+
24+
{
25+
// An example of doing addition
26+
// on constants
27+
28+
ClientSession session(scope);
29+
30+
auto a = Const(scope, 2);
31+
auto b = Const(scope, 3);
32+
33+
auto c = Add(scope, a, b);
34+
35+
std::vector<Tensor> outputs;
36+
TF_CHECK_OK(session.Run({c}, &outputs));
37+
38+
// we know that it will be scalar
39+
// we can also get the underlying data by calling flat
40+
std::cout << "Underlying Scalar value -> " << outputs[0].flat<int>()
41+
<< std::endl;
42+
}
43+
44+
{
45+
// An example of how to supply a variable (i.e. not a constant)
46+
// whose value is supplied at the time when we run the session
47+
48+
ClientSession session(scope);
49+
50+
// we will use Placeholder as the type for our variables
51+
auto a = Placeholder(scope, DT_INT32);
52+
auto b = Placeholder(scope, DT_INT32);
53+
54+
// define the add operation that takes
55+
// the placeholders a and b as inputs
56+
auto c = Add(scope, a, b);
57+
58+
std::vector<Tensor> outputs;
59+
60+
// we now specify the values for our placeholders
61+
// note that the way Run method is called. It is quite
62+
// different from previous example.
63+
//
64+
// Here we are using this overload of Run method
65+
// Run(const FeedType& inputs, const std::vector<Output>& fetch_outputs,
66+
// std::vector<Tensor>* outputs) const;
67+
//
68+
//
69+
// which takes FeedType (alias of std::unordered_map<Output, Input::Initializer, OutputHash>
70+
// as the first argument.
71+
// Note - In std::unordered_map OutputHash is optional
72+
// So we just need to supply a map whose key of type "Output" and the
73+
// value that respect Initializer
74+
//
75+
// {a,2} & {b,3} would satisfiy this requirement since type 'a' & 'b'
76+
// is Output
77+
78+
auto status = session.Run({
79+
{
80+
{a, 2},
81+
{b, 3}
82+
} }, {c}, &outputs);
83+
84+
TF_CHECK_OK(status);
85+
86+
// we know that it will be scalar
87+
// we can also get the underlying data by calling flat
88+
std::cout << "Underlying Scalar value -> " << outputs[0].flat<int>()
89+
<< std::endl;
90+
}
91+
92+
{
93+
// This is yet another example that makes use of Placeholder however
94+
// this time we want one of the placeholder to have a default value
95+
//
96+
// In other words, it does not need to be specified during the session
97+
// execution. if you give a new value it would accept it else would use
98+
// the default value
99+
100+
ClientSession session(scope);
101+
102+
// create an input
103+
auto defaultAInput = Input(8);
104+
105+
// we will use Placeholder as the type for our variables
106+
auto a = PlaceholderWithDefault(scope, defaultAInput, PartialTensorShape());
107+
auto b = Placeholder(scope, DT_INT32);
108+
109+
// define the add operation that takes
110+
// the placeholders a and b as inputs
111+
auto c = Add(scope, a, b);
112+
113+
std::vector<Tensor> outputs;
114+
115+
// In this Run we are not specifying 'a'
116+
// so its default value i.e. 8 will be used
117+
auto status = session.Run({
118+
{
119+
{b, 3}
120+
} }, {c}, &outputs);
121+
122+
TF_CHECK_OK(status);
123+
124+
std::cout << "Underlying Scalar value (using default placeholder value [8]) -> " << outputs[0].flat<int>()
125+
<< std::endl;
126+
127+
// here we do specify a value for placeholder 'a' i.e. 9
128+
status = session.Run({
129+
{
130+
{a, 9},
131+
{b, 3}
132+
} }, {c}, &outputs);
133+
134+
TF_CHECK_OK(status);
135+
136+
std::cout << "Underlying Scalar value (after supplying new value [9]) -> " << outputs[0].flat<int>()
137+
<< std::endl;
138+
139+
}
140+
141+
return 0;
142+
}

0 commit comments

Comments
 (0)