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)
Start learning this pattern below
Jump into concepts and practice - no test required
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.
Practice
Solution
Step 1: Understand eager execution behavior
Eager execution runs TensorFlow operations immediately as they are called, making it easy to debug and understand.Step 2: Understand graph execution behavior
Graph execution builds a computation graph first, then runs it for better performance and optimization.Final Answer:
Eager execution runs operations immediately, while graph execution builds a computation plan first. -> Option DQuick Check:
Eager vs Graph = Immediate vs Plan [OK]
- Thinking graph execution runs immediately
- Confusing hardware requirements
- Assuming eager is only for inference
Solution
Step 1: Recall TensorFlow's method to switch execution modes
TensorFlow uses the @tf.function decorator to convert a Python function into a graph function.Step 2: Evaluate other options for correctness
tf.convert_to_graph and tf.enable_graph_mode do not exist; wrapping in tf.Graph() is not the standard way.Final Answer:
Use @tf.function decorator above the function definition. -> Option AQuick Check:
@tf.function converts to graph [OK]
- Using non-existent TensorFlow functions
- Trying to enable graph mode globally
- Confusing tf.Graph() usage
import tensorflow as tf
@tf.function
def add(a, b):
print('Running add')
return a + b
result1 = add(1, 2)
result2 = add(3, 4)What will be printed when this code runs?
Solution
Step 1: Understand print behavior inside @tf.function
When a function is decorated with @tf.function, it runs as a graph. Python print runs only once during graph tracing, not on every call.Step 2: Analyze the calls to add()
The first call triggers tracing and prints 'Running add'. The second call uses the compiled graph and does not print again.Final Answer:
Running add -> Option BQuick Check:
Print runs once during tracing [OK]
- Expecting print every call
- Thinking print is disabled
- Assuming error from print usage
import tensorflow as tf
def multiply(a, b):
return a * b
@tf.function
def call_multiply(x, y):
return multiply(x, y)
print(call_multiply(2, 3))But the output is a Tensor object, not a number. How can you fix it to print the actual number?
Solution
Step 1: Understand output type of @tf.function
Functions decorated with @tf.function return TensorFlow tensors, not plain Python numbers.Step 2: Convert tensor to number for printing
Use the .numpy() method on the tensor to get the actual number value for printing.Final Answer:
Add .numpy() to the print call: print(call_multiply(2, 3).numpy()) -> Option CQuick Check:
Tensor to number: use .numpy() [OK]
- Expecting tensor to print as number
- Removing @tf.function unnecessarily
- Changing multiply without need
Solution
Step 1: Identify how to switch to graph execution selectively
Using @tf.function on the training step compiles it to a graph, speeding execution while keeping eager mode elsewhere.Step 2: Evaluate other options for drawbacks
Setting experimental_run_functions_eagerly(True) forces eager mode (slower). Rewriting with tf.Graph() and tf.Session() is outdated. Disabling eager globally removes debugging ease.Final Answer:
Decorate the training step function with @tf.function and run training normally. -> Option AQuick Check:
@tf.function speeds training, keeps eager debugging [OK]
- Forcing eager mode instead of graph
- Using old TensorFlow 1.x APIs
- Disabling eager globally losing debugging
