how to store numpy arrays as tfrecord?
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
TFRecord is TensorFlow’s binary record format for storing sequences of serialized examples efficiently. When your source data starts as NumPy arrays, the main question is not whether TFRecord can store them, but how to serialize the array contents and enough metadata to reconstruct the original shape and dtype correctly.
A Practical Pattern: Store Tensor Bytes Plus Metadata
One reliable approach is:
- Convert the NumPy array to a TensorFlow tensor
- Serialize it with
tf.io.serialize_tensor - Store the resulting bytes in a
tf.train.Example - Save shape or label metadata alongside it if needed
This avoids manually flattening and rebuilding arrays unless you specifically want that control.
Write a NumPy Array to TFRecord
Here is a complete example that writes arrays and labels.
This stores each array as one record.
Read the TFRecord Back into Tensors
To read the file, define the schema and parse the serialized tensor back.
Because the tensor bytes include shape information, parse_tensor reconstructs the original dimensions.
When to Store Extra Metadata
Sometimes you still want explicit metadata, such as:
- Original shape for validation
- Dtype name for heterogeneous datasets
- Sample ID or filename
- Class labels or timestamps
That is especially helpful when your pipeline spans multiple languages or tools and you want the record format to stay self-describing.
Why TFRecord Helps at Scale
You could save NumPy arrays directly with .npy or .npz, and that is often fine for smaller workflows. TFRecord becomes attractive when you want:
- Sequential streaming reads
- Easy sharding into many files
- Integration with
tf.data - Consistent training input pipelines
It is less about replacing NumPy entirely and more about packaging examples for TensorFlow ingestion efficiently.
Common Pitfalls
A common mistake is flattening the NumPy array into raw bytes without storing enough information to rebuild shape and dtype later. The data is technically saved, but not meaningfully recoverable.
Another mistake is mixing dtypes during writing and then hard-coding the wrong out_type in tf.io.parse_tensor. That leads to parse errors or incorrect values.
A third mistake is writing one giant TFRecord file for a large dataset. In practice, sharding into multiple files usually works better for throughput and operational handling.
Summary
- NumPy arrays can be stored in TFRecord by serializing them as tensor bytes.
- '
tf.io.serialize_tensorandtf.io.parse_tensorare a clean pair for this workflow.' - Add metadata such as labels or shape when the dataset needs to be self-describing.
- TFRecord is especially useful when the next step is a
tf.datapipeline. - Be careful to keep dtype and reconstruction logic consistent between writing and reading.
Related reading
- How to stratify the training and testing data in Scikit-Learn?
- How to structure Machine Learning projects using Object Oriented programming in Python?
- How to suppress all autograph warnings from Tensorflow?
- How to suppress specific warning in Tensorflow Python
- How to subtract column's values from column's values if columns have different quantity of rows by matching names
- How to sum leading diagonal of table in R
- How to subscribe to a list of multiple kafka wildcard patterns using kafka-python?
- How to sum all the values in a dictionary?
.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.