TensorFlow can run your code in two ways: step-by-step (eager) or by building a plan first (graph). This helps you choose speed or easy debugging.
TensorFlow architecture (eager vs graph execution)
import tensorflow as tf # Eager execution is enabled by default x = tf.constant(3) y = tf.constant(4) sum = x + y print(sum) # To use graph execution, decorate a function @tf.function def add(a, b): return a + b result = add(x, y) print(result)
Eager execution runs operations immediately and returns results.
Graph execution builds a computation graph and runs it later for speed.
import tensorflow as tf # Eager execution example x = tf.constant(5) y = tf.constant(7) print(x * y)
import tensorflow as tf @tf.function def multiply(a, b): return a * b result = multiply(tf.constant(5), tf.constant(7)) print(result)
import tensorflow as tf # Mixing eager and graph print(tf.constant(2) + tf.constant(3)) @tf.function def add_and_square(a, b): c = a + b return c * c print(add_and_square(tf.constant(2), tf.constant(3)))
This program shows how TensorFlow runs operations eagerly and with graph execution, printing results for both.
import tensorflow as tf # Eager execution example print('Eager execution:') x = tf.constant(10) y = tf.constant(20) sum_eager = x + y print('Sum:', sum_eager) # Graph execution example @tf.function def compute_sum(a, b): return a + b print('\nGraph execution:') sum_graph = compute_sum(x, y) print('Sum:', sum_graph) # Check if eager is enabled print('\nIs eager execution enabled?', tf.executing_eagerly())
Time complexity: Both eager and graph execution have similar operation costs, but graph execution can optimize and run faster overall.
Space complexity: Graph execution may use more memory to store the computation graph.
Common mistake: Forgetting to use @tf.function for graph execution or expecting eager behavior inside graph functions.
When to use: Use eager for easy debugging and quick tests; use graph for production and speed.
TensorFlow runs code eagerly by default for simplicity.
Graph execution builds a plan for faster, optimized runs.
You can switch between them using @tf.function decorator.