0
0
TensorFlowml~5 mins

TensorFlow architecture (eager vs graph execution)

Choose your learning style9 modes available
Introduction

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.

When you want to quickly test ideas and see results immediately.
When you need to debug your model step-by-step.
When you want to optimize your model for faster training and deployment.
When you want to save and share your model efficiently.
When running on devices with limited resources where speed matters.
Syntax
TensorFlow
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.

Examples
This runs immediately and prints 35.
TensorFlow
import tensorflow as tf

# Eager execution example
x = tf.constant(5)
y = tf.constant(7)
print(x * y)
This builds a graph for multiply and runs it, printing 35.
TensorFlow
import tensorflow as tf

@tf.function
def multiply(a, b):
    return a * b

result = multiply(tf.constant(5), tf.constant(7))
print(result)
Shows eager addition and graph function for add and square.
TensorFlow
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)))
Sample Model

This program shows how TensorFlow runs operations eagerly and with graph execution, printing results for both.

TensorFlow
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())
OutputSuccess
Important Notes

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.

Summary

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.