TECHNOLOGY

TFRecord and Example: define feature shape beyond serialized bytes

In this article
THE QUICK TAKE

TFRecord holds serialized records, while the Example parser determines how stored feature values become typed and shaped model inputs.

The container does not define the model example

TFRecordDataset reads records from TFRecord files. The returned serialized records still need interpretation. A common payload is a serialized tf.train.Example, but that message and the TFRecord container are different layers of the data pipeline.

For an image task, a record can contain encoded image bytes, labels and other features. Reading the byte string successfully does not demonstrate that the image can be decoded or that the label describes the intended image. Keep these checks separate when diagnosing a failed training input.

Example stores typed lists with named keys

The TensorFlow Example protocol defines a mapping from feature names to Feature values, using byte, floating-point or integer lists. The protocol comments explicitly note that data shape and use are determined by configurations and parsers. A flat list is not a complete description of the intended tensor axes.

An illustrative feature vector can represent a sequence or a flattened matrix. Both can fit into a list representation. Without the corresponding shape convention, a consumer may reconstruct a valid tensor with the wrong interpretation.

The parser supplies the feature contract

The parse_single_example API takes a mapping of feature keys to parsing specifications such as fixed- or variable-length features. That mapping determines the output tensors and sparse values. Preserve it with the dataset-preparation code rather than recreating it from memory after a model has been trained.

Check the semantics of missing features and defaults under the chosen specification. A default that makes a record parsable can still be inappropriate supervision. Decide which missing features should reject an example and which have a documented alternative meaning.

Decoding and reshaping need their own agreement

A byte feature can hold encoded media or other serialized content. The consumer must use the appropriate decoder and downstream transformation. A text feature also needs an encoding and tokenization contract before it becomes an LLM input.

Compare a small set of source examples with their parsed, decoded and reshaped forms. Retain identifiers through those transformations. If a list is flattened differently by the producer and consumer, the resulting tensor can have the expected shape while its values occupy the wrong positions.

Validate complete records before optimizing the reader

A useful preparation check distinguishes container read failures, Example parsing failures, decode failures and semantic label errors. These categories point to different corrections. Do not hide all of them behind a single skipped-record count when the resulting training population needs to be explained.

After the record contract is correct, batch and parallel reading can be tuned for the actual workload. Faster ingestion does not validate the feature schema. Keep the schema, decoding policy and selected dataset artifacts versioned with the training configuration.

Primary references

TensorFlow TFRecordDataset.

TensorFlow Example protocol.

TensorFlow parse_single_example.

Related explanations: Paired image and text learning; Provenance of derived samples.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles