Introducing eager execution

Eager execution in TensorFlow is more Pythonic and allows for rapid prototyping. Unlike the graph mode, where we need to construct a graph every time to perform any operations, eager execution follows the imperative programming paradigm, where any operations can be performed immediately, without having to create a graph, just like we do in Python. Hence, with eager execution, we can say goodbye to sessions and placeholders. It also makes the debugging process easier with an immediate runtime error, unlike the graph mode.

For instance, in the graph mode, to compute anything, we run the session. As shown in the following code, to evaluate the value of z, we have to run the TensorFlow session:

x = tf.constant(11)
y = tf.constant(11)
z = x*y

with tf.Session() as sess:
print sess.run(z)

With eager execution, we don't need to create a session; we can simply compute z, just like we do in Python. In order to enable eager execution, just call the tf.enable_eager_execution() function:

x = tf.constant(11)
y = tf.constant(11)
z = x*y

print z

It will return the following:

<tf.Tensor: id=789, shape=(), dtype=int32, numpy=121>

In order to get the output value, we can print the following:

z.numpy()

121

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset