Mapping

Transform each individual observation in a dataset through mapping.

Chapter Goals:

  • Learn how to map a function onto each observation of a dataset
  • Implement a function that creates a dataset of serialized protocol buffers and parses each observation

A. Mapping function

After initially creating a dataset from NumPy arrays or files, we oftentimes want to apply changes to make the dataset observations more useful. For example, we might create a dataset from heights measured in inches, but we want to train a model on the heights in centimeters. We can convert each observation to the desired format by using the map function.

Press + to interact
import numpy as np
import tensorflow as tf
data = np.array([65.2, 70. ])
d1 = tf.data.Dataset.from_tensor_slices(data)
d2 = d1.map(lambda x:x * 2.54)
print(d2)

In the example above, d1 is a dataset containing the height values from data, measured in inches. We ...