TensorFlow Example vs SequenceExample
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.Example and tf.train.SequenceExample are both TensorFlow protocol buffer messages used to serialize training data, often inside TFRecord files. The difference is not about file format quality; it is about the shape of the data you need to store.
Use Example for Fixed-Shape Records
Example is the simpler structure. It stores one map of feature names to values, where each value is one of TensorFlow's basic list types:
- '
BytesList' - '
FloatList' - '
Int64List'
This works well when each record has the same logical fields, such as:
- one image and one label
- one row of tabular features
- one text field plus one class id
Here is a minimal runnable example:
Everything belongs to one flat record. If a feature is multi-valued, you still store it as a list, but conceptually it remains part of a single example.
Use SequenceExample for Ordered Variable-Length Data
SequenceExample adds structure for sequences. It separates data into:
- '
context, for features that describe the whole record' - '
feature_lists, for ordered features that change over time or by step'
This is useful for:
- sentences represented as token sequences
- clickstream events
- frame-level video features
- time series with per-step measurements
Example:
The steps feature is ordered. That is the key difference: SequenceExample understands that some features are sequences rather than plain per-record attributes.
How To Choose Between Them
Ask two questions:
- Does each record have one fixed set of fields?
- Or does each record contain an ordered series of per-step values?
If the answer to the first question is yes, use Example. If the answer to the second question is yes, use SequenceExample.
There is some overlap. You can sometimes force sequence data into Example by storing padded arrays or serialized tensors. But that usually makes parsing less explicit and harder to maintain.
SequenceExample becomes particularly useful when lengths vary across records. For example, one sentence may have 5 tokens and another may have 30. Representing both naturally is much cleaner with feature_lists.
Parsing Differences
The parsing APIs reflect the structural difference. Example is parsed with fixed or variable-length feature specs. SequenceExample uses separate specs for context features and sequence features.
That separation is valuable because it keeps metadata and sequential features distinct.
Storage and Pipeline Considerations
Both messages can be written to TFRecord files and consumed with tf.data.TFRecordDataset. So the operational pipeline is similar:
- serialize records
- write them to TFRecord
- read them with a dataset
- parse each record into tensors
The difference is in how much structure you preserve. If your model depends on order, sequence length, or per-step alignment, SequenceExample usually maps more directly to the training task.
Common Pitfalls
One common mistake is using Example for variable-length sequential data and then manually inventing padding and masking rules too early. That works, but it hides the fact that the data is sequential.
Another pitfall is misunderstanding FeatureList. It is not just "a list field"; it is an ordered list of feature entries, typically one entry per timestep.
It is also easy to mix record-level metadata into the sequence itself. Values like labels, ids, or global attributes usually belong in context, not in feature_lists.
Finally, remember that both formats store only primitive feature containers. If you need complex nested objects, you still need a clear serialization strategy on top of these message types.
Summary
- '
Exampleis best for flat, fixed-shape records with one logical set of features.' - '
SequenceExampleis best for ordered, variable-length sequence data with shared metadata.' - '
contextstores record-level attributes, whilefeature_listsstores per-step values.' - Both can live in TFRecord files and work with the same input pipeline style.
- Choose the format that matches the natural structure of the data instead of forcing one shape into another.
Related reading
- TensorFlow failed call to cuInit CUDA_ERROR_NO_DEVICE
- TensorFlow for binary classification
- tensorflow for poets The name 'import/input' refers to an Operation not in the graph.
- tensorflow for poets The name 'import/input' refers to an Operation not in the graph.
- TensorFlow Example vs SequenceExample
- Tensorflow executing an ops with a specific core of a CPU
- TensorFlow exponential moving average
- Tensorflow feature column for variable list of values
.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.