How do I create padded batches in Tensorflow for tf.train.SequenceExample data using the DataSet API?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
tf.train.SequenceExample is a common format for variable-length sequence data such as token sequences, frame features, or event streams. When those sequences have different lengths, the normal batch operation is not enough, so the standard TensorFlow solution is to parse each example first and then use padded_batch to align sequence lengths within each batch.
Build or Read Serialized SequenceExample Records
A SequenceExample usually contains context features plus one or more variable-length feature lists. The example below creates two serialized records so the padding behavior is easy to see.
In a real pipeline these records often come from TFRecord files, but the padded-batch logic is the same.
Parse the Sequence Data First
Use tf.io.parse_single_sequence_example inside Dataset.map so the dataset contains tensors rather than raw serialized bytes.
After parsing, each element has a scalar label and a one-dimensional tokens tensor whose length may differ between records.
Use padded_batch
Now batch the parsed dataset and tell TensorFlow which shapes may vary and what padding value to use.
[None] means the sequence length may vary and should be padded to the longest sequence in the batch. The scalar label uses [] because it does not need sequence padding.
What the Shapes Mean
For the two-example dataset above, the result is a batch where:
- '
labelhas shape(2,)' - '
tokenshas shape(2, 3)'
The second example had only two tokens, so TensorFlow pads it with the provided value 0 to match the longest sequence length in the batch.
That is the main idea behind padded batching: keep batch dimensions regular without requiring all examples to have identical raw sequence lengths.
Include Additional Sequence Features
If your SequenceExample contains several parallel sequences, add them all to sequence_features and list them in padded_shapes.
The important rule is that each variable-length dimension needs a None in the padded shape declaration.
Pipeline Tips
A typical input pipeline also adds performance helpers.
prefetch and parallel mapping do not change correctness, but they often improve training throughput.
Common Pitfalls
The most common mistake is calling batch instead of padded_batch on variable-length sequences. Regular batching requires equal shapes and will fail.
Another issue is using the wrong padded_shapes declaration. Scalars need [], while variable-length one-dimensional sequences need [None].
A third problem is forgetting to parse the serialized SequenceExample before batching. padded_batch works on tensors, not on raw serialized protocol-buffer bytes.
Summary
- Parse
SequenceExamplerecords withtf.io.parse_single_sequence_examplebefore batching. - Use
padded_batchwhen sequence lengths vary across examples. - Mark variable-length dimensions with
Noneinpadded_shapes. - Supply explicit
padding_valueswhen the default zero is not appropriate. - Add
prefetchand parallel mapping after the logic is correct.

