Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Tensorflow's Graph and GraphDef

Tags:

tensorflow

I'm quite new to the tensorflow. I would like to understand to conceptual difference between Graph and GraphDef.

furthermore, which one should I have to run a graph loaded from protobuf file (.pb)?

Thanks!

like image 820
YAO Avatar asked Nov 01 '17 16:11

YAO


People also ask

What is GraphDef?

GraphDef is the proto defined here. This is the serialized version of graph. You can print, store, or restore a GraphDef in any TensorFlow frontend (Python, R, C++, Java, ...).

What is TensorFlow graph?

TensorFlow uses graphs as the format for saved models when it exports them from Python. Graphs are also easily optimized, allowing the compiler to do transformations like: Statically infer the value of tensors by folding constant nodes in your computation ("constant folding").

What is Protos in TensorFlow?

proto. These are the fundamental building blocks of TensorFlow graphs, with each one defining a single operation along with its input connections.

Where is tf function used?

You can use tf. function to make graphs out of your programs. It is a transformation tool that creates Python-independent dataflow graphs out of your Python code. This will help you create performant and portable models, and it is required to use SavedModel .


1 Answers

Graph or Computional Graph is the core concept of tensorflow to present computation. When you use tensorflow, you firstly create you own Computation Graph and pass the Graph to tensorflow. How to do that? As you may know, tensorflow support many front-end programming languages, like Python, C++, Java and Go and the core language is C++; how do the other languages transform the Graph to C++? They use a tool called protobuf which can generate specific language stubs, that's where the GraphDef come from. It's a serialized version of Graph.

which one should I have to run a graph loaded from protobuf file (.pb)

You should read your *pb file using GraphDef and bind the GraphDef to a (default) Graph, then use a session to run the Graph for computation, like the following code:

import tensorflow as tf from tensorflow.python.platform import gfile with tf.Session() as sess:     model_filename ='PATH_TO_PB.pb'     with gfile.FastGFile(model_filename, 'rb') as f:         graph_def = tf.GraphDef()         graph_def.ParseFromString(f.read())         g_in = tf.import_graph_def(graph_def) LOGDIR='/logs/tests/1/' train_writer = tf.summary.FileWriter(LOGDIR) train_writer.add_graph(sess.graph) 
like image 105
Elinx Avatar answered Sep 24 '22 05:09

Elinx