TensorFlow create dataset from numpy array
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
Creating a TensorFlow dataset from NumPy arrays is one of the simplest ways to build a clean training pipeline. The key is not just converting the arrays, but also preserving shape and dtype, then adding batching, shuffling, and prefetching so the input pipeline behaves well during training.
The Basic Conversion
For in-memory arrays, the usual entry point is tf.data.Dataset.from_tensor_slices.
This creates a dataset where each element is one sample and one label. The first dimension of both arrays must line up, because TensorFlow slices them together row by row.
Add Shuffle, Batch, and Prefetch
A raw dataset is usually not enough for model training. The typical next steps are:
- shuffle the training set
- batch records together
- prefetch to overlap data loading with model execution
This is the standard baseline for small and medium in-memory datasets.
Dtypes Matter
Many TensorFlow issues that look like dataset bugs are actually dtype bugs. NumPy defaults do not always match what Keras layers or losses expect.
If needed, cast before dataset creation:
Doing this up front avoids confusing errors later during training.
Multi-Input Models
If your model has several inputs, the dataset structure should match the model input structure. A dictionary is often the cleanest option.
This lines up naturally with Keras models whose input layers are named num and cat.
Splitting Train and Validation
For NumPy-backed datasets, split the arrays first, then build separate datasets.
This keeps the boundary explicit and avoids training and validation pipelines drifting apart.
Use the Dataset With Keras
Once the pipeline is built, you can pass it directly to model.fit.
That keeps training code clean and moves input logic into the tf.data pipeline where it belongs.
Common Pitfalls
The biggest mistake is passing arrays with mismatched first dimensions. from_tensor_slices expects all input components to align sample by sample.
Another issue is forgetting dtype control. Float inputs, integer class labels, and one-hot labels each have different expectations depending on the model and loss function.
Developers also skip shuffling on the training set, which can create biased mini-batches and unstable optimization behavior.
Finally, do not build one dataset and then try to treat it as both training and validation data. Split the raw arrays first so each pipeline has a clear purpose.
Summary
- Use
tf.data.Dataset.from_tensor_slicesfor NumPy arrays held in memory. - Check shape alignment and dtypes before building the dataset.
- Add shuffle, batch, and prefetch for practical model training.
- Match dataset structure to the model input structure for multi-input networks.
- Split arrays first, then create separate training and validation datasets.
Related reading
- TensorFlow create dataset from numpy array
- Tensorflow create minibatch from numpy array 2 GB
- Tensorflow create tf.NodeDef and set attributes
- Tensorflow Creating a graph in a class and running it outside
- Tensorflow Cross Device Communication
- Tensorflow CUDA - CUPTI error CUPTI could not be loaded or symbol could not be found
- tensorflow creating mask of varied lengths
- Tensorflow Data API - prefetch
.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.