"For long, the big complaint about TensorFlow was *It's not flexible for debugging!* With the advent of TensorFlow 2.0, that changed drastically.\n",
"\n",
"Now TensorFlow allows you to run the oprations **eagerly**. That means, you can run TensorFlow operations by Python and return the outputs to Python again. That creates a lot of flexibility, especially for debugging. \n",
"\n",
"But there are some merits in NOT using the eagerly option. You can run operations on TensorFlow graphs that in some scenarios leads to significant speed up. According to TensorFlow:\n",
"\n",
"> Graphs are data structures that contain a set of [tf.Operation](https://www.tensorflow.org/api_docs/python/tf/Operation) objects, which represent units of computation; and [tf.Tensor](https://www.tensorflow.org/api_docs/python/tf/Tensor) objects, which represent the units of data that flow between operations. They are defined in a [tf.Graph](https://www.tensorflow.org/api_docs/python/tf/Graph) context. Since these graphs are data structures, they can be saved, run, and restored all without the original Python code.\n",
"\n",
"Let's have some example for transforming functions to graphs!\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "iJoLp_aUvBFR"
},
"source": [
"# Loading necessary libraries\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"import timeit"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "8a9c8qUj-MSj"
},
"source": [
"### Operation\n",
"\n",
"We can take a Python function on graph with [@tf.function](https://www.tensorflow.org/api_docs/python/tf/function) decorator."
For long, the big complaint about TensorFlow was *It's not flexible for debugging!* With the advent of TensorFlow 2.0, that changed drastically.
Now TensorFlow allows you to run the oprations **eagerly**. That means, you can run TensorFlow operations by Python and return the outputs to Python again. That creates a lot of flexibility, especially for debugging.
But there are some merits in NOT using the eagerly option. You can run operations on TensorFlow graphs that in some scenarios leads to significant speed up. According to TensorFlow:
> Graphs are data structures that contain a set of [tf.Operation](https://www.tensorflow.org/api_docs/python/tf/Operation) objects, which represent units of computation; and [tf.Tensor](https://www.tensorflow.org/api_docs/python/tf/Tensor) objects, which represent the units of data that flow between operations. They are defined in a [tf.Graph](https://www.tensorflow.org/api_docs/python/tf/Graph) context. Since these graphs are data structures, they can be saved, run, and restored all without the original Python code.
Let's have some example for transforming functions to graphs!
"""
# Loading necessary libraries
importtensorflowastf
importnumpyasnp
importtimeit
"""### Operation
We can take a Python function on graph with [@tf.function](https://www.tensorflow.org/api_docs/python/tf/function) decorator.
"""
@tf.function
defmultiply_fn(a,b):
returntf.matmul(a,b)
# Create some tensors
a=tf.constant([[0.5,0.5]])
b=tf.constant([[10.0],[1.0]])
# Check function
print('Multiple a of shape {} with b of shape {}'.format(a.shape,b.shape))
print(multiply_fn(a,b).numpy())
# Function without neing take to graph, i.e., with eager execution.
defadd_fn(a,b):
returntf.add(a,b)
# Create some tensors
a=tf.constant([[0.5,0.5]])
b=tf.constant([[10.0],[1.0]])
# Check function
print('Add a of shape {} with b of shape {}'.format(a.shape,b.shape))
print(add_fn(a,b).numpy())
"""### Speedup
Now let's define a custom model and run it:
1. eagerly
2. on graph
To check how to define models refer to: https://www.tensorflow.org/api_docs/python/tf/keras/Model