tf.SequenceExample with multidimensional arrays
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
tf.train.SequenceExample is useful when each training example contains a sequence of items and each item may itself contain structured numeric data. The tricky part with multidimensional arrays is that SequenceExample stores scalar lists, so you typically flatten each time-step array, store its shape separately, and reshape it again when parsing.
How SequenceExample Is Structured
A SequenceExample has two parts:
- '
contextfor features that describe the whole example,' - '
feature_listsfor sequence data where each step can have its own feature values.'
That maps well to data such as:
- a sequence of image embeddings,
- a sequence of sensor matrices,
- a variable-length list of frames or feature vectors.
For multidimensional data, the usual design is:
- keep per-example metadata such as shape in
context, - store each sequence step as a flattened float or int list in
feature_lists.
Writing a Sequence of 2D Arrays
Suppose each time step is a 2 x 3 matrix. We can flatten each matrix before storing it.
The multidimensional structure is not lost, but it is represented indirectly: flat values plus shape metadata.
Parsing and Reshaping
When reading the serialized record back, parse the flat sequence and then reshape each step to its original dimensions.
The FixedLenSequenceFeature([6], tf.float32) part works because each flattened matrix has exactly six values.
Why Flattening Is Usually the Easiest Option
You could store shape information in many different ways, but flattening each step keeps the record format simple. TensorFlow input pipelines generally work best when every step in the sequence has a predictable per-step feature length.
That means SequenceExample is often a good fit for:
- frame embeddings,
- fixed-size per-step sensor windows,
- and any sequence where length varies but inner shape stays stable.
If the inner shape changes too often, the serialization logic and parsing code become much more awkward, which is usually a sign that padding or a different record design may be cleaner.
Common Pitfalls
- Trying to store a multidimensional tensor directly without flattening it into feature values.
- Forgetting to store enough shape metadata to reconstruct the original structure.
- Using
FixedLenSequenceFeatureeven though the flattened size varies between steps. - Assuming
SequenceExampleis always the best format when the data is highly irregular. - Mixing per-example metadata and per-step sequence data without a clear separation between
contextandfeature_lists.
Summary
- '
tf.train.SequenceExampleis a good fit for sequence data with per-example metadata and per-step values.' - For multidimensional arrays, flatten each step before writing and store shape information separately.
- Parse the flat values back with
parse_single_sequence_exampleand reshape them afterward. - The format works best when each time step has a consistent inner shape.
- If the inner shapes vary too much, padding or a different serialization strategy may be a better choice.
Related reading
- tf.shape get wrong shape in tensorflow
- tf.tape.gradient returns None for certain losses
- tf.train.init_from_checkpoint does not initialize variables created with tf.Variable
- tf.train.MonitoredTrainingSession and reinitializable iterator from Dataset
- tf.transform add preprocessing to Keras model?
- The added layer must be an instance of class Layer. Found tensorflow.python.keras.engine.input_layer.InputLayer
- The best shortest path algorithm
- The best way to calculate the height in a binary search tree? balancing an AVL-tree

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.