TensorFlow
machine learning
neural networks
graph setup
programming

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.

Practice ML system design

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.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, shape=(None, 1))
6w = tf.Variable([[2.0]], dtype=tf.float32)
7b = tf.Variable([1.0], dtype=tf.float32)
8y = tf.matmul(x, w) + b
9
10with tf.compat.v1.Session() as sess:
11    sess.run(tf.compat.v1.global_variables_initializer())
12    result = sess.run(y, feed_dict={x: [[3.0], [4.0]]})
13    print(result)

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.

python
1import tensorflow as tf
2
3x = tf.constant([[3.0], [4.0]])
4w = tf.constant([[2.0]])
5b = tf.constant([1.0])
6
7y = tf.matmul(x, w) + b
8print(y.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:

python
1import tensorflow as tf
2
3@tf.function
4def compute(x):
5    w = tf.constant([[2.0]])
6    b = tf.constant([1.0])
7    return tf.matmul(x, w) + b
8
9
10x = tf.constant([[3.0], [4.0]])
11print(compute(x))

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:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1)
7])

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 print and 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.function or 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design