Parsing Examples
Set up a function for parsing Example objects with and without labels.
We'll cover the following...
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.
Press + to interact
import tensorflow as tfdef 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_specexample_spec = create_example_spec(True)parsed_example = tf.io.parse_single_example(ser_ex, example_spec)print(parsed_example)