What is the utility of Tensor as opposed to EagerTensor in Tensorflow 2.0?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In TensorFlow 2.0, the concept of Tensors is central to the framework, as they are the basic units of data representation. Despite the simplicity and elegance of using `EagerTensor` due to the default eager execution mode, understanding the utility of the `Tensor` in TensorFlow 2.0 requires a deeper dive into scenarios where the symbolic `Tensor` becomes important.
Understanding Tensors in TensorFlow 2.0
A `Tensor` in TensorFlow is an n-dimensional array or list that holds data of a particular type. TensorFlow 2.0 brings with it eager execution, where operations are performed immediately as they are called within Python. The distinction between `Tensor` and `EagerTensor` emerges because `EagerTensors` are the direct result of operations executed eagerly, while `Tensors` more commonly refer to symbolic tensors used in graph mode.
Eager Execution vs. Graph Execution
- Eager Execution: Operations return `EagerTensor` objects and are executed immediately. This is more intuitive because it behaves like typical Python code.
- Graph Execution: Operations with `Tensor` objects that form part of a computation graph are not executed immediately. Instead, these operations are compiled and optimized to run in a session.
When to Use Symbolic Tensors
Understanding when to use symbolic `Tensors` as opposed to `EagerTensors` can be beneficial in several contexts:
- Performance Optimization:Graph execution often enables more significant optimizations than eager execution. In many cases, building a model using symbolic `Tensors` yields a graph that TensorFlow can optimize and run efficiently, taking full advantage of distributed environments and hardware accelerations.
- Deployment to Production:Using graphs (with symbolic `Tensors`) allows the exporting of models in a universal format that can be run independently of the Python environment using tools like TensorFlow Serving.
- Advanced Features:Features like AutoGraph automatically convert a subset of Python constructs into their equivalent TensorFlow graph operations. This allows for using more complex control flows and operations that are not directly supported in eager execution.
- Memory Efficiency:Graph execution can manage memory more efficiently, as it can reuse buffer allocations. Symbolic `Tensors` facilitate this optimization.
Example: Using Symbolic Tensors in @tf.function
The `@tf.function` decorator is used to create a callable graph by converting a Python function to its graph form:

