liveBook · Manning

Preprocessing

As explained in Deep Learning with Python, Third Edition

The transformation of raw data into a form suitable for input to a model.

Raw data usually cannot go straight into a neural network. Preprocessing turns inputs—and, when necessary, targets—into tensors with values and shapes the model can use. The work depends on the data: a review needs a numeric representation, while an image may need rescaling and task-specific labels. section 6.2.1 section 12.2.3

From review to numbers

A text classifier can split a review into words, look up each word in a vocabulary, and use an [UNK] index for words the vocabulary lacks. Those indices are not the only possible model input: an IMDb sentiment example turns sequences of word indices into multi-hot float32 vectors. The packaged IMDb reviews already contain word indices; raw text requires the earlier steps too. section 14.2 section 6.2.1 section 4.1.1

Choose the transformations that solve a concrete input problem:

  • Vectorize: turn data into tensors. Inputs and targets are typically floating-point tensors, with integers or strings used in specific cases. section 6.2.1
  • Normalize: rescale image pixels from 0–255 to float32 values in 0–1, or scale each numeric feature independently when feature ranges differ. section 6.2.1 section 4.4
  • Standardize text when differences such as capitalization or punctuation should not produce different representations. Lowercasing and removing punctuation is one simple choice. section 14.2
  • Handle missing values where they occur; the appropriate preprocessing also depends on the data domain. section 6.2.1

Normalize house-price features

House-price inputs can mix small floating-point values with much larger integers. Before training, normalize each feature independently to mean 0 and standard deviation 1. Feeding large or heterogeneous values directly into a network is not a safe default. section 6.2.1

Preprocessing can also make labels match a model’s output. For a grid-based object detector, bounding boxes are translated into grid-relative coordinates; class and box arrays then assign labels, coordinates, and confidence values to grid locations. Images are decoded, passed through the matching image preprocessor, and batched with their labels. Streaming batches with tf.data avoids loading every decoded image into memory at once. section 12.2.3 section 12.2.2

Listing 12.9 Creating a dataset to train on
import tensorflow as tf

def load_image(path):                                               #1
    x = tf.io.read_file(path)
    x = tf.image.decode_jpeg(x, channels=3)
    return preprocessor(x)

images = tf.data.Dataset.from_tensor_slices([x["path"] for x in metadata])
images = images.map(load_image, num_parallel_calls=8)
labels = {"box": box_array, "class": class_array}
labels = tf.data.Dataset.from_tensor_slices(labels)

dataset = tf.data.Dataset.zip(images, labels).batch(16).prefetch(2)   #2
val_dataset, train_dataset = dataset.take(500), dataset.skip(500)     #3

The loading pipeline pairs preprocessed images with prepared targets, then batches and prefetches the dataset. section 12.2.3

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through vectorization, normalization, text transformations, and detection targets; leaves many domain-specific techniques to their practical examples.
Examples
Uses IMDb reviews, MNIST pixels, house-price features, and grid-based bounding-box targets.
What it assumes
Introduces vectorization from raw data while initially using IMDb reviews whose words have already been indexed.
What you can do after
Practice/perform converting text and numeric features into model-ready inputs and preparing grid-aligned detection targets.
How it connects
Links preprocessing to tensor vectorization, feature normalization, tokenization, vocabulary lookup, and object detection.

Also explained in

Compare how 22 books explain preprocessing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.