Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is eager execution in TensorFlow?
Eager execution runs operations immediately as they are called, like normal Python code. It is easy to debug and understand because results are returned right away.
Click to reveal answer
beginner
What is graph execution in TensorFlow?
Graph execution builds a computation graph first, then runs it all at once. This can be faster and more efficient for large models but harder to debug.
Click to reveal answer
beginner
How does eager execution help beginners?
Eager execution lets beginners see results immediately, making it easier to test and fix code step-by-step, just like regular Python programming.
Click to reveal answer
intermediate
Why might someone choose graph execution over eager execution?
Graph execution can optimize the whole computation, making it faster and better for deploying models in production where speed matters.
Click to reveal answer
intermediate
Can TensorFlow switch between eager and graph execution?
Yes, TensorFlow 2 uses eager execution by default but can convert code to graph execution using @tf.function to get speed benefits.
Click to reveal answer
What does eager execution in TensorFlow do?
ARuns operations immediately and returns results
BBuilds a graph before running operations
COnly works on GPUs
DRequires manual session management
✗ Incorrect
Eager execution runs operations immediately and returns results, making it easy to debug.
Which TensorFlow mode is usually faster for large models?
AGraph execution
BDebug mode
CInteractive mode
DEager execution
✗ Incorrect
Graph execution can optimize and run the whole computation graph efficiently, making it faster for large models.
How can you convert eager code to graph execution in TensorFlow 2?
AUse tf.Graph() manually
BUse tf.Session()
CUse @tf.function decorator
DUse tf.enable_eager_execution()
✗ Incorrect
The @tf.function decorator converts eager code into a graph for better performance.
Which is true about eager execution?
AIt is deprecated in TensorFlow 2
BIt is harder to debug
CIt requires building a graph first
DIt runs operations step-by-step immediately
✗ Incorrect
Eager execution runs operations step-by-step immediately, making debugging easier.
Why might graph execution be harder for beginners?
ABecause it runs code slowly
BBecause it requires understanding graphs and sessions
CBecause it only works on CPUs
DBecause it does not support variables
✗ Incorrect
Graph execution requires understanding how to build and run computation graphs, which can be confusing for beginners.
Explain the difference between eager execution and graph execution in TensorFlow.
Think about how and when TensorFlow runs your code.
You got /4 concepts.
Describe a situation where you would prefer eager execution over graph execution.
Consider what helps you understand your code better.
You got /3 concepts.
Practice
(1/5)
1. What is the main difference between eager execution and graph execution in TensorFlow?
easy
A. Eager execution requires a GPU, graph execution runs only on CPU.
B. Eager execution uses less memory than graph execution in all cases.
C. Graph execution is only for training, eager execution is only for inference.
D. Eager execution runs operations immediately, while graph execution builds a computation plan first.
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 D
Quick Check:
Eager vs Graph = Immediate vs Plan [OK]
Hint: Eager means now, graph means plan first [OK]
Common Mistakes:
Thinking graph execution runs immediately
Confusing hardware requirements
Assuming eager is only for inference
2. Which of the following is the correct way to convert a Python function to a TensorFlow graph function?
easy
A. Use @tf.function decorator above the function definition.
B. Call tf.convert_to_graph(function) before running it.
C. Wrap the function inside tf.Graph() and call it.
D. Set tf.enable_graph_mode(True) before defining the function.
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 A
Quick Check:
@tf.function converts to graph [OK]
Hint: Remember @tf.function for graph conversion [OK]
Common Mistakes:
Using non-existent TensorFlow functions
Trying to enable graph mode globally
Confusing tf.Graph() usage
3. Consider the following code snippet:
import tensorflow as tf
@tf.function
def add(a, b):
print('Running add')
return a + b
result1 = add(1, 2)
result2 = add(3, 4)
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 B
Quick Check:
Print runs once during tracing [OK]
Hint: Print inside @tf.function runs once [OK]
Common Mistakes:
Expecting print every call
Thinking print is disabled
Assuming error from print usage
4. You wrote this code:
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?
medium
A. Wrap multiply inside tf.function as well
B. Remove @tf.function decorator from call_multiply
C. Add .numpy() to the print call: print(call_multiply(2, 3).numpy())
D. Change multiply to use tf.multiply instead of * operator
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 C
Quick Check:
Tensor to number: use .numpy() [OK]
Hint: Use .numpy() to get number from tensor [OK]
Common Mistakes:
Expecting tensor to print as number
Removing @tf.function unnecessarily
Changing multiply without need
5. You want to speed up a TensorFlow model training loop by switching from eager to graph execution. Which approach correctly applies this change while keeping eager mode for debugging?
hard
A. Decorate the training step function with @tf.function and run training normally.
B. Set tf.config.experimental_run_functions_eagerly(True) before training.
C. Rewrite the entire model using tf.Graph() and tf.Session().
D. Disable eager execution globally using tf.compat.v1.disable_eager_execution().
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 A