Tensorflow create minibatch from numpy array 2 GB
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
When a NumPy array is around 2 GB, the real problem is not minibatching itself. The real problem is avoiding unnecessary copies and avoiding a training loop that tries to move the whole array through Python on every step. TensorFlow's tf.data pipeline is the right tool because it batches lazily and keeps the input path closer to the runtime.
Avoid Feeding the Whole Array Repeatedly
A common beginner pattern is calling Session.run(..., feed_dict=...) or repeatedly slicing Python arrays inside a training loop. With very large arrays, that creates extra copies, heavy Python overhead, and unpredictable memory pressure.
If the array already fits in RAM and is a normal numpy.ndarray, tf.data.Dataset.from_tensor_slices is the cleanest starting point.
This does not train on the entire array at once. It exposes minibatches as the dataset is consumed.
Use Memory Mapping When the Array Is Too Big for Comfortable RAM Use
If 2 GB is technically loadable but leaves the system under memory pressure, store the data as a memory-mapped NumPy array and batch from that. np.memmap lets the operating system page data in as needed instead of forcing one giant in-memory allocation.
This is a strong pattern when the bottleneck is memory footprint rather than raw model compute.
Generator Pipelines Are Useful for More Complex Storage Layouts
If the data is not stored as one clean contiguous array, use from_generator so each batch or sample is yielded on demand.
This is slightly more Python-heavy than from_tensor_slices, but it gives you control when the source data is irregular or lives in custom binary storage.
Shuffle Deliberately
With very large arrays, a full in-memory shuffle may be expensive. TensorFlow's shuffle buffer gives a practical compromise between randomness and memory usage.
The larger the buffer, the closer the shuffle is to a full permutation. The smaller the buffer, the lower the memory overhead.
Integrate Directly with Keras Training
Once the dataset yields correctly shaped minibatches, pass it straight into model.fit.
This is cleaner than manually slicing batches in Python loops, and it gives TensorFlow room to pipeline data loading with compute.
When a Single NumPy Array Is the Wrong Storage Format
If the dataset is much larger than memory or must be shared across machines, a single .npy or in-memory array may be the wrong long-term format. In those cases, consider sharded files such as TFRecords or on-disk chunked formats that stream naturally into tf.data.
Minibatching from a 2 GB NumPy array is possible. It is just not always the best architecture if the dataset keeps growing.
Common Pitfalls
A common mistake is creating minibatches by copying slices into new Python lists every training step. That wastes memory bandwidth and makes the CPU the bottleneck.
Another mistake is assuming that because the array fits in RAM once, every downstream step is cheap. Extra copies during shuffle, cast, or feed operations can still exhaust memory.
People also often overlook prefetch, which means data loading and model execution happen strictly in sequence.
Finally, if performance is poor, measure whether the problem is the model, the storage format, or the Python input path before blaming TensorFlow itself.
Summary
- Use
tf.datato minibatch large NumPy arrays lazily. - '
from_tensor_slicesis the cleanest path when the array is already in memory.' - Use
np.memmapwhen the array is too large for comfortable RAM use. - Prefer dataset pipelines over manual Python batch slicing in training loops.
- If the dataset keeps growing, consider streaming-friendly storage instead of one giant array.
Related reading
- Tensorflow create tf.NodeDef and set attributes
- Tensorflow Creating a graph in a class and running it outside
- tensorflow creating mask of varied lengths
- Tensorflow Cross Device Communication
- Tensorflow Data API - prefetch
- Tensorflow dataset data preprocessing is done once for the whole dataset or for each call to iterator.next?
- TensorFlow DataSet API causes graph size to explode
- Tensorflow Dictionary lookup with String tensor

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.