7. Graph

7.1. What is a tf.Graph

In python, Graph is a class object, Class Graph.

TensorFlow provides a default graph that is an implicit argument(隐式参数) to all API functions in the same context.


7.1.1. Graph structure

7.1.2. Graph collections

  1. 本质就是一个dict,associate a list of objects with a key,就是说,一个key对应着一堆objects,这里的objects指的就是 tf.Operation objects and tf.Tensor objects
  2. 意义就是可以批量操作ops&tensors. Because disconnected parts of a TensorFlow program might want to create variables, it is sometimes useful to have a single way to access all of them。



  • Executing v = tf.Variable(0) adds to the graph a tf.Operation that will store a writeable tensor value that persists between tf.Session.run calls.
  • when you create a tf.Variable, it is added by default to collections representing “global variables” and “trainable variables”.
  1. tf.GraphKeys 包含了所有默认的集合名称。
  2. 实现方式就是新建ops object时,传入 collections 参数。

7.2. Executing a graph

我们构建的graph实际上就是一个client program,其和C++ runtime之间的连接由tf.Session来实现。


  • in a tf.Session.run(operation/tensor)
  • operation.run()
  • tensor.eval()

7.3. Saving and Restoring Models