Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to modify an existing TensorFlow computation graph?

Tags:

TensorFlow graph is usually built gradually from inputs to outputs, and then executed. Looking at the Python code, the inputs lists of operations are immutable which suggests that the inputs should not be modified. Does that mean that there is no way to update/modify an existing graph?

like image 249
Andrzej Pronobis Avatar asked Dec 12 '15 01:12

Andrzej Pronobis


People also ask

Does TensorFlow creates a computational graph?

In TensorFlow, machine learning algorithms are represented as computational graphs. A computational graph is a type of directed graph where nodes describe operations, while edges represent the data (tensor) flowing between those operations.

Why we are using computational graph for TensorFlow?

A program in TensorFlow is basically a computation graph. A graph can hold many operations which will be executed in order when a session executes a graph. A computation graph comprises nodes and edges. Each node represents an operation and each edge describes a tensor that gets transferred between the nodes.

How graphs are stored and represented in TensorFlow?

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 a data flow graph TensorFlow?

A dataflow graph is the representation of a computation where the nodes represent units of computation, and the edges represent the data consumed or produced by the computation. In the context of tf. Graph , every API call defines tf. Operation (node) that can have multiple inputs and outputs tf. Tensor (edges).


2 Answers

The TensorFlow tf.Graph class is an append-only data structure, which means that you can add nodes to the graph after executing part of the graph, but you cannot remove or modify existing nodes. Since TensorFlow executes only the necessary subgraph when you call Session.run(), there is no execution-time cost to having redundant nodes in the graph (although they will continue to consume memory).

To remove all nodes in the graph, you can create a session with a new graph:

with tf.Graph().as_default():  # Create a new graph, and make it the default.   with tf.Session() as sess:  # `sess` will use the new, currently empty, graph.     # Build graph and execute nodes in here. 
like image 176
mrry Avatar answered Dec 06 '22 16:12

mrry


Yes, tf.Graph are build in an append-only fashion as @mrry puts it.

But there's workaround:

Conceptually you can modify an existing graph by cloning it and perform the modifications needed along the way. As of r1.1, Tensorflow provides a module named tf.contrib.graph_editor which implements the above idea as a set of convinient functions.

like image 36
zaxliu Avatar answered Dec 06 '22 17:12

zaxliu