liveBook · Manning

Preprocessing

As explained in Deep Learning with R, Third Edition

Data preprocessing is the preparation of raw data by reshaping, scaling, encoding, or otherwise converting it into a model-compatible form.

Preprocessing turns raw data into inputs and targets a neural network can use. The work depends on the data: images may need reshaping and scaling, while text must become numeric tensors. It belongs before model training, not as an afterthought. section 6.2.1 section 14.2

Listing 2.4 Preparing the image data
train_images <- array_reshape(train_images, c(60000, 28 * 28))
train_images <- train_images / 255
test_images <- array_reshape(test_images, c(10000, 28 * 28))
test_images <- test_images / 255

Start with the MNIST image-preparation code: it applies the same reshape-and-scale steps to training and test images. section 2.1

Each MNIST image begins as a 28 × 28 grid of integer pixel values from 0 to 255. The dense network expects a flat input, so array_reshape() makes each image a vector of 784 values. Dividing by 255 converts the values to floating-point numbers between 0 and 1. Both changes happen before fit() or evaluation. section 2.1

Match the preparation to what the model will receive and predict:

  • Vectorize inputs and targets when needed. For text classification, multi-hot encoding can turn word sequences into floating-point tensors; data already in suitable vector form can skip that step. section 6.2.1
  • Normalize values. Image pixels can be divided by 255; house-price features with different ranges can be scaled independently to mean 0 and standard deviation 1. Large or mismatched ranges can cause disruptive gradient updates. section 6.2.1
  • Build an image input pipeline. JPEG files can be read, decoded to RGB, converted to tensors, resized to a shared size, and batched; image_dataset_from_directory() handles these steps. section 8.2.4
  • Prepare task-specific targets too. For grid-based object detection, bounding boxes are translated into grid coordinates, with class labels and box-confidence targets assigned to cells. section 12.2.3
Listing 14.11 Applying a bag-of-words encoding to the IMDb reviews
max_tokens <- 20000
text_vectorization <- layer_text_vectorization(
  max_tokens = max_tokens,
  split = "whitespace",  #1
  output_mode = "multi_hot"
)

train_ds_no_labels <- train_ds |> dataset_map(\(x, y) x)
adapt(text_vectorization, train_ds_no_labels)

bag_of_words_train_ds <- train_ds |>
  dataset_map(\(x, y) tuple(text_vectorization(x), y),
              num_parallel_calls = 8)
bag_of_words_val_ds <- val_ds |>
  dataset_map(\(x, y) tuple(text_vectorization(x), y),
              num_parallel_calls = 8)
bag_of_words_test_ds = test_ds |>
  dataset_map(\(x, y) tuple(text_vectorization(x), y),
              num_parallel_calls = 8)

The IMDb example shows a complete text route: adapt a TextVectorization layer on training inputs, then map training, validation, and test datasets to multi-hot vectors. section 14.4.1

Text preparation starts by splitting writing into tokens, such as characters or words, before mapping them to numeric representations. When processing text on the fly, speed matters: tokenization runs on the CPU, and parallel dataset mapping helps keep new batches ready for an accelerator. Precomputing tokens is another option when practical. section 14.2 section 14.4.1

In a machine-learning workflow, preprocessing follows the choice of training, validation, and test sets and precedes the first baseline model. Keep validation and test labels from leaking into training, then use validation performance—not test performance—to guide model changes. section 20.1.5

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through reshaping, scaling, tokenization, dataset pipelines, and detection targets; leaves domain-specific choices to practical examples.
Examples
Uses MNIST pixels, JPEG image directories, IMDb reviews, house-price features, and grid-aligned bounding boxes.
What it assumes
Starts with concrete image and text examples; defers teaching regular expressions rather than requiring their mechanics.
What you can do after
Apply it via Keras and tf.data to reshape or encode data, normalize values, and feed prepared batches to a model.
How it connects
Connects tensor vectorization, normalization, tokenization, data pipelines, object detection, and validation.

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 R, Third Edition. The section links point to the parts of the book it draws on.