Training of keras model get's slower after each repetition
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 Keras training gets slower every time you repeat an experiment, the problem is usually not that gradient descent somehow becomes inherently slower. More often, the process is accumulating state around the model: extra graphs, callbacks, file handles, datasets, or GPU memory pressure. The fix is to isolate each training run and measure where the slowdown is actually happening.
The Most Common Cause: Rebuilding Models Without Clearing State
A frequent pattern is training many models inside a loop while leaving the TensorFlow backend state alive. That can accumulate graph objects and memory allocations.
clear_session() is the first thing to try when repeated experiments grow slower over time.
Distinguish Epoch Slowdown from Run-to-Run Slowdown
There are two different symptoms.
- Each later epoch inside one training run gets slower.
- Each new call to
fit()in a repeated experiment loop gets slower.
The second case usually points to leaked state or a pipeline issue. The first case can come from callbacks, logging, dataset shuffling overhead, checkpointing, or CPU throttling.
Do not treat them as the same bug.
Check the Input Pipeline Before Blaming the Model
If the model trains quickly at the start and then slows down, data loading may be the real bottleneck. Python generators, disk reads, on-the-fly augmentation, and non-prefetched tf.data pipelines can all cause unstable throughput.
Caching and prefetching often help more than changing model architecture when the real issue is input starvation.
Watch Callbacks and Disk I/O
Training can degrade when you save a checkpoint, TensorBoard log, or CSV log too often. Writing to a slow disk on every epoch or every batch adds overhead that looks like model slowdown.
A practical test is to temporarily disable callbacks and compare timings.
If training becomes stable without callbacks, the model was not the problem.
GPU Memory Pressure Can Cause Secondary Slowdowns
On GPU systems, repeated training runs can fragment memory or push the process into a less efficient allocation pattern. Even if the code does not crash with an out-of-memory error, runtime can degrade as memory becomes tighter.
Useful checks include:
- monitoring GPU memory during repeated runs
- reducing batch size
- making sure old models are deleted before new ones are built
- avoiding unnecessary copies of training arrays
If you train many candidate models in one process, memory hygiene matters.
Use Timing Around the Real Steps
Instead of guessing, time the pieces separately.
You can apply the same idea to dataset creation, model construction, evaluation, and checkpoint saving. Once you know which phase is growing, the fix becomes much clearer.
Reuse Data, Not Model State
For repeated experiments, it is reasonable to reuse preprocessed tensors or cached datasets. It is usually a mistake to reuse model state unless you are intentionally fine-tuning from a previous checkpoint.
That distinction matters. Reusing data improves speed. Reusing hidden backend state often creates the slowdown you are trying to diagnose.
Common Pitfalls
- Rebuilding many models in one process without calling
tf.keras.backend.clear_session(). - Measuring only total runtime instead of timing model build, data loading, and callbacks separately.
- Using a slow Python data generator and assuming the neural network is the bottleneck.
- Saving checkpoints or logs too frequently.
- Ignoring GPU memory growth because the process still technically finishes.
Summary
- Repeated Keras slowdowns usually come from accumulated state, data pipelines, or I/O overhead.
- Clear the backend session between repeated experiment runs.
- Time the individual phases before changing model architecture.
- Optimize the input pipeline with caching and prefetching when data loading is the bottleneck.
- Remove unnecessary callbacks and manage GPU memory carefully in long experiment loops.
Related reading
- Training on imbalanced data using TensorFlow
- Training on imbalanced data using TensorFlow
- Training on sequences of sentences using Keras
- Training TensorFlow for Predicting a Column in a csv file
- Training Resnet deep neural network from scratch
- Training tensorflow im2txt fails with truncated record at
- Traveling salesman example with known global optimum
- Travelling Salesman with multiple salesmen?

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.