Resource Exhausted when training a neural network - keras
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
In Keras, a ResourceExhausted error usually means the model, batch, or intermediate tensors do not fit into available memory, most often GPU memory. The message often appears during forward or backward passes when TensorFlow tries to allocate a large tensor and cannot. The fix is usually not one magic switch, but a combination of reducing memory pressure and understanding where the memory is going.
What Usually Consumes the Memory
Training memory is not only the model weights. A training step also needs space for:
- activations from each layer
- gradients during backpropagation
- optimizer state such as momentum or Adam statistics
- the input batch itself
That is why a model that fits for inference can still fail during training. Backpropagation is more memory-intensive than simple forward prediction.
The Fastest Fix: Reduce Batch Size
The first thing to try is usually a smaller batch size.
If the error happens at batch_size=64, try 32, 16, or 8. This is often the highest-leverage change because activations scale directly with batch size.
Reduce Model Size or Input Size
If lowering the batch size is not enough, the next step is reducing model footprint.
Common options are:
- fewer layers
- fewer filters or hidden units
- smaller input images
- replacing a
Flattenlayer with pooling
For example, this version is often much lighter than a large flatten-based network:
A Flatten layer after large feature maps often explodes parameter count and memory usage.
Enable Memory Growth on GPU
TensorFlow can reserve large chunks of GPU memory up front. Allowing memory growth can make notebook and shared-GPU environments behave better.
This does not make the GPU larger, but it can reduce wasteful allocation behavior and improve coexistence with other workloads.
Mixed Precision Can Help
On supported hardware, mixed precision can cut memory usage and often speed training too.
This is especially useful on modern GPUs designed for lower-precision matrix math. Just remember that output layers or losses may need careful dtype handling in some models.
Clean Up Old Models in Notebook Sessions
If you are iterating in a notebook, old graphs and model objects can accumulate. Clear them before rebuilding a new model.
This is not a substitute for reducing true memory demand, but it helps when experimentation leaves stale state around.
Input Pipelines Matter Too
Sometimes the training input pipeline is the hidden memory problem. Large preloaded arrays, aggressive prefetch settings, or duplicated datasets can consume far more RAM than expected.
TensorFlow datasets are often safer than loading everything into giant Python structures.
Even here, be mindful that batching still affects device memory usage.
Common Pitfalls
The biggest mistake is focusing only on model weights. During training, activations and optimizer state often consume as much or more memory than the raw parameter tensors.
Another mistake is using a large Flatten layer after high-resolution convolution features. That creates enormous dense layers very quickly.
People also keep retrying the same model in a notebook without clearing old state, which makes memory behavior look worse than it really is.
Finally, do not assume the fix is always a stronger GPU. Often a smaller batch, lighter architecture, or mixed precision solves the immediate issue without changing hardware.
Summary
- '
ResourceExhaustedusually means the training step needs more memory than is available.' - Reducing batch size is often the fastest and most effective fix.
- Smaller models and smaller inputs reduce memory pressure substantially.
- GPU memory growth and mixed precision can help in the right environments.
- In notebook workflows, clear old model state before running new experiments.
Related reading
- Restore subset of variables in Tensorflow
- Restoring a Tensorflow model that uses Iterators
- Restoring TensorFlow model
- Retrain Tensorflow final layer but still use previous Imagenet classes
- Restore original text from Keras’s imdb dataset
- Restore variables that are a subset of new model in Tensorflow?
- Resources for working with Machine Learning in F
- Restoring saved TensorFlow model to evaluate on test set
.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.