At what stage is a tensorflow graph set up?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The answer depends on which TensorFlow execution model you are using. In older TensorFlow 1 style code, the graph is built first and run later. In modern TensorFlow 2, operations run eagerly by default, and a graph is created only when TensorFlow traces code through mechanisms such as @tf.function, model export, or certain internal optimizations.
TensorFlow 1 Style: Build First, Execute Later
In TensorFlow 1, graph setup happens during the model-definition stage. You write operations, TensorFlow adds them to a computation graph, and nothing actually computes until a session executes the graph.
In this style:
- placeholders, variables, and operations are added to the graph during definition
- actual numerical work happens later inside
Session.run
So the graph is set up before execution.
TensorFlow 2 Style: Eager by Default
TensorFlow 2 changed the default execution model. Ordinary operations execute immediately, more like standard Python and NumPy.
Here, no explicit graph-construction stage is required. The operations run eagerly as Python executes each line.
That is why the answer to "when is the graph set up?" in TensorFlow 2 is often: not at all, unless you ask TensorFlow to trace one.
When TensorFlow 2 Does Build a Graph
Even in TensorFlow 2, graphs still exist. They are created when TensorFlow traces Python code into a graph function for performance, serialization, or deployment.
The most common trigger is @tf.function:
The first time compute is called with a compatible input signature, TensorFlow traces the function and builds a graph representation. Later calls can reuse that traced graph.
So in TensorFlow 2, graph setup typically happens at tracing time, not necessarily when you define the Python function.
What Happens During Keras Model Training
Keras on TensorFlow 2 often mixes eager development with graph-based execution under the hood. When you define a model:
you are mostly defining layer structure. During model.compile and especially model.fit, TensorFlow may trace parts of the training step into graphs for efficiency.
That means there are really two stages to think about:
- model structure is declared when you create layers and connect them
- executable graphs may be traced later when training or inference begins
This is why some errors appear only on the first batch. TensorFlow may be tracing and validating the graph at that moment.
Why the Distinction Matters
Knowing when the graph is created helps with debugging:
- in eager mode, Python
printand standard debugging work naturally - in traced graph mode, execution is deferred into a compiled representation
- shape and type errors may appear when tracing starts, not when the function is defined
It also helps explain performance. Eager execution is convenient for experimentation, while traced graphs let TensorFlow optimize execution and export models.
Common Pitfalls
The most common mistake is answering the question as if TensorFlow had only one execution model. TensorFlow 1 and TensorFlow 2 behave differently, and many explanations become misleading when they ignore that split.
Another mistake is assuming @tf.function runs Python exactly line by line each call. It does not. TensorFlow traces the function and then executes the graph, so some Python-side behavior only happens during tracing.
A third issue is thinking model construction and graph construction are always the same event. In Keras, the layer graph and the traced execution graph are related but not identical concepts.
Finally, developers sometimes debug graph-traced code as if it were eager code. If an error appears only during fit, inspect input shapes, dtypes, and tracing behavior rather than expecting immediate eager-style feedback.
Summary
- In TensorFlow 1 style code, the graph is built during definition and executed later in a session.
- In TensorFlow 2, operations run eagerly by default, so no graph is required for ordinary code.
- TensorFlow 2 creates graphs when tracing code, commonly through
@tf.functionor Keras training internals. - Model definition time and graph tracing time are often different stages.
- The exact answer depends on whether you are using old graph mode, eager mode, or a traced function in TensorFlow 2.
Related reading
- Attach a queue to a numpy array in tensorflow for data fetch instead of files?
- Attach a queue to a numpy array in tensorflow for data fetch instead of files?
- Attempting to reset tensorflow graph when using keras, failing
- Attention Layer throwing TypeError Permute layer does not support masking in Keras
- Attempting to perform BLAS operation using StreamExecutor without BLAS support error occurs
- AttributeError 'Adam' object has no attribute 'build' during unpickling
- Attempt to insert non-property list object when trying to save a custom object in Swift 3
- Attempt to set a non-property-list object as an NSUserDefaults

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.