Utilizing control flow mechanics in building graphs
Now let's learn about an interesting TensorFlow mechanic. TensorFlow provides a mechanism for making decisions when building a graph. However, there are some subtle differences when we use Python's control flow statements compared to TensorFlow's control flow functions, when constructing computation graphs.
To illustrate these differences with some simple code examples, let's consider implementing the following equation in TensorFlow:
In the following code, we may naively use Python's if
statement to build a graph that corresponds to the preceding equation:
>>> import tensorflow as tf >>> >>> x, y = 1.0, 2.0 >>> >>> g = tf.Graph() >>> with g.as_default(): ... tf_x = tf.placeholder(dtype=tf.float32, ... shape=None, name='tf_x') ... tf_y = tf.placeholder(dtype=tf.float32, ... shape=None, name=...