1# TensorFlow 2 3TensorFlow is a computational dataflow graph library. 4 5## Getting started 6 7 8### Python API example 9The following is an example python code to do a simple matrix multiply 10of two constants and get the result from a locally-running TensorFlow 11process. 12 13First, bring in tensorflow python dependency 14 15//third_party/py/tensorflow 16 17to get the python TensorFlow API. 18 19Then: 20 21```python 22import tensorflow as tf 23 24with tf.Session(): 25 input1 = tf.constant(1.0, shape=[1, 1], name="input1") 26 input2 = tf.constant(2.0, shape=[1, 1], name="input2") 27 output = tf.matmul(input1, input2) 28 29 # Run graph and fetch the output 30 result = output.eval() 31 print result 32``` 33 34### C++ API Example 35 36If you are running TensorFlow locally, link your binary with 37 38//third_party/tensorflow/core 39 40and link in the operation implementations you want to supported, e.g., 41 42//third_party/tensorflow/core:kernels 43 44An example program to take a GraphDef and run it using TensorFlow 45using the C++ Session API: 46 47```c++ 48#include <memory> 49#include <string> 50#include <vector> 51 52#include "tensorflow/core/framework/graph.pb.h" 53#include "tensorflow/core/public/session.h" 54#include "tensorflow/core/framework/tensor.h" 55 56int main(int argc, char** argv) { 57 // Construct your graph. 58 tensorflow::GraphDef graph = ...; 59 60 // Create a Session running TensorFlow locally in process. 61 std::unique_ptr<tensorflow::Session> session(tensorflow::NewSession({})); 62 63 // Initialize the session with the graph. 64 tensorflow::Status s = session->Create(graph); 65 if (!s.ok()) { ... } 66 67 // Specify the 'feeds' of your network if needed. 68 std::vector<std::pair<string, tensorflow::Tensor>> inputs; 69 70 // Run the session, asking for the first output of "my_output". 71 std::vector<tensorflow::Tensor> outputs; 72 s = session->Run(inputs, {"my_output:0"}, {}, &outputs); 73 if (!s.ok()) { ... } 74 75 // Do something with your outputs 76 auto output_vector = outputs[0].vec<float>(); 77 if (output_vector(0) > 0.5) { ... } 78 79 // Close the session. 80 session->Close(); 81 82 return 0; 83} 84``` 85 86For a more fully-featured C++ example, see 87`tensorflow/cc/tutorials/example_trainer.cc` 88