TensorFlow 1 version View source on GitHub

A TensorFlow computation, represented as a dataflow graph.

Used in the notebooks

Used in the tutorials

Graphs are used by tf.functions to represent the function's computations. Each graph contains a set of tf.Operation objects, which represent units of computation; and tf.Tensor objects, which represent the units of data that flow between operations.

Using graphs directly (deprecated)

A tf.Graph can be constructed and used directly without a tf.function, as was required in TensorFlow 1, but this is deprecated and it is recommended to use a tf.function instead. If a graph is directly used, other deprecated TensorFlow 1 classes are also required to execute the graph, such as a tf.compat.v1.Session.

A default graph can be registered with the tf.Graph.as_default context manager. Then, operations will be added to the graph instead of being executed eagerly. For example:

g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  assert c.graph is g

tf.compat.v1.get_default_graph() can be used to obtain the default graph.

Important note: This class is not thread-safe for graph construction. All operations should be created from a single thread, or external synchronization must be provided. Unless otherwise specified, all methods are not thread-safe.

A Graph instance supports an arbitrary number of "collections" that are identified by name. For convenience when building a large graph, collections can store groups of related objects: for example, the tf.Variable uses a collection (named tf.GraphKeys.GLOBAL_VARIABLES) for all variables that are created during the construction of a graph. The caller may define additional collections by specifying a new name.

building_function Returns True iff this graph represents a function.
collections Returns the names of the collections known to this graph.
finalized True if this graph has been finalized.
graph_def_versions The GraphDef version information of this graph.

For details on the meaning of each version, see GraphDef.

seed The graph-level random seed of this graph.
version Returns a version number that increases as ops are added to the graph.

Note that this is unrelated to the tf.Graph.graph_def_versions.



View source

Stores value in the collection with the given name.

Note that collections are not sets, so it is possible to add a value to a collection several times.

name The key for the collection. The GraphKeys class contains many standard names for collections.
value The value to add to the collection.


View source

Stores value in the collections given by names.

Note that collections are not sets, so it is possible to add a value to a collection several times. This function makes sure that duplicates in names are ignored, but it will not check for pre-existing membership of value in any of the collections in names.

names can be any iterable, but if names is a string, it is treated as a single collection name.

names The keys for the collections to add to. The GraphKeys class contains many standard names for collections.
value The value to add to the collections.


View source

Returns a context manager that makes this Graph the default graph.

This method should be used if you want to create multiple graphs in the same process. For convenience, a global default graph is provided, and all ops will be added to this graph if you do not create a new graph explicitly.

Use this method with the with keyword to specify that ops created within the scope of a block should be added to this graph. In this case, once the scope of the with is exited, the previous default graph is set again as default. There is a stack, so it's ok to have multiple nested levels of as_default calls.

The default graph is a property of the current thread. If you create a new thread, and wish to use the default graph in that thread, you must explicitly add a with g.as_default(): in that thread's function.

The following code examples are equivalent:

# 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

# 2. Constructing and making default:
with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  assert c.graph is g

If eager execution is enabled ops created under this context manager will be added to the graph instead of executed eagerly.

A context manager for using this graph as the default graph.


View source

Returns a serialized GraphDef representation of this graph.

The serialized GraphDef can be imported into another Graph (using tf.import_graph_def) or used with the C++ Session API.

This method is thread-safe.

from_version Optional. If this is set, returns a GraphDef containing only the nodes that were added to this graph since its version property had the given value.
add_shapes If true, adds an "_output_shapes" list attr to each node with the inferred shapes of each of its outputs.

A GraphDef protocol buffer.

ValueError If the graph_def would be too large.


View source

Returns the object referred to by obj, as an Operation or Tensor.

This function validates that obj represents an element of this graph, and gives an informative error message if it is not.

This function is the canonical way to get/validate an object of one of the allowed types from an external argument reference in the Session API.

This method may be called concurrently from multiple threads.

obj A Tensor, an Operation, or the name of a tensor or operation. Can also be any object with an _as_graph_element() method that returns a value of one of these types. Note: _as_graph_element will be called inside the graph's lock and so may not modify the graph.
allow_tensor If true, obj may refer to a Tensor.
allow_operation If true, obj may refer to an Operation.

The Tensor or Operation in the Graph corresponding to obj.

TypeError If obj is not a type we support attempting to convert to types.
ValueError If obj is of an appropriate type but invalid. For example, an invalid string.
KeyError If obj is not an object in the graph.


View source

Clears all values in a collection.