Search⌘ K
AI Features

Parsing Examples

Explore how to create a function that parses features from serialized Example objects in TensorFlow. Understand separating input features from labels like Weekly_Sales to build an efficient data pipeline for training and evaluation of machine learning models.

Chapter Goals:

  • Create a function to parse feature data from serialized Example objects

A. Extracting feature data

The input pipeline consists of reading serialized Example objects from the TFRecords file and parsing feature data from the serialized Examples. We parse feature data for a single Example (which represents data for one DataFrame row) using the tf.io.parse_single_example function.

Python 3.5
import tensorflow as tf
def create_example_spec(has_labels):
example_spec = {}
int_vals = ['Store', 'Dept', 'IsHoliday', 'Size']
float_vals = ['Temperature', 'Fuel_Price', 'CPI', 'Unemployment']
if has_labels:
float_vals.append('Weekly_Sales')
for feature_name in int_vals:
example_spec[feature_name] = tf.io.FixedLenFeature((), tf.int64)
for feature_name in float_vals:
example_spec[feature_name] = tf.io.FixedLenFeature((), tf.float32)
example_spec['Type'] = tf.io.FixedLenFeature((), tf.string)
return example_spec
example_spec = create_example_spec(True)
parsed_example = tf.io.parse_single_example(ser_ex, example_spec)
print(parsed_example)

B. Labeled data

For both training and evaluation, we require the data to be labeled in order to ...