Hot to fix Tensorflow model not running in Eager mode with .fit?
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
By default, TensorFlow 2.x runs model.fit() in graph mode (via tf.function) for performance, even though eager execution is enabled globally. This means print() statements, pdb breakpoints, and NumPy operations inside your model's call() method do not execute as expected during training. To force eager execution during fit(), pass run_eagerly=True to model.compile(). This is essential for debugging custom layers, custom training steps, and complex model logic, but should be disabled for production training since graph mode is significantly faster.
The Problem
Fix 1: run_eagerly=True in compile()
This forces fit() to run every operation eagerly, including call(), custom loss functions, and custom metrics.
Fix 2: tf.config.run_functions_eagerly(True)
Force all tf.function-decorated code to run eagerly:
Fix 3: tf.debugging for Graph Mode
If you want graph-mode speed but still need debugging output:
Debugging Custom Layers
Custom Training Step Debugging
Checking Current Execution Mode
Performance Impact
Common Pitfalls
- Leaving
run_eagerly=Truein production: Eager mode is 2-5x slower than graph mode because it cannot optimize the computation graph. Userun_eagerly=Trueonly for debugging, then switch back toFalse(or omit it) for actual training. Consider using an environment variable to toggle:run_eagerly=os.environ.get('DEBUG', False). - Using
print()instead oftf.print()in model code: Pythonprint()only executes during graph tracing (once), not during actual training. Usetf.print()for output that should appear every step.tf.printis a TensorFlow operation that executes within the graph. - Calling
.numpy()inside model code without eager mode:tensor.numpy()only works in eager mode. In graph mode, it raisesAttributeError: 'Tensor' object has no attribute 'numpy'. Usetf.printortf.debuggingassertions instead, or enablerun_eagerly=True. - Confusing
tf.executing_eagerly()withmodel.run_eagerly:tf.executing_eagerly()returnsTrueat the top level in TF2, but insidemodel.fit()thecall()method runs inside atf.function(graph mode) regardless. Onlymodel.compile(run_eagerly=True)forces the training loop itself to be eager. - Custom loss functions not working as expected in graph mode: If your custom loss uses Python control flow (
if,forwith Python variables), it may behave differently in graph mode vs eager mode. Usetf.cond,tf.while_loop, orrun_eagerly=Trueto ensure correct behavior, then convert to TF ops for production.
Summary
model.fit()runs in graph mode by default for performance, even though TF2 enables eager execution globally- Pass
run_eagerly=Truetomodel.compile()to force eager execution during training (for debugging) - Use
tf.print()instead ofprint()andtf.debuggingassertions for output that works in both modes tf.config.run_functions_eagerly(True)forces alltf.functioncalls to run eagerly (global setting)- Always disable eager mode for production training — graph mode is 2-5x faster
Related reading
- Hot to fix Tensorflow model not running in Eager mode with .fit?
- How-to run TensorFlow on multiple core and threads
- How are the new tf.contrib.summary summaries in TensorFlow evaluated?
- How can I access the filenames gathered by tf.data.Dataset.list_files?
- How a typical cluster of five servers can tolerate the failure of any two servers?
- How are filters selected for convolutional neural networks
- How about a walk through of the lifecycle of ember/ember-data objects. Or tips/hints for debugging Ember.js and Ember-Data?
- How can a dead thread be restarted?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my 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.