Running a tensorflow program multiple times each time afresh
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
If you want to run a TensorFlow program repeatedly "from scratch", the real question is which state you need to reset. In modern TensorFlow, the usual sources of carry-over are Keras global state, random seeds, model objects kept alive in Python, and sometimes process-level resources such as GPU memory allocations.
Build the Model Inside a Function
The first rule is simple: create a new model for each run instead of reusing the old object. Wrapping the full experiment in a function gives each iteration a clean boundary.
This handles the most common case well. A fresh model is created each time, Keras state is cleared, and the seed is reset so runs are reproducible when that is what you want.
What clear_session() Actually Resets
tf.keras.backend.clear_session() resets Keras-managed global state such as layer name counters and references held by the Keras backend. It is especially useful when you build many models in a loop, because otherwise memory usage can grow over time.
What it does not do is magically erase every piece of state in your Python process. If you still keep references to old tensors, datasets, callbacks, or model objects, those can continue to exist until garbage collection removes them.
That means a clean rerun usually requires three things:
- create fresh model objects
- reset the seed if reproducibility matters
- avoid keeping old references in outer scopes
Use a Separate Process for Full Isolation
If you need a truly fresh run every time, process isolation is the most reliable approach. This matters when:
- GPU memory behavior becomes sticky
- native libraries hold process-level state
- one run can crash or corrupt the next
- you want strict isolation for benchmarking
A simple launcher can run the training script in a new Python process:
This is heavier than a function call, but it is the closest thing to "start over completely" without restarting your whole environment manually.
TensorFlow 1.x and Graph Resetting
If you are maintaining TensorFlow 1.x code or compatibility-mode graph code, graph reset also matters. In that older style, sessions and graphs were explicit runtime objects, so a fresh execution often meant both:
Then you would rebuild the graph and create a new session. In TensorFlow 2.x, eager execution and Keras-style model construction make that pattern less central, but you may still see it in legacy projects.
Decide Whether You Want Reproducibility or Diversity
Sometimes "afresh" means "independent random initialization every run." Other times it means "same starting conditions every run." Those are different goals.
For reproducible runs, set a known seed.
For genuinely different restarts, do not reuse the same seed each time.
Both are valid. The mistake is assuming that repeated calls without explicit seeding or cleanup are either fully reproducible or fully isolated. They are often neither.
Common Pitfalls
The biggest pitfall is reusing the same model instance and expecting fit() to behave like a brand-new experiment. It will continue training from the current weights.
Another common problem is forgetting that notebooks keep references alive. Even after calling clear_session(), variables in cells can still point at older objects.
GPU users often assume memory release is immediate. In practice, process-level isolation is sometimes the only dependable reset when running many experiments.
Finally, legacy TensorFlow advice about sessions and graphs is not always appropriate for TensorFlow 2.x. Make sure the reset strategy matches the programming model your code actually uses.
Summary
- Build a new model object for every run.
- Use
tf.keras.backend.clear_session()when creating models in loops. - Reset seeds explicitly if reproducibility matters.
- Use a separate process when you need strong isolation.
- Distinguish between TensorFlow 2.x Keras workflows and legacy TensorFlow 1.x graph workflows.
Related reading
- Running a Tensorflow program on an IPU Model throws an Illegal instruction core dumped error
- Running Keras model for prediction in multiple threads
- Running Keras with double precision fails
- Running multiple tensorflow sessions concurrently
- Running Adam Optimizer
- Running session using tensorflow c api is significantly slower than using python
- Running Apache Beam python pipelines in Kubernetes
- Running Bash commands in Python
.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 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.